顯示具有 Html5 標籤的文章。 顯示所有文章
顯示具有 Html5 標籤的文章。 顯示所有文章

2014年7月21日 星期一

HTTP Live Streaming

其實今天並沒有要研究HLS,只是要找一個不需掛載任何播放插件的方法,播放網路上的串流。結果找上了HLS就稍後try了一下…

HTTP Live Streaming (HLS)其實是Apple所制定的,主要是給iOS裝置的safari瀏覽器上播放串流。

什麼情況下可以使用HLS:
  • Streaming audio or video to iPhone, iPod touch, iPad, or Apple TV
  • Streaming live events without special server software
  • Sending video on demand with encryption and authenticationtt
Http Live Streaming 的架構其實簡單,主要是將需票串流的檔案切成小片段(.ts),再用一個index file(.m3u8)記錄每個小片段組成播放清單。
所以需把mp4檔案,切成HLS的架構,所幸ffmpeg就可支援了。

ffmpeg -y -i taiwan-240p.mp4 -pix_fmt yuv420p -vcodec libx264 -acodec libfaac -r 25 -profile:v baseline -b:v 1500k -maxrate 2000k -force_key_frames 50  -map 0 -flags -global_header -f segment -segment_list ./hls/index.m3u8 -segment_time 10 -segment_format mpeg_ts -segment_list_type m3u8 ./hls/segment%05d.ts

最後把 index.m3u8 塞給播放端即可。
居然神奇的pi是可以播Http Live Streaming的格式…太強了。
Http Live Streaming主要在支援行動裝置播放的,可以直接用HTML5的video tag,給Apple iOS的safari讀取和播放。例如:

<video  src="http://192.168.101.253/streaming/index.m3u8"   height="300" width="400" controls  > </video>

測試在iPhone和iPad的safari和chrome都可以播放:


測試Android 4.4.2的chrome,可以播放,不過剛開始需拉動一下進度bar影像才會出來,不然只有聲音。看來Android的支援度還沒有很完整。



參考資料:

2014年7月19日 星期六

Start a streaming from web

終於搞定由Web上啟動一個串流播放,一直困擾怎麼把ffmpeg丟到背景執行,搞了一個早上。參考了php:exec上的範例,才搞定這。不過,還不太了解下面這行:
$command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
試過其他的輸出導向都會卡住… 看來這篇文章還要好好研究一下…
研究了php fork process,原來php也可以fork,有趣了…用PHP寫Multi Process程式




Internet Streaming


越玩越起勁了,網路串流整合成功。SRS+FFmpeg+PHP+Video.js+Html5
FFmpeg – the swiss army knife of Internet Streaming

This will execute $cmd in the background (no cmd window) without PHP waiting for it to finish, on both Windows and Unix. 

<?php 
function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
?>