【问题标题】:Stream h264/aacplus media over udp通过 udp 流式传输 h264/aacplus 媒体
【发布时间】:2011-11-04 00:03:17
【问题描述】:

有人有这方面的经验吗?

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

无论如何,我需要解决的问题是有几个不同的地理位置,覆盖着网络摄像头,我需要 udp 能够一直有传入的流,而不用担心网络起伏(udp 将继续当网络再次启动时发送数据包)。那么,你们中的任何人是否有任何使用 h264 和 aacplus over udp 协议的流式直播媒体的经验,如果有,请给我任何链接或指导如何完成它。

非常感谢。

【问题讨论】:

    标签: stream udp h.264 aac


    【解决方案1】:

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

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

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

    $ 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 
    

    然后我尝试 VLC 使用相同的 ''--demux ffmpeg'' 开关;它还抱怨 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 信息可能与此有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 2012-04-10
      相关资源
      最近更新 更多