您的位置:首页 > 理论基础 > 计算机网络

FFMPEG采集摄像头数据并切片为iPhone的HTTP Stream流

2016-03-27 17:01 417 查看
一、Windows下面编译ffmpeg

首先需要解决的问题是:在windows下面编译 ffmpeg, 并让其支持dshow, 本人把ffmpeg编译成功了, 但是编译出来的ffmpeg不支持dshow, 在网上找了有文章介绍如何编译ffmpeg让其支持dhsow, 按照文章说的方法试了N次, 终究没有成功。无奈只有找现成的windows下面的exe了。

在这里找到了可用的ffmpeg.exe, 测试了一下,支持dshow。

下载地址为: http://ffmpeg.zeranoe.com/builds/

请下载适合自己操作系统的压缩包, 我的系统是Win7 64Bit 的,所以选择的是 http://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-20130809-git-3b2e99f-win64-static.7z

下载解压到C盘根目录。

二、测试FFMPEG支持dshow的情况

原文出处请参考 :http://ffmpeg.org/trac/ffmpeg/wiki/DirectShow

执行下面的命令 , 即可显示你的系统支持音频捕获设备,视频捕获设备:

[plain] view
plain copy

c:\> ffmpeg -list_devices true -f dshow -i dummy

ffmpeg version N-45279-g6b86dd5... --enable-runtime-cpudetect

libavutil 51. 74.100 / 51. 74.100

libavcodec 54. 65.100 / 54. 65.100

libavformat 54. 31.100 / 54. 31.100

libavdevice 54. 3.100 / 54. 3.100

libavfilter 3. 19.102 / 3. 19.102

libswscale 2. 1.101 / 2. 1.101

libswresample 0. 16.100 / 0. 16.100

[dshow @ 03ACF580] DirectShow video devices

[dshow @ 03ACF580] "Integrated Camera"

[dshow @ 03ACF580] "screen-capture-recorder"

[dshow @ 03ACF580] DirectShow audio devices

[dshow @ 03ACF580] "Internal Microphone (Conexant 2"

[dshow @ 03ACF580] "virtual-audio-capturer"

dummy: Immediate exit requested

我们下来使用 视频捕获设备 "Integraged Camera" 保存为MP4文件:

[plain] view
plain copy

c:\> ffmpeg -f dshow -i video="Integrated Camera" out.mp4

嘿嘿,摄像头灯亮了,文件已经输出到了和 ffmpeg.exe 相同的路径下面。 用VLC播放 out.mp4文件,正常。

用下面的命令可以查看视频捕获设备支持的图片大小等详细信息。

[plain] view
plain copy

C:\ffmpeg\bin>ffmpeg -f dshow -list_options true -i video="Integrated Camera"

用下面的命令我们可以把视频分辨率存储为1280x720, FPS为15帧/秒, 输出为 out.avi 文件。是不是很方便啊 ?

[plain] view
plain copy

C:\>ffmpeg -f dshow -s 1280x720 -r 15 -vcodec mjpeg -i video="Integrated Camera" out.avi

三、测试FFMPEG对DSHOW支持的[b]另外一种方法[/b]

原文出处 : http://ffmpeg.org/trac/ffmpeg/wiki/How%20to%20capture%20a%20webcam%20input

用下面的命令我们也可以输出视频捕获设备,只不过不是名字,而是ID的形式。

[plain] view
plain copy

ffmpeg -y -f vfwcap -i list

输出如下 :

[plain] view
plain copy

libavutil 52. 41.100 / 52. 41.100

libavcodec 55. 24.100 / 55. 24.100

libavformat 55. 13.102 / 55. 13.102

libavdevice 55. 3.100 / 55. 3.100

libavfilter 3. 82.100 / 3. 82.100

libswscale 2. 4.100 / 2. 4.100

libswresample 0. 17.103 / 0. 17.103

libpostproc 52. 3.100 / 52. 3.100

[vfwcap @ 000000000034d940] Driver 0

[vfwcap @ 000000000034d940] Microsoft WDM Image Capture (Win32)

[vfwcap @ 000000000034d940] Version: 6.1.7600.16385

list: Input/output error

从上面看出 vfwcap的索引号为0, 用下面的命令即可捕获视频数据。其中 -i 0 就代表使用ID为0的视频采集设备, -r 25代表帧率为25帧/秒

[plain] view
plain copy

ffmpeg -y -f vfwcap -r 25 -i 0 out.mp4

四、Apple公司的HLS文件切片相关资料

原文出处为 : http://orfika.net/src/mpegts-segmenter/

下面是相关的技术资料的地址 :

