1. 程式人生 > >使用ffmpeg和nginx搭建一個簡易的HLS伺服器(2)-參考

使用ffmpeg和nginx搭建一個簡易的HLS伺服器(2)-參考

參考:http://idadu.org/wordpress/使用ffmpeg和nginx搭建一個簡易的hls伺服器/

使用ffmpeg和nginx搭建一個簡易的HLS伺服器

0. HLS Intro

HTTP Live Streaming (also known as HLS) is an HTTP-based media streaming communications protocol implemented by Apple Inc. as part of their QuickTime and iOS software. It works by breaking the overall stream into a sequence of small HTTP-based file downloads, each download loading one short chunk of an overall potentially unbounded transport stream. As the stream is played, the client may select from a number of different alternate streams containing the same material encoded at a variety of data rates, allowing the streaming session to adapt to the available data rate. At the start of the streaming session, it downloads an extended M3U (m3u8) playlist containing the metadata for the various sub-streams which are available.

image

上圖是一個典型基礎的HLS的結構圖。
Server是用來生成media檔案(media segment file)和播放列表檔案(m3u8 file)的功能套件。在上圖中,Server套件從攝像機中採集實時Audio/Video源,通過編碼和切割,生成目標檔案。
這種模式是典型的Live場景。另外一個模式是通過轉碼(transcode)和切割的方式,將已有的視訊檔案生成為目標檔案。本文就是使用ffmpeg工具來實現的這種模式。

Distribution是將Server生成的檔案部署到HTTP伺服器上。本文使用nginx作為HTTP server。

Client通過HTTP協議獲取檔案,按照HLS協議demux下載所得的檔案,然後playback。本文使用VLC播放器驗證所搭建的HLS server。

1. Why HLS

HLS具備的最大優勢是其基於HTTP的特性。streaming基於HTTP的意義在於硬體基礎設施基本上不需要做特殊的處理,HLS能很好的適配當前已有的網際網路架構。例如,firewall等無需對HLS開特例,HLS也能部署到CDN上,等等。

2. 使用ffmpeg生成ts segment檔案和m3u8檔案

從ffmpeg的官網上下載最新的程式碼進行編譯,生成可執行的二進位制檔案ffmpeg工具。
通過ffmpeg,可以生成segment檔案以及m3u8.

ffmpeg -i sample.mkv -codec copy -map 0 -f segment -vbsf h264_mp4toannexb -flags -global_header -segment_format mpegts -segment_list sample.m3u8 -segment_time 10 seg-%03d.ts

生成如下檔案:

[email protected]:/home/xixi/Videos# ll
total 139440
drwxr-xr-x  2 xixi xixi     4096 Nov  2 22:46 ./
drwxr-xr-x 27 xixi xixi     4096 Nov  2 22:40 ../
-rw-r--r--  1 root root      290 Nov  2 22:46 sample.m3u8
-rw-rw-r--  1 xixi xixi 68588654 Oct 30 00:30 sample.mkv
-rw-r--r--  1 root root 12087648 Nov  2 22:46 seg-000.ts
-rw-r--r--  1 root root 13876092 Nov  2 22:46 seg-001.ts
-rw-r--r--  1 root root 10020964 Nov  2 22:46 seg-002.ts
-rw-r--r--  1 root root 14175576 Nov  2 22:46 seg-003.ts
-rw-r--r--  1 root root 12369084 Nov  2 22:46 seg-004.ts

其中m3u8檔案如下:

[email protected]:/home/xixi/Videos# more sample.m3u8
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-ALLOW-CACHE:YES
#EXT-X-TARGETDURATION:12
#EXTINF:11.544733,
seg-000.ts
#EXTINF:11.044000,
seg-001.ts
#EXTINF:7.841000,
seg-002.ts
#EXTINF:10.377000,
seg-003.ts
#EXTINF:10.210000,
seg-004.ts
#EXTINF:9.700000,
seg-005.ts
#EXT-X-ENDLIST

3. 使用nginx部署HTTP server

在 /etc/nginx/nginx.conf 中新增Virtual Host Configs

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/hls;

在 /etc/nginx/sites-enabled 中新增名為hls的檔案如下

server {
listen   80; ## listen for ipv4; this line is default and implied
root /var/www/hls;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow all;
}
}

在 /etc/nginx/mime.types 中新增mime配置

application/x-mpegURL                   m3u8;

video/MP2T                              ts;

4. 使用VLC驗證HLS

下載VLC播放器,選單欄選擇 媒體 ,開啟網路串流,然後輸入url

http://192.168.0.103/sample.m3u8

VLC正常播放就驗證了這個簡易的HLS server

Reference

第一種方案:ffmpeg+nginx 新的ffmpeg已經支援HLS。(本人也參與了程式碼供獻,給自己做個廣告:)) 點播: 生成hls分片: ffmpeg -i <媒體檔案> -c:v libx264 -c:a -f hls /usr/local/nginx/html/test.m3u8 直播: ffmpeg -i udp://@:1234 -c:v libx264 -c:a -f hls  /usr/local/nginx/html/test.m3u8 建立web伺服器: 預設配置就可以。  server {         listen       80;         server_name  localhost;         #charset koi8-r;         #access_log  logs/host.access.log  main;         location / {             root   html;             index  index.html index.htm;         } } 啟動nginx。 客戶端訪問:http://IP/test.m3u8 在windows上可以用vlc播放。 第二個文案,用nginx-rtmp-module rtmp {     server {         listen 1935;         chunk_size 4000;         #HLS         # For HLS to work please create a directory in tmpfs (/tmp/app here)         # for the fragments. The directory contents is served via HTTP (see         # http{} section in config)         #         # Incoming stream must be in H264/AAC. For iPhones use baseline H264         # profile (see ffmpeg example).         # This example creates RTMP stream from movie ready for HLS:         #         # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264          #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1          #    -f flv rtmp://localhost:1935/hls/movie         #         # If you need to transcode live stream use 'exec' feature.         #         application hls {             live on;             hls on;             hls_path /tmp/app;             hls_fragment 5s;         }     } } http {     server {         listen      80;         location /hls {             # Serve HLS fragments             types {                 application/vnd.apple.mpegurl m3u8;                 video/mp2t ts;             }             alias /tmp/app;             expires -1;         }     } }