通过udp传输h264 / aacplus媒体

时间:2011-11-04 00:03:18

标签: stream udp h.264 aac

有没有人有这方面的经验?

到目前为止,我已尝试使用ffmpeg(使用libx264和libaacplus)通过udp复用到mpegts,但是mpegts muxer显然已被破坏(从几个不同来源确认)。 我也试过vlc,但它只能编码AAC-LC而不能编码HE-AAC v2(aacplus)。

无论如何,我需要解决的问题是有几个不同的地理位置,覆盖着网络摄像头,我需要udp能够一直拥有传入流,而不用担心网络起伏(udp只会继续当网络再次启动时发送数据包)。所以,你们有没有使用h264和udp协议的aacplus使用流媒体直播媒体的经验,如果是的话,请你给我任何链接或指示如何实现它。

提前多多感谢。

1 个答案:

答案 0 :(得分:3)

我正在开发一种DVR系统,它必须定期从视频设备录制h264文件,同时提供本地预览以允许调整视频参数和摄像机视图。虽然我在这个领域根本不是专家,但我在UDP上传输h264相对成功,所以我会尝试分享没有的东西以及为我做了什么,这些都是基于 ffmpeg < / strong>作为服务器(在我的情况下没有音频)。

最初,我已将我的应用程序设置为同时将视频录制到文件并将其提供给.ffm文件,以便ffserver使用RTP / UDP对其进行流式传输以进行相机预览。该方法的问题恰恰在于当馈送ffmpeg进程停止更改视频文件时,预览将停止并且永远不会恢复,尽管后续的ffmpeg进程已经开始再次提供ffserver。随着RTP ffserver显然抱怨帧时间戳开始回到0而不是继续他们停止的地方。然后我意识到,如果我能够通过纯UDP发送h264数据包,我将获得与您描述的完全相同的效果,一旦下一个ffmpeg进程接管,预览就会恢复。

在尝试理解ffmpeg文档时,我也尝试使用mpegts格式,但这样做我在另一端的播放器上获取了MPEG2视频(这显示了一个多播地址,但它也适用于特定目标):< / p>

$ ffmpeg -y -f video4linux2 -i /dev/video2 -vcodec libx264 -preset ultrafast /mnt/hd/video.mp4 -an -f mpegts udp://224.124.0.1:5000
ffmpeg version N-35860-g62adc60, Copyright (c) 2000-2011 the FFmpeg developers
  built on Dec 16 2011 09:47:41 with gcc 4.5.3
  configuration: --prefix=/usr --libdir=/usr/lib --shlibdir=/usr/lib --mandir=/usr/man --disable-debug --enable-shared --disable-static --enab
le-pthreads --enable-libtheora --enable-libvorbis --enable-gpl --enable-version3 --enable-postproc --enable-swscale --enable-avfilter --enable
-libx264 --enable-libvpx --enable-librtmp --disable-indev='v4l,dv1394'
  libavutil    51. 32. 0 / 51. 32. 0
  libavcodec   53. 46. 0 / 53. 46. 0
  libavformat  53. 26. 0 / 53. 26. 0
  libavdevice  53.  4. 0 / 53.  4. 0
  libavfilter   2. 53. 0 /  2. 53. 0
  libswscale    2.  1. 0 /  2.  1. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[video4linux2,v4l2 @ 0x8a96b00] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video2':
  Duration: N/A, start: 1325538250.366878, bitrate: 27620 kb/s
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, 27620 kb/s, 29.97 tbr, 1000k tbn, 29.97 tbc
[buffer @ 0x8a9d8c0] w:320 h:240 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:
[buffer @ 0x8a9c860] w:320 h:240 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:
[libx264 @ 0x8a97780] using cpu capabilities: MMX2 Cache64
[libx264 @ 0x8a97780] profile Constrained Baseline, level 1.3
[libx264 @ 0x8a97780] 264 - core 120 - H.264/MPEG-4 AVC codec - Copyleft 2003-2011 - http://www.videolan.org/x264.html - options: cabac=0 ref=
1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 
fast_pskip=1 chroma_qp_offset=0 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=
0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
[mpegts @ 0x8a98100] muxrate VBR, pcr every 2 pkts, sdt every 200, pat/pmt every 40 pkts
Output #0, mp4, to '/mnt/hd/video.mp4':
  Metadata:
    encoder         : Lavf53.26.0
    Stream #0:0: Video: h264 (![0][0][0] / 0x0021), yuv420p, 320x240, q=-1--1, 30k tbn, 29.97 tbc
