ffmpeg学习---11.ffmpeg的流媒体服务器与播放器的设置

一. 用ffmpeg播放
ffserver
它有两个参数需要注意 
-d --> enalbe debug mode
-f --> 指定配置文件的路径,如果没有-f参数则默认是/etc/ffserver.conf
1. ffmpeg编译完成后会生成
ffserver
ffmpeg
ffplay

ffserver.conf文件(ffmpeg源码下的doc目录中有ffserver.conf里面有详细解析)
  1. HTTPPort 8091
  2. HTTPBindAddress 0.0.0.0
  3. MaxClients 10
  4. MaxBandwidth 50000
  5. <Feed feed1.ffm>
  6. file /tmp/feed1.ffm                 -->缓冲文件的路径是/tmp/feed1.ffm
  7. FileMaxSize 10M                     -->缓冲文件最大是10M
  8. </Feed>

  9. <Stream feed1.mjpeg>
  10. Feed feed1.ffm
  11. Format mjpeg
  12. VideoSize 640x480
  13. VideoFrameRate 10
  14. VideoBitRate 2000
  15. VideoQMin 1
  16. VideoQMax 10
  17. </Stream>


2. 运行结果
2.1 首先运行server端
  1. cong@msi:/work/ffmpeg/out/bin$ ./ffserver --./ffserver.conf
  2. ffserver version 2.7.2 Copyright (c) 2000-2015 the FFmpeg developers
  3.   built with gcc 4.(Ubuntu 4.8.2-19ubuntu1)
  4.   configuration: --enable-shared --prefix=../out
  5.   libavutil 54. 27.100 / 54. 27.100
  6.   libavcodec 56. 41.100 / 56. 41.100
  7.   libavformat 56. 36.100 / 56. 36.100
  8.   libavdevice 56. 4.100 / 56. 4.100
  9.   libavfilter 5. 16.101 / 5. 16.101
  10.   libswscale 3. 1.101 / 3. 1.101
  11.   libswresample 1. 2.100 / 1. 2.100
  12. ./ffserver.conf:19: Setting default value for video bit rate tolerance = 500000. Use NoDefaults to disable it.
  13. ./ffserver.conf:19: Setting default value for video rate control equation = tex^qComp. Use NoDefaults to disable it.
  14. ./ffserver.conf:19: Setting default value for video max rate = 4000000. Use NoDefaults to disable it.
  15. ./ffserver.conf:19: Setting default value for video buffer size = 4000000. Use NoDefaults to disable it.
  16. ./ffserver.conf:50: Setting default value for video bit rate tolerance = 75000. Use NoDefaults to disable it.
  17. ./ffserver.conf:50: Setting default value for video rate control equation = tex^qComp. Use NoDefaults to disable it.
  18. ./ffserver.conf:50: Setting default value for video max rate = 600000. Use NoDefaults to disable it.
  19. ./ffserver.conf:50: Setting default value for video buffer size = 600000. Use NoDefaults to disable it.
  20. Mon Sep 14 18:18:38 2015 Deleting feed file '/tmp/feed1.ffm' as stream counts differ (!= 2)
  21. Mon Sep 14 18:18:38 2015 FFserver started.
  22. Mon Sep 14 18:18:51 2015 127.0.0.- - New connection: POST /feed1.ffm
  23. Mon Sep 14 18:18:53 2015 127.0.0.- - [POST] "/feed1.ffm HTTP/1.1" 200 671744
