ffmpeg 网络实时视频

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingxia2008/article/details/27537055

ffmpeg和ffserver配合使用可以实现实时的流媒体服务,这里实时的数据来自摄像头,如 果考虑到应用程序和网络状况,这里客户端看到的画面还是会落后与摄像头本地获取的画面,最糟糕时甚至看不到画面(扯远了),这里我们关注的是ffmpeg 和ffserver之间是如何配合工作的,了解了他们之间的关系,对个别问题也能有的放矢。

ffserver先于ffmpeg启动,它在启 动的时候需要加参数-f指定其配置文件,配置文件里包含有向客户端传送的流的配置(如编码方式,帧率,采样率……),也有对feed1.ffm的配置,还 有其他的一些配置。feed1.ffm是什么?这里可以理解为一个缓冲文件,下面会提到它是如何被用到,ffserver启动后,feed1.ffm就会 被创建,这时如果你打开feed1.ffm看看,会发现feed1.ffm开始的部分已经写入了内容,你可以找到关键字ffm以及向客户端传送流的配置信 息,在feed1.ffm做缓冲用的时候,这些信息是不会被覆盖掉的,就把它们理解为feed1.ffm文件的头吧。

ffserver启动 后,ffmpeg启动,它启动时加的一个关键参数就是“http://ip:8090/feed1.ffm”,其中ip是运行ffserver主机的 ip,如果ffmpeg和ffserver都在同一系统中运行的话,用localhost也行。ffmpeg启动后会与ffserver建立一个连接(短 暂的连接),通过这第一次的连接,ffmpeg从ffserver那里获取了向客户端输出流的配置,并把这些配置作为自己编码输出的配置,然后 ffmpeg断开了这次连接,再次与ffserver建立连接(长久的连接),利用这个连接ffmpeg会把编码后的数据发送给ffserver。如果你 观察ffserver端的输出就会发现这段时间会出现两次HTTP的200,这就是两次连接的过程。

ffmpeg从摄像头获取数据后,按照 输出流的编码方式编码,然后发送给ffserver,ffserver收到ffmpeg的数据后,如果网络上没有播放的请求,就把数据写入 feed1.ffm中缓存,写入时把数据加上些头信息然后分块,每块4096B(每块也有结构),当feed1.ffm的大小到了 ffserver.conf中规定的大小后,就会从文件开始(跳过头)写入,覆盖旧的数据。直到网络上有播放的请求,ffserver从 feed1.ffm中读取数据,发送给客户端。

上面粗略的描述在实时流媒体服务时ffmpeg和ffserver的关系,这些看法都是在阅 读较早FFmpeg代码(very old)时得出的,现在不知这个架构是否改变,兄弟我把砖扔出,希望大家用玉来砸。

参考:

http://blog.csdn.net/eastmoon502136/article/details/8286678

libav for Ubuntu:

2011  sudo apt-get install libav-tools
2012  ffserver

jxdong@ubuntu-server:/media/android/jiangxd/bin$ vi /etc/avserver.conf

# Port on which the server is listening. You must select a different
# port from your standard HTTP web server if it is running on the same
# computer.
Port 8090

# Address on which the server is bound. Only useful if you have
# several network interfaces.
BindAddress 0.0.0.0

http://192.168.2.149:8090/stat.html

麦库截图20131616164318250.jpg

jxdong@ubuntu-server:/media/android/jiangxd/bin$ ffserver -f /etc/avserver.conf
avserver version 0.8.6-4:0.8.6-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
  built on Apr  2 2013 17:02:36 with gcc 4.6.3
Wed Oct 16 16:51:55 2013 Opening file '/var/www/mp3/2012.mp4'
Wed Oct 16 16:51:55 2013 [mov,mp4,m4a,3gp,3g2,mj2 @ 0xb2a020]max_analyze_duration reached
Wed Oct 16 16:51:55 2013 AVserver started.
Wed Oct 16 16:52:00 2013 [mov,mp4,m4a,3gp,3g2,mj2 @ 0xbafc60]max_analyze_duration reached
Wed Oct 16 16:52:00 2013 Tag mp4v/0x7634706d incompatible with output codec id '13'
Wed Oct 16 16:52:00 2013 Error writing output header
Wed Oct 16 16:52:00 2013 192.168.2.151 - - [GET] "/2012.mp4 HTTP/1.0" 200 68
Wed Oct 16 16:52:04 2013 [mov,mp4,m4a,3gp,3g2,mj2 @ 0xd29560]max_analyze_duration reached
Wed Oct 16 16:52:04 2013 Tag mp4v/0x7634706d incompatible with output codec id '13'
Wed Oct 16 16:52:04 2013 Error writing output header
Wed Oct 16 16:52:04 2013 192.168.2.151 - - [GET] "/2012.mp4 HTTP/1.0" 200 68
Wed Oct 16 16:55:30 2013 File '/feed1.ffm' not found
Wed Oct 16 16:55:30 2013 192.168.2.151 - - [GET] "/feed1.ffm HTTP/1.1" 404 149

开始使用 http :   http://192.168.2.149:8090/2012.mp4   获取 2012.mp4

后来使用 rtp  :    rtp://192.168.2.149:8090/2012.mp4      获取 feed.ffm

2.3 How do I make it work?

First, build the kit. It *really* helps to have installed LAME first. Then when you run the ffserver ./configure, make sure that you have the --enable-libmp3lame flag turned on.

LAME is important as it allows for streaming audio to Windows Media Player. Don’t ask why the other audio types do not work.

As a simple test, just run the following two command lines where INPUTFILE is some file which you can decode with ffmpeg:

ffserver -f doc/ffserver.conf &
ffmpeg -i INPUTFILE http://localhost:8090/feed1.ffm

At this point you should be able to go to your Windows machine and fire up Windows Media Player (WMP). Go to Open URL and enter

    http://<linuxbox>:8090/test.asf

You should (after a short delay) see video and hear audio.

WARNING: trying to stream test1.mpg doesn’t work with WMP as it tries to transfer the entire file before starting to play. The same is true of AVI files.

在 android 上测试通过 只能用  localhost  , 不能用 IP

猜你喜欢

转载自blog.csdn.net/jingxia2008/article/details/27537055