Output #1, mpegts, to 'udp://224.124.0.1:5000':
  Metadata:
    encoder         : Lavf53.26.0
    Stream #1:0: Video: mpeg2video, yuv420p, 320x240, q=2-31, 200 kb/s, 90k tbn, 29.97 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> libx264)
  Stream #0:0 -> #1:0 (rawvideo -> mpeg2video)
Press [q] to stop, [?] for help

在客户端PC上,我能够使用 ffplay 观看视频,而且确实采用MPEG2格式:

$ ffplay -f mpegts udp://224.124.0.1:5000
ffplay version N-35860-g62adc60, Copyright (c) 2003-2011 the FFmpeg developers
  built on Dec 16 2011 09:47:41 with gcc 4.5.3
  configuration: --prefix=/usr --libdir=/usr/lib --shlibdir=/usr/lib --mandir=/usr/man --disable-debug --enable-shared --disable-static --enable-pthreads --enable-libtheora --enable-libvorbis --enable-gpl --enable-version3 --enable-postproc --enable-swscale --enable-avfilter --enable-libx264 --enable-libvpx --enable-librtmp --disable-indev='v4l,dv1394'
  libavutil    51. 32. 0 / 51. 32. 0
  libavcodec   53. 46. 0 / 53. 46. 0
  libavformat  53. 26. 0 / 53. 26. 0
  libavdevice  53.  4. 0 / 53.  4. 0
  libavfilter   2. 53. 0 /  2. 53. 0
  libswscale    2.  1. 0 /  2.  1. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[mpegts @ 0x80f02e0] Unable to seek back to the start
[mpeg2video @ 0x8111a00] mpeg_decode_postinit() failure
    Last message repeated 6 times                                                                                                             
[mpegts @ 0x80f02e0] max_analyze_duration 5000000 reached at 5005000
[mpegts @ 0x80f02e0] Estimating duration from bitrate, this may be inaccurate                                                                 
Input #0, mpegts, from 'udp://224.124.0.1:5000':                                                                                              
  Duration: N/A, start: 255.420433, bitrate: 104857 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 104857 kb/s, 30.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc

VLC 也可以播放流,但只有在指定了开关 - demux ffmpeg 之后(感谢this):

vlc -vv --demux ffmpeg udp://@224.124.0.1:5000

由于我还希望预览在h264中,但我已经对它进行了编码,我尝试使用复制编解码器进行UDP流式传输,但ffmpeg失败并出现了段错误(包含的版本供参考):

ffmpeg -y -f video4linux2 -i /dev/video2 -vcodec libx264 -preset ultrafast /mnt/hd/video.mp4 -an -vcodec cop
y -f h264 udp://224.124.0.1:5000
ffmpeg version N-35860-g62adc60, Copyright (c) 2000-2011 the FFmpeg developers
  built on Dec 16 2011 09:47:41 with gcc 4.5.3
  configuration: --prefix=/usr --libdir=/usr/lib --shlibdir=/usr/lib --mandir=/usr/man --disable-debug --enable-shared --disable-static --enab