2.2 然后在另一个终端运行ffmpeg
ffmpeg用来把本地摄像头中的数据转为feed1.ffm缓存起来
  1. cong@msi:/work/ffmpeg/out/bin$ ./ffmpeg -v verbose -r 5 -s 640x480 -f video4linux2 -/dev/video0 http://localhost:8091/feed1.ffm
  2. ffmpeg version 2.7.2 Copyright (c) 2000-2015 the FFmpeg developers
  3.   built with gcc 4.(Ubuntu 4.8.2-19ubuntu1)
  4.   configuration: --enable-shared --prefix=../out
  5.   libavutil 54. 27.100 / 54. 27.100
  6.   libavcodec 56. 41.100 / 56. 41.100
  7.   libavformat 56. 36.100 / 56. 36.100
  8.   libavdevice 56. 4.100 / 56. 4.100
  9.   libavfilter 5. 16.101 / 5. 16.101
  10.   libswscale 3. 1.101 / 3. 1.101
  11.   libswresample 1. 2.100 / 1. 2.100
  12. [video4linux2,v4l2 @ 0x115f180] fd:3 capabilities:84000001
  13. Input #0, video4linux2,v4l2, from '/dev/video0':
  14.   Duration: N/A, start: 30290.856550, bitrate: 24576 kb/s
  15.     Stream #0:0: Video: rawvideo, 1 reference frame (YUY2 / 0x32595559), yuyv422, 640x480, 24576 kb/s, 5 fps, 5 tbr, 1000k tbn, 1000k tbc
  16. [tcp @ 0x11641e0] Connection to tcp://localhost:8091 failed (Connection refused), trying next address
  17. [tcp @ 0x116c6a0] Connection to tcp://localhost:8091 failed (Connection refused), trying next address
  18. [graph 0 input from stream 0:0 @ 0x1154680] w:640 h:480 pixfmt:yuyv422 tb:1/5 fr:5/1 sar:0/1 sws_param:flags=2
  19. [scaler for output stream 0:0 @ 0x1165b60] w:640 h:480 flags:'0x4' interl:0
  20. [graph 0 input from stream 0:0 @ 0x1154680] TB:0.200000 FRAME_RATE:5.000000 SAMPLE_RATE:nan
  21. [swscaler @ 0x1141040] deprecated pixel format used, make sure you did set range correctly
  22. [scaler for output stream 0:0 @ 0x1165b60] w:640 h:480 fmt:yuyv422 sar:0/-> w:640 h:480 fmt:yuvj422p sar:0/1 flags:0x4
  23. [graph 1 input from stream 0:0 @ 0x1156a80] w:640 h:480 pixfmt:yuyv422 tb:1/5 fr:5/1 sar:0/1 sws_param:flags=2
  24. [scaler for output stream 0:1 @ 0x1157b00] w:640 h:480 flags:'0x4' interl:0
  25. [graph 1 input from stream 0:0 @ 0x1156a80] TB:0.200000 FRAME_RATE:5.000000 SAMPLE_RATE:nan
  26. [scaler for output stream 0:1 @ 0x1157b00] w:640 h:480 fmt:yuyv422 sar:0/-> w:640 h:480 fmt:yuv420p sar:0/1 flags:0x4
  27. Output #0, ffm, to 'http://localhost:8091/feed1.ffm':
  28.   Metadata:
  29.     creation_time : now
  30.     encoder : Lavf56.36.100
  31.     Stream #0:0: Video: mjpeg, 1 reference frame, yuvj422p(pc), 640x480, q=1-10, 2000 kb/s, 5 fps, 1000k tbn, 10 tbc
  32.     Metadata:
  33.       encoder : Lavc56.41.100 mjpeg
  34.     Stream #0:1: Video: mpeg4, 1 reference frame, yuv420p, 640x480, q=2-51, 300 kb/s, 5 fps, 1000k tbn, 24 tbc
  35.     Metadata:
  36.       encoder : Lavc56.41.100 mpeg4
  37. Stream mapping:
  38.   Stream #0:-> #0:(rawvideo (native) -> mjpeg (native))
  39.   Stream #0:-> #0:(rawvideo (native) -> mpeg4 (native))
  40. Press [q] to stop, [?] for help
  41. *** 1 
  42. *** 4 
  43. *** 1 
  44. *** 4 
  45. *** 1 
  46. *** 4 
  47. frame= 6 fps=0.0 q=1.6 Lq=12.8 size= 216kB time=00:00:00.62 bitrate=2831.2kbits/s dup=15 drop=
  48. video:210kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.969670%
  49. Input file #0 (/dev/video0):
  50.   Input stream #0:(video): 3 packets read (1843200 bytes); 3 frames decoded; 
  51.   Total: 3 packets (1843200 bytes) demuxed
  52. Output file #0 (http://localhost:8091/feed1.ffm):
  53.   Output stream #0:(video): 6 frames encoded; 6 packets muxed (181221 bytes); 
  54.   Output stream #0:(video): 15 frames encoded; 15 packets muxed (33584 bytes); 
  55.   Total: 21 packets (214805 bytes) muxed
  56. Received signal 2: terminating.
2.3 查看
这样在/tmp目录下就会生与一个feed1.ffm作为缓冲文件
  1. cong@msi:/work/ffmpeg/out/bin$ ls -/tmp/feed1.ffm 
  2. -rw-rw-r-- 1 cong cong 888832 Sep 14 18:24 /tmp/feed1.ffm

二. 实时流媒体的播放rtsp
1. 修改ffserver的配置ffserver.conf
  1. HTTPPort 8091
  2. RTSPPort 5454                     -->这儿添加了rtsp的port
  3. HTTPBindAddress 0.0.0.0
  4. MaxClients 10
  5. MaxBandwidth 50000
  6. <Feed feed1.ffm>
  7. file /tmp/feed1.ffm
  8. FileMaxSize 10M
  9. </Feed>

  10. <Stream feed1.mjpeg>
  11. Feed feed1.ffm
  12. Format mjpeg
  13. VideoSize 640x480
  14. VideoFrameRate 10
  15. VideoBitRate 2000
  16. VideoQMin 1
  17. VideoQMax 10
  18. </Stream>


  19. <Stream live.h264>                -->rtsp的配置
  20. Format rtp
  21. Feed feed1.ffm
  22. #VideoCodec libx264
  23. VideoFrameRate 24
  24. VideoBitRate 300
  25. #VideoSize 480x272
  26. VideoSize 640x480
  27. #AVPresetVideo default
  28. #AVPresetVideo superfast
  29. AVOptionVideo flags +global_header

  30. PixelFormat yuv420p
  31. AVOptionVideo me_range 4
  32. AVOptionVideo qdiff 4
  33. AVOptionVideo qmin 2
  34. AVOptionVideo qmax 51

  35. #MulticastAddress 224.124.0.1
  36. #MulticastPort 5000
  37. #MulticastTTL 1

  38.   
  39. NoAudio
  40. #AudioCodec libfaac
  41. #AudioBitRate 32
  42. #AudioChannels 2
  43. #AudioSampleRate 22050
  44. #AVOptionAudio flags +global_header
  45. </Stream>
2. 运行
  1. a.开启server
  2. cong@msi:/work/ffmpeg/out/bin$ ./ffserver --./ffserver.conf
  3. b.开启ffmpeg
  4. cong@msi:/work/ffmpeg/out/bin$ ./ffmpeg -v verbose -r 5 -s 640x480 -f video4linux2 -/dev/video0 http://localhost:8091/feed1.ffm
  5. c.本地用ffplay播放
  6. cong@msi:/work/ffmpeg/out/bin$ ./ffplay rtsp://localhost:5454/live.h264
如果没有问题,则ffplay就会播放出摄像头的影像。

三.顺序流媒体的播放
上面的RTSP是实时的,类似于网络视频聊天,而这个顺序流媒体即看网络电影
3.1 ffserver.conf
  1. <Stream test.mpg>         --> 这个针对于mpg格式 
  2. File /tmp/minzu.mpg       --> ffmpeg源码目录下的doc/ffserver.conf中对Format参数的说明:
  3. Format mpeg               --> mpeg: MPEG-1 multiplexed video and audio 
  4. </Stream> 

  5. <Stream test.avi>        -->这个针对于avi格式 
  6. File /tmp/minzu.avi
  7. Format avi               -->avi: AVI format (MPEG-4 video, MPEG audio sound)
  8. </Stream>                
完整的ffserver.conf如下:
  1. cong@msi:/work/ffmpeg/ffmpeg-2.7.2$ cat ffserver.conf 
  2. HTTPPort 8091
  3. HTTPBindAddress 0.0.0.0
  4. MaxClients 10
  5. MaxBandwidth 50000

  6. <Stream test.mpg> 
  7. File /tmp/minzu.mpg 
  8. Format mpeg
  9. </Stream> 

  10. <Stream test.avi> 
  11. File /tmp/minzu.avi
  12. Format avi
  13. </Stream>

3.2 视频文件的格式
  1. cong@msi:/work/ffmpeg/ffmpeg-2.7.2$ ./ffplay /tmp/minzu.mpg 
  2.     Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 640x360 [SAR 1:1 DAR 16:9], max. 2516 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
  3.     Stream #0:1[0x1c0]: Audio: mp2, 44100 Hz, stereo, s16p, 224 kb/s

  4. cong@msi:/work/ffmpeg/ffmpeg-2.7.2$ ./ffplay /tmp/minzu.avi
  5.     Stream #0:0: Video: mpeg4 (Advanced Simple Profile) (XVID / 0x44495658), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 1511 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc
  6.     Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16p, 224 kb/s
3.3 播放测试
cong@msi:/work/ffmpeg/ffmpeg-2.7.2$ ./ffplay http://192.168.4.11:8091/test.mpg
cong@msi:/work/ffmpeg/ffmpeg-2.7.2$ ./ffplay http://192.168.4.11:8091/test.avi
上面两个播放分别都可以看到视频并听到声音
注意: 
    ffserver.conf中的配置一定要与要播放的视频格式相对应


参考文章
How To : Stream A Webcam From The Raspberry Pi
http://sirlagz.net/2012/08/04/how-to-stream-a-webcam-from-the-raspberry-pi/
ffmpeg+ffserver搭建流媒体服务器
http://blog.chinaunix.net/uid-9688646-id-3399113.html
基于ffmpeg的流媒体服务器
http://www.cnblogs.com/lidabo/p/3701071.html
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值