使用ffmpeg和nginx搭建一个简易的HLS服务器(2)-参考
2014-04-16 15:11
686 查看
参考:http://idadu.org/wordpress/使用ffmpeg和nginx搭建一个简易的hls服务器/
2013年11月2日MultiMediaHLS
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.
上图是一个典型基础的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。
HLS具备的最大优势是其基于HTTP的特性。streaming基于HTTP的意义在于硬件基础设施基本上不需要做特殊的处理,HLS能很好的适配当前已有的互联网架构。例如,firewall等无需对HLS开特例,HLS也能部署到CDN上,等等。
从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
生成如下文件:
root@xixi10111011:/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文件如下:
root@xixi10111011:/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
在 /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;
下载VLC播放器,菜单栏选择 媒体 ,打开网络串流,然后输入url
http://192.168.0.103/sample.m3u8
VLC正常播放就验证了这个简易的HLS server
http://en.wikipedia.org/wiki/HLS
https://developer.apple.com/streaming/
http://www.ffmpeg.org/
http://www.videolan.org/
第一种方案: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;
}
}
}
使用ffmpeg和nginx搭建一个简易的HLS服务器
2013年11月2日MultiMediaHLS
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.
上图是一个典型基础的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
生成如下文件:
root@xixi10111011:/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文件如下:
root@xixi10111011:/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播放器,菜单栏选择 媒体 ,打开网络串流,然后输入urlhttp://192.168.0.103/sample.m3u8
VLC正常播放就验证了这个简易的HLS server
Reference
http://en.wikipedia.org/wiki/HLShttps://developer.apple.com/streaming/
http://www.ffmpeg.org/
http://www.videolan.org/
第一种方案: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;
}
}
}
相关文章推荐
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFmpeg搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用nginx+ffmpeg搭建hls服务器
- 使用Nginx+FFmpeg搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用Nginx+FFMPEG搭建HLS直播转码服务器
- 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
- Nginx+ffmpeg的HLS开源服务器搭建配置及开发详解
- ffmpeg+nginx搭建HLS服务器及基于ARM实现的简单hls解决方案
- Nginx+ffmpeg的HLS开源服务器搭建配置及开发详
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
- Windows系统下ffmpeg+nginx+Vitamio搭建HLS服务器+Android客户端方法
- 基于nginx和ffmpeg搭建HLS流媒体服务器
- 利用FFMPEG+Nginx 搭建 HLS 服务器