le-pthreads --enable-libtheora --enable-libvorbis --enable-gpl --enable-version3 --enable-postproc --enable-swscale --enable-avfilter --enable
-libx264 --enable-libvpx --enable-librtmp --disable-indev='v4l,dv1394'
  libavutil    51. 32. 0 / 51. 32. 0
  libavcodec   53. 46. 0 / 53. 46. 0
  libavformat  53. 26. 0 / 53. 26. 0
  libavdevice  53.  4. 0 / 53.  4. 0
  libavfilter   2. 53. 0 /  2. 53. 0
  libswscale    2.  1. 0 /  2.  1. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[video4linux2,v4l2 @ 0x92c7b00] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video2':                                                                                              
  Duration: N/A, start: 1325539132.411691, bitrate: 27620 kb/s
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, 27620 kb/s, 29.97 tbr, 1000k tbn, 29.97 tbc
[buffer @ 0x92ce860] w:320 h:240 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:
[libx264 @ 0x92c8780] using cpu capabilities: MMX2 Cache64
[libx264 @ 0x92c8780] profile Constrained Baseline, level 1.3
[libx264 @ 0x92c8780] 264 - core 120 - H.264/MPEG-4 AVC codec - Copyleft 2003-2011 - http://www.videolan.org/x264.html - options: cabac=0 ref=
1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 
fast_pskip=1 chroma_qp_offset=0 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=
0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to '/mnt/hd/video.mp4':
  Metadata:
    encoder         : Lavf53.26.0
    Stream #0:0: Video: h264 (![0][0][0] / 0x0021), yuv420p, 320x240, q=-1--1, 30k tbn, 29.97 tbc
Output #1, h264, to 'udp://224.124.0.1:5000':
  Metadata:
    encoder         : Lavf53.26.0
    Stream #1:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, q=2-31, 27620 kb/s, 90k tbn, 29.97 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> libx264)
  Stream #0:0 -> #1:0 (copy)
Press [q] to stop, [?] for help
Segmentation fault

虽然不够理想,但为UDP流媒体部分指定格式h264会导致第二次并发h264转换,但它有效:

$ ffmpeg -y -f video4linux2 -i /dev/video2 -vcodec libx264 -preset ultrafast /mnt/hd/video.mp4 -an -f h264 -preset ultrafast udp://224.124.0.1:5000
ffmpeg version N-35860-g62adc60, Copyright (c) 2000-2011 the FFmpeg developers
  built on Dec 16 2011 09:47:41 with gcc 4.5.3
  configuration: --prefix=/usr --libdir=/usr/lib --shlibdir=/usr/lib --mandir=/usr/man --disable-debug --enable-shared --disable-static --enab
le-pthreads --enable-libtheora --enable-libvorbis --enable-gpl --enable-version3 --enable-postproc --enable-swscale --enable-avfilter --enable
-libx264 --enable-libvpx --enable-librtmp --disable-indev='v4l,dv1394'
  libavutil    51. 32. 0 / 51. 32. 0
  libavcodec   53. 46. 0 / 53. 46. 0
  libavformat  53. 26. 0 / 53. 26. 0
  libavdevice  53.  4. 0 / 53.  4. 0
  libavfilter   2. 53. 0 /  2. 53. 0
  libswscale    2.  1. 0 /  2.  1. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[video4linux2,v4l2 @ 0x913ab00] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video2':                                                                                              
  Duration: N/A, start: 1325539689.729735, bitrate: 27620 kb/s
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, 27620 kb/s, 29.97 tbr, 1000k tbn, 29.97 tbc
[buffer @ 0x9141840] w:320 h:240 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:
[buffer @ 0x913e480] w:320 h:240 pixfmt:yuv420p tb:1/1000000 sar:0/1 sws_param:
[libx264 @ 0x913b780] using cpu capabilities: MMX2 Cache64
[libx264 @ 0x913b780] profile Constrained Baseline, level 1.3
[libx264 @ 0x913b780] 264 - core 120 - H.264/MPEG-4 AVC codec - Copyleft 2003-2011 - http://www.videolan.org/x264.html - options: cabac=0 ref=
1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 
fast_pskip=1 chroma_qp_offset=0 threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=
0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
[libx264 @ 0x913c820] using cpu capabilities: MMX2 Cache64
[libx264 @ 0x913c820] profile Constrained Baseline, level 1.3
Output #0, mp4, to '/mnt/hd/video.mp4':
  Metadata:
    encoder         : Lavf53.26.0
    Stream #0:0: Video: h264 (![0][0][0] / 0x0021), yuv420p, 320x240, q=-1--1, 30k tbn, 29.97 tbc