MPEG TS segmenter by Chase Douglas source: http://svn.assembla.com/svn/legend/segmenter/ ffmpeg & segmenter LiveStream Howto:       http://www.ioncannon.net/programming/452/iphone-http-streaming-with-ffmpeg-and-an-open-source-segmenter/ Apple's LiveStream docs:                   https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/Introduction/Introduction.html Apple's LiveStream draft at IETF:          http://tools.ietf.org/html/draft-pantos-http-live-streaming-07" ffmpeg 0.10.0 release:                     http://ffmpeg.org/download.html#release_0.10 Cardon McDonal's IOCANNON:                 http://www.ioncannon.net/


Index of /src/mpegts-segmenter/

NameLast ModifiedSizeType
Parent Directory/- Directory
mpegts-segmenter.diff2012-Feb-07 10:13:385.7Kapplication/octet-stream
mpegts-segmenter.spec2012-Feb-07 10:13:380.9Kapplication/octet-stream
mpegts-segmenter.tar.gz2012-Feb-07 10:13:389.9Kapplication/octet-stream
还附带文件切片源码。

下面的地址也可以下载 切片工具的源码,这个工具可能比较完善一点吧。

https://github.com/johnf/m3u8-segmenter 这个是C的切片工具

https://github.com/streamio/streamio-ffmpeg 这个是ruby语言写的转码和切片

五、用FFMPEG实现iPhone的HTTP Stream技术步骤

原文出处 :http://www.ioncannon.net/programming/452/iphone-http-streaming-with-ffmpeg-and-an-open-source-segmenter/


Step 1: 获取最新版本的 FFMpeg

The FFMpeg download page 从该地址获取最新版本的ffmpeg

使用下面的命令进行配置,生成Makefile文件,然后make吧。
configure --enable-gpl --enable-nonfree --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libx264


其中最重要的事情是注意 --enable-libx264 这个编译选项。


Step 2: 转码视频格式,让 iPhone 可以使用

我们必须让ffmpeg创建 X264编码格式的视频流, iPhone才能播放,有几个步骤需要注意:

视频文件的码率必须在: 100 Kbps to 1.6 Mbps 这个范围

苹果公司建议的视频流为:

Low – 96 Kbps video, 64 Kbps audio
Medium – 256 Kbps video, 64 Kbps audio
High – 800 Kbps video, 64 Kbps audio

iPhone 的屏幕视频播放尺寸设置为: 480×320

建议使用下面的参数进行视频转码:

ffmpeg -i <in file> -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s 320×240 -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder
0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 <output file>

假如你想知道这些命令参数的更详细的信息,请参考 X264 encoding guide 和 FFMpeg
documentation , 上面例子设置的码率为98k, 你可以修改为你想设置的码率。

要更改的参数为 “ -b, -maxrate, -bufsize values ”


Step 3: 下载并编译 segmenter

现在,你已经完成了视频采集的工作,但是还没有完成整个构建HTTP Streaming 的过程。 你需要一种方法来把视频文件切成小块,你可以下载苹果的 segmenter 。

下载切片源码的SVN地址为 : segmenter source.

下载下来后用下面的命令即可编译 :

all:

gcc -Wall -g segmenter.c -o segmenter -lavformat -lavcodec -lavutil -lbz2 -lm -lz -lfaac -lmp3lame -lx264 -lfaad
clean:

rm segmenter

在编译完成了 segmenter 工具之后, 你就可以创建你的 HTTP Streaming 内容了。

命令格式为:

segmenter <input MPEG-TS file> <segment duration in seconds> <output MPEG-TS file prefix> <output m3u8 index file> <http prefix>

下面是一个使用的例子,从视频文件创建一个流, 每个切片文件10秒:

segmenter sample_low.ts 10 sample_low stream_low.m3u8 http://www.ioncannon.net/


Step 4: 准备 HTTP server 服务器

进行到这一步的时候, 你应该已经有好多视频流的切片文件了,这些文件可以上传到web服务器, 但是这里有一个比较重要的事情需要注意,那就是mime types的设置。

.m3u8 application/x-mpegURL

.ts video/MP2T

假如你使用的是Apache服务器的话,你需要添加如下的代码到 httpd.conf 配置文件里:

AddType application/x-mpegURL .m3u8

AddType video/MP2T .ts

假如你使用的是 lighttpd 服务器的话,你需要添加下面的代码到你的配置文件中:

mimetype.assign = ( ".m3u8" => "application/x-mpegURL", ".ts" => "video/MP2T" )


Step 5: 测试 stream

万事俱备只欠东风了,下来需要使用 HTML5 的 video 标签,例子如下:

<html>

<head>

<title>Video Test</title>

<metaname="viewport"content="width=320;
initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>

</head>

