我正在開發一種類似DVR的系統,它必須定期從視頻設備記錄h264文件,同時提供本地預覽以允許調整視頻參數和攝像頭視圖。雖然我在這個領域並不擅長,但我相對成功地通過UDP流式傳輸h264,所以我會嘗試分享什麼沒有做什麼以及爲我做什麼,這些都是基於ffmpeg作爲服務器(在我的情況下沒有音頻)。
最初,我設置了我的應用程序,將視頻同時錄製到一個文件中,並將其提供給.fm文件以供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
雖然不太理想,指定格式H264的UDP流部分導致第二次併發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時立即恢復。
希望這能有所幫助,請讓我知道,如果你有在地方MPEGTS使用H264作爲選擇格式的任何額外的信息 - 我懷疑這失蹤SPS/PPS信息可能有一些用它做。