Output #1, h264, to 'udp://224.124.0.1:5000':
  Metadata:
    encoder         : Lavf53.26.0
    Stream #1:0: Video: h264, yuv420p, 320x240, q=-1--1, 90k tbn, 29.97 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> libx264)
  Stream #0:0 -> #1:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help

在客户端 ffplay 反复抱怨一些丢失的信息,但几秒钟后终于出现了视频,这很好但有点不稳定:

$ ffplay -f h264 udp://224.124.0.1:5000
[h264 @ 0xa0be740] non-existing PPS referenced               
[h264 @ 0xa0be740] non-existing PPS 0 referenced                              
[h264 @ 0xa0be740] decode_slice_header error
[h264 @ 0xa0be740] no frame!                             

(...)

[h264 @ 0xa0be740] non-existing PPS referenced               
[h264 @ 0xa0be740] non-existing PPS 0 referenced                              
[h264 @ 0xa0be740] decode_slice_header error
[h264 @ 0xa0be740] no frame!
[h264 @ 0xa0e72e0] max_analyze_duration 5000000 reached at 5013967                  
[h264 @ 0xa0e72e0] Estimating duration from bitrate, this may be inaccurate
Input #0, h264, from 'udp://224.124.0.1:5000':                                              
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (Constrained Baseline), yuv420p, 320x240, 47.27 fps, 29.97 tbr, 1200k tbn, 59.94 tbc
[h264 @ 0xa0be740] Missing reference picture
[h264 @ 0xa0be740] decode_slice_header error            
[h264 @ 0xa0be740] concealing 300 DC, 300 AC, 300 MV errors
[h264 @ 0xa0be740] Missing reference picture  0KB sq=    0B f=0/0   0/0   
[h264 @ 0xa0be740] decode_slice_header error
[h264 @ 0xa0be740] mmco: unref short failure
[h264 @ 0xa0be740] concealing 300 DC, 300 AC, 300 MV errors
  10.78 A-V:  0.000 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0 

然后我尝试使用相同的'' - drux ffmpeg''切换 VLC ;它还抱怨SPS / PPS(不知道它到底是什么)但最终它播放的视频非常流畅:

$ vlc -v --demux ffmpeg udp/h264://@224.124.0.1:5000 
VLC media player 1.1.12 The Luggage (revision exported)
Blocked: call to unsetenv("DBUS_ACTIVATION_ADDRESS")
Blocked: call to unsetenv("DBUS_ACTIVATION_BUS_TYPE")
[0x943346c] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
Blocked: call to setlocale(6, "")
Blocked: call to setlocale(6, "")
[0x94cde8c] qt4 interface error: Unable to load extensions module
[0x96720e4] h264 demux error: this doesn't look like a H264 ES stream, continuing anyway
[0x963989c] access_udp access warning: unimplemented query in control
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x967328c] packetizer_h264 demux packetizer warning: waiting for SPS/PPS
[0x94be644] main input error: Invalid PCR value in ES_OUT_SET_(GROUP_)PCR !
[0xb2a325f4] avcodec decoder warning: disabling direct rendering
[0x96dfac4] main video output warning: vlc_object_find_name(postproc) is not safe!
[0x94c2474] signals interface warning: signal 17 overridden (0xb6f31030)
[0x94c2474] signals interface warning:  /usr/lib/qt/lib/libQtCore.so.4(?)[(nil)]
[0x96dfac4] main video output warning: late picture skipped (32703 > -4)

我仍在试图找出遗漏的东西,但结果令人满意。在VLC播放之后,我可以停止播放ffmpeg,当我在另一端重新启动ffmpeg时播放暂停并立即恢复。

希望这可以提供任何帮助,如果您有关于使用h264作为所选格式代替mpegts的任何其他信息,请告诉我 - 我怀疑这个缺少的SPS / PPS信息可能与它有关。