<bodystyle="background-color:#FFFFFF;
">

<center>

<video width='150'height='150'src="stream-128k.m3u8"/>

</center>

</body>

</html>

上面所有的步骤都正确的话,你现在应该已经看到视频了。

假如你想在应用程序里面测试上面的视频流的话,你需要下载苹果公司的视频播放器,下载地址为: download the MoviePlayer iPhone demo
application 。


Step 6: 自动化的 stream 编码和切片 segmentation

这是一个小脚本,可以把输入文件编码转换后再切片为10秒一个的文件小块。

#!/bin/sh
BR=800k

ffmpeg -i $1 -f mpegts -acodec libmp3lame -ar 48000-ab 64k-s 320×240-vcodec libx264-b$BR-flags+loop-cmp +chroma-partitions +parti4x4+partp8x8+partb8x8-subq 5-trellis 1-refs 1-coder 0
-me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71-bt 200k-maxrate$BR-bufsize$BR -rc_eq'blurCplx^(1-qComp)'-qcomp 0.6-qmin 10-qmax 51-qdiff 4-level 30-aspect 320:240-g 30-async 2
sample_$BR_pre.ts

segmenter sample_$BR_pre.ts 10 sample_$BR stream-$BR.m3u8 http://www.ioncannon.net/
rm -f sample_$BR_pre.ts


Step 7: 创建不同码率 rate 的 HTTP stream

之前将的例子都是创建单一码率的HTTP Stream, 我们需要创建不同码率的视频流, 下面是一个简单的小例子。

[plain] view
plain copy

#EXTM3U

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=96000
http://192.168.132.15/ipv/stream-96k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=256000
http://192.168.132.15/ipv/stream-256k.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000
http://192.168.132.15/ipv/stream-800k.m3u8
六、让nginx支持MP4文件的直接播放

这是nginx的第三方模块的网址 : http://wiki.nginx.org/3rdPartyModules

在上面可以找到支持MP4的模块,我们也可以直接从下面的网址下载 :

http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz

编译的时候可能会有点小错误:

解决错误:

因为在新版本的nginx中废弃了 zero_in_uri 这个flag,稍微修改一下 nginx_mod_h264_streaming 的源代码

vim /usr/local/src/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c

把158到161行注释掉

157 /* TODO: Win32 */

158 //if (r->zero_in_uri)

159 //{

160 // return NGX_DECLINED;

161 //}

然后再make就正常了,make install 完成安装

在nginx配置文件中加入

location ~ .mp4$ {

mp4;

}

下面的命令从摄像头采集数据后发送到服务器进行切片

[plain] view
plain copy

C:\ffmpeg-win64-static\bin>ffmpeg.exe -f dshow -i video="Integrated Camera" -vcodec libx264 -pix_fmt yuv420p -f flv rtmp://192.168.59.129/hls/mystream

下面的命令时以文件gd.flv为输入数据流, 转码后发送到服务器进行切片

[plain] view
plain copy

C:\ffmpeg-win64-static\bin>ffmpeg.exe -re -i "E:\Movie\gd.flv" -vcodec copy -acodec copy -f flv rtmp://192.168.59.129/hls/mystream

[plain] view
plain copy

ffmpeg.exe -f dshow -i video="Integrated Camera":audio="麦克风 (Realtek High Definition Au" -q 4 -s 640*480 -aspect 4:3 -r 10 -vcodec flv -ar 22050 -ab 64k -ac 1 -acodec libmp3lame -threads 4 -f flv rtmp://192.168.59.129/hls/mystream

ffmpeg.exe -f dshow -i video="Integrated Camera":audio="麦克风 (Realtek High Definition Au" -s 640*480 -vcodec libx264 -acodec libmp3lame -pix_fmt yuv420p -threads 4 -f flv rtmp://192.168.59.129/hls/mystream

虚拟切片:

https://github.com/AndyA/ts-split

摄像头采集后的数据直接切片 :

ffmpeg.exe -f dshow -i video="Integrated Camera":audio="麦克风 (Realtek High Definition Au" -s 640*480 -vcodec libx264 -acodec libmp3lame -flags -global_header -map 0 -f segment -segment_time 10 -segment_list live.m3u8 -segment_format mpegts live%05d.ts

FMPEG 把文件传送到rtmp的速度不正常的问题:

ffmpeg -re -i "输入文件"-qscale 3 -s 720*360 -aspect 16:9 -r 25 -threads 4 -vcodec flv-acodec libmp3lame -ar 44100 -ab 128k -ac 2 -f flvrtmp://127.0.0.1/rtmpsvr/RtmpVideo

关键是 -re解决同步压缩和办法的速度同步问题,而且要放在前面,不然不一定起作用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: