I have ffmpeg/ffserver running in the following setup:
10.1.2.2 captures from video capture cards (16 channels) and streams them to an ffserver as mpeg1video
10.1.1.1/10.1.2.1 (two NICs) hosts the ffserver and some processes that convert the mpeg1video streams into webm streams along with zoneminder (feeding off the original ffmpeg streams). 10.1.2.2 and 10.1.2.1 are connected via gigabit crossover cable. The /tmp director is mounted as 128MB of RAM via tmpfs.
The mpeg1video stream is always delayed about 0.1-0.2 seconds behind the webm stream, this is perfectly acceptable to me (but I mention it because it might be useful to know for troubleshooting purposes).
When restarted all streams are delayed by 3-5 seconds (I would like to cut this delay down - I can view any individual stream directly from the video device live, so it might be that I'm getting these in the wrong format, except for the compounding delay). When run overnight the streams still appear "live" (fluid motion) in both mpeg1video and webm formats, but are clearly delayed by 4-5 HOURS (as evident by the fact the lights appeared to my amazement to be off when I came in this morning, and kept the screens up watching them until 4-5 hours after people started to come in that morning the lights came on [but not all together, they appear to get out of sync from one another as well]).
These are the processes I'm using:
On 10.1.2.2:
ffmpeg -f video4linux2 -standard ntsc -i /dev/video0 http://10.1.2.1:8090/0.ffm
On 10.1.2.1/10.1.1.15:
ffserver -f /etc/ffserver.conf
ffmpeg -f mpegvideo -i http://localhost:8090/0.mpg -vcodec libvpx http://localhost:8090/0_webm.ffm
And an excerpt from /etc/ffserver.conf on 10.1.2.1/10.1.1.15:
Port 8090
MaxHTTPConnections 2000
MAXClients 100
MaxBandwidth 1000000
CustomLog /var/log/ffserver
NoDaemon
<Feed 0.ffm>
File /tmp/0.ffm
FileMaxSize 1M
ACL allow 10.1.2.2
</Feed>
<Feed 0_webm.ffm>
File /tmp/0_webm.ffm
FileMaxSize 1M
ACL allow localhost
</Feed>
<Stream 0.mpg>
Feed 0.ffm
Format mpeg1video
NoAudio
VideoFrameRate 29.97
VideoBitRate 512K
VideoSize 320x240
VideoBufferSize 40
VideoGopSize 12
#VideoIntraOnly
</Stream>
<Stream 0.webm>
Feed 0_webm.ffm
Format webm
NoAudio
VideoCodec libvpx
VideoSize 320x240
VideoFrameRate 29.97
AVOptionVideo flags +global_header
AVOptionVideo cpu-used 0
AVOptionVideo quality good
PreRoll 0
StartSendOnKey
VideoGopSize 12
VideoBitRate 256
</Stream>
Now, 1MB is far too small to hold 4-5 hours of streaming video at 29.97 FPS and 320x240 resolution in yuv420p color - so my questions is, what is going on here and how do I fix it? To me is seems like something taking place within the initial ffmpeg process on 10.1.2.2, but I can't figure out what, does ffmpeg use some buffer of it's own or is there perhaps a command I can use to force it to drop any excessive frames when pulling from the video card - that is just to take a snapshot as quickly as it can and send from a single-image buffer to ffserver to ensure what is being sent is always live?
My goal here is a live stream feeding into zoneminder and a live stream output in webm format for each of the 16 channels (0-15) - so I am open to any solution but would prefer something based around ffmpeg/ffserver because they seem to be the gold-standard within Linux for tasks like this.
The initial delay of 3-5 seconds and the 4-5 hour delay may be different issues as well. When I kill 10.1.2.2 via "sudo reboot" it takes 3-5 seconds for the individual mpg/webm streams to cut out coming from 10.1.1.15, so it seems there is some buffering taking place within ffserver that goes beyond simply relaying the streams.
Edit: tried reducing all the feed FileMaxSize's to 30K and the 3-5 second delay is still present in all the streams. The same delay exists whether there are 16 or just 1 ffmpeg processes on 10.1.2.2.
Log outputs:
Server:
sudo ffserver -f /etc/ffserver.conf -d
ffserver version git-2013-03-15-cd5f50a Copyright (c) 2000-2013 the FFmpeg developers
built on Mar 15 2013 14:03:16 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --enable-gpl --enable-libass --enable-libfaac --enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3
libavutil 52. 19.100 / 52. 19.100
libavcodec 55. 0.100 / 55. 0.100
libavformat 55. 0.100 / 55. 0.100
libavdevice 55. 0.100 / 55. 0.100
libavfilter 3. 45.104 / 3. 45.104
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
Thu Mar 21 14:08:53 2013 FFserver started.
Thu Mar 21 14:08:57 2013 10.1.3.2 - - New connection: GET /15.ffm
Thu Mar 21 14:08:57 2013 10.1.3.2 - - [GET] "/15.ffm HTTP/1.1" 200 4175
Thu Mar 21 14:08:57 2013 10.1.3.2 - - New connection: POST /15.ffm
Thu Mar 21 14:09:03 2013 127.0.0.1 - - New connection: GET /15.mpg
Thu Mar 21 14:09:06 2013 127.0.0.1 - - New connection: GET /15_webm.ffm
Thu Mar 21 14:09:06 2013 127.0.0.1 - - [GET] "/15_webm.ffm HTTP/1.1" 200 4175
sudo ffmpeg -f video4linux2 -standard ntsc -i /dev/video7 http://10.1.3.1:8090/15.ffm
video device --> mpg
ffmpeg version git-2013-03-21-4331484 Copyright (c) 2000-2013 the FFmpeg developers
built on Mar 20 2013 20:44:06 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --enable-gpl --enable-libass --enable-libfaac --enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3
libavutil 52. 22.100 / 52. 22.100
libavcodec 55. 1.100 / 55. 1.100
libavformat 55. 0.100 / 55. 0.100
libavdevice 55. 0.100 / 55. 0.100
libavfilter 3. 48.100 / 3. 48.100
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
[video4linux2,v4l2 @ 0xa1d34c0] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video7':
Duration: N/A, start: 1363889339.048888, bitrate: 27620 kb/s
Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, 27620 kb/s, 29.97 fps, 29.97 tbr, 1000k tbn, 1000k tbc
Output #0, ffm, to 'http://10.1.3.1:8090/15.ffm':
Metadata:
creation_time : now
encoder : Lavf55.0.100
Stream #0:0: Video: mpeg1video, yuv420p, 320x240, q=2-31, 512 kb/s, 1000k tbn, 29.97 tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo -> mpeg1video)
Press [q] to stop, [?] for help
frame= 3552 fps= 30 q=5.5 size= 7644kB time=00:01:58.45 bitrate= 528.7kbits/s
ffmpeg -f mpegvideo -i http://localhost:8090/15.mpg -vcodec libvpx http://localhost:8090/15_webm.ffm
mpg --> webm
ffmpeg version git-2013-03-15-cd5f50a Copyright (c) 2000-2013 the FFmpeg developers
built on Mar 15 2013 14:03:16 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --enable-gpl --enable-libass --enable-libfaac --enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3
libavutil 52. 19.100 / 52. 19.100
libavcodec 55. 0.100 / 55. 0.100
libavformat 55. 0.100 / 55. 0.100
libavdevice 55. 0.100 / 55. 0.100
libavfilter 3. 45.104 / 3. 45.104
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
[mpeg1video @ 0x3901000] 0x0 is invalid
Last message repeated 2 times
[mpegvideo @ 0x38f4f80] max_analyze_duration 5000000 reached at 5011633 microseconds
[mpegvideo @ 0x38f4f80] Estimating duration from bitrate, this may be inaccurate
Input #0, mpegvideo, from 'http://localhost:8090/15.mpg':
Duration: N/A, bitrate: 1024 kb/s
Stream #0:0: Video: mpeg1video, yuv420p, 320x240 [SAR 1:1 DAR 4:3], 1024 kb/s, 29.92 fps, 29.97 tbr, 1200k tbn, 29.97 tbc
[libvpx @ 0x39046a0] v1.2.0
Output #0, ffm, to 'http://localhost:8090/15_webm.ffm':
Metadata:
creation_time : now
encoder : Lavf55.0.100
Stream #0:0: Video: vp8, yuv420p, 320x240 [SAR 1:1 DAR 4:3], q=1-31, 128 kb/s, 1000k tbn, 29.97 tbc
Stream mapping:
Stream #0:0 -> #0:0 (mpeg1video -> libvpx)
Press [q] to stop, [?] for help
frame= 4845 fps= 31 q=0.0 size= 4192kB time=00:02:41.66 bitrate= 212.4kbits/s dup=4 drop=0
Adding more output per request:
$v4l
No command 'v4l' found, did you mean:
Command 'vl' from package 'atfs' (universe)
Command 'dv4l' from package 'dv4l' (universe)
v4l: command not found
$v4l2
No command 'v4l2' found, did you mean:
Command 'qv4l2' from package 'qv4l2' (universe)
v4l2: command not found
$ sudo ffprobe -show_streams -show_format -f video4linux2 /dev/video0
ffprobe version git-2013-03-16-c9a51c2 Copyright (c) 2007-2013 the FFmpeg developers
built on Mar 16 2013 18:48:40 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --enable-gpl --enable-libass --enable-libfaac --enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libspeex --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-version3
libavutil 52. 19.100 / 52. 19.100
libavcodec 55. 0.100 / 55. 0.100
libavformat 55. 0.100 / 55. 0.100
libavdevice 55. 0.100 / 55. 0.100
libavfilter 3. 47.100 / 3. 47.100
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
[video4linux2,v4l2 @ 0xb0a0540] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 1364261173.317100, bitrate: 27620 kb/s
Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 320x240, 27620 kb/s, 29.97 fps, 29.97 tbr, 1000k tbn, 1000k tbc
[STREAM]
index=0
codec_name=rawvideo
codec_long_name=raw video
profile=unknown
codec_type=video
codec_time_base=1/1000000
codec_tag_string=I420
codec_tag=0x30323449
width=320
height=240
has_b_frames=0
sample_aspect_ratio=0:1
display_aspect_ratio=0:1
pix_fmt=yuv420p
level=-99
timecode=N/A
id=N/A
r_frame_rate=30000/1001
avg_frame_rate=30000/1001
time_base=1/1000000
start_pts=1364261173317100
start_time=1364261173.317100
duration_ts=N/A
duration=N/A
bit_rate=27620379
nb_frames=N/A
nb_read_frames=N/A
nb_read_packets=N/A
DISPOSITION:default=0
DISPOSITION:dub=0
DISPOSITION:original=0
DISPOSITION:comment=0
DISPOSITION:lyrics=0
DISPOSITION:karaoke=0
DISPOSITION:forced=0
DISPOSITION:hearing_impaired=0
DISPOSITION:visual_impaired=0
DISPOSITION:clean_effects=0
DISPOSITION:attached_pic=0
[/STREAM]
[FORMAT]
filename=/dev/video0
nb_streams=1
format_name=video4linux2,v4l2
format_long_name=Video4Linux2 device grab
start_time=1364261173.317100
duration=N/A
size=N/A
bit_rate=27620379
[/FORMAT]
Video capture cards are both bt878a 8-channel using the bttv driver with a /etc/modprobe.d/bttv.conf setup as follows per the manufacturer's instructions on each of the two machines containing the cards:
options i2c-algo-bit_test=1
options bttv gbuffers=16 card=102,102,102,102,102,102,102,102 radio=0,0,0,0,0,0,0,0 tuner=4,4,4,4,4,4,4,4 chroma_agc=1 combfilter=2 full_luma_range=1 coring=1 autoload=0
On the local machines using:
ffplay -f video4linux2 -standard ntsc -i /dev/video0
Works in real-time and doesn't appear to accrue latency no matter how long it runs.
Source: (StackOverflow)
I can use ffmpeg to make screen casts:
ffmpeg -f x11grab -s 1280x800 -i :0.0 -c:v libx264 -framerate 30 -r 30 -crf 18 out.mkv
However the output comes out to be too fast paced. It also happens with GTK RecordMyDesktop
if I enable the encode on the fly. So, the questions is how to get a normal video pace. Also in order to capture the sound with ffmpeg what option should be used?
FFmpeg Output:
ffmpeg -f x11grab -s 1280x800 -r 30 -i :0.0 -c:v libx264 -framerate 30 -r 30 -crf 18 out.mkv
ffmpeg version N-35162-g87244c8 Copyright (c) 2000-2012 the FFmpeg developers
built on Oct 7 2012 15:56:19 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
configuration: --enable-gpl --enable-libfaac --enable-libfdk-aac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-x11grab --enable-libx264 --enable-nonfree --enable-version3
libavutil 51. 73.102 / 51. 73.102
libavcodec 54. 64.100 / 54. 64.100
libavformat 54. 29.105 / 54. 29.105
libavdevice 54. 3.100 / 54. 3.100
libavfilter 3. 19.102 / 3. 19.102
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 16.100 / 0. 16.100
libpostproc 52. 1.100 / 52. 1.100
[x11grab @ 0xab896a0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1280 height: 800
[x11grab @ 0xab896a0] shared memory extension found
[x11grab @ 0xab896a0] Estimating duration from bitrate, this may be inaccurate
Input #0, x11grab, from ':0.0':
Duration: N/A, start: 1350136942.608988, bitrate: 983040 kb/s
Stream #0:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1280x800, 983040 kb/s, 30 tbr, 1000k tbn, 30 tbc
[libx264 @ 0xab87320] using cpu capabilities: MMX2 SSE2Fast SSSE3 Cache64 SlowCTZ SlowAtom
[libx264 @ 0xab87320] profile High 4:4:4 Predictive, level 3.2, 4:4:4 8-bit
[libx264 @ 0xab87320] 264 - core 128 r2 198a7ea - H.264/MPEG-4 AVC codec - Copyleft 2003-2012 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=18.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, matroska, to 'out.mkv':
Metadata:
encoder : Lavf54.29.105
Stream #0:0: Video: h264, yuv444p, 1280x800, q=-1--1, 1k tbn, 30 tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help
frame= 10 fps=0.0 q=0.0 size= 1kB time=00:00:00.00 bitrate= 0.0kbits/sframe= 19 fps= 17 q=0.0 size= 1kB time=00:00:00.00 bitrate= 0.0kbits/sframe= 28 fps= 17 q=0.0 size= 1kB time=00:00:00.00 bitrate= 0.0kbits/sframe= 37 fps= 17 q=0.0 size= 1kB time=00:00:00.00 bitrate= 0.0kbits/sframe= 45 fps= 16 q=0.0 size= 1kB time=00:00:00.00 bitrate= 0.0kbits/sframe= 47 fps= 14 q=0.0 size= 1kB time=00:00:00.00 bitrate= 0.0kbits/sframe= 52 fps= 13 q=24.0 size= 257kB time=00:00:00.00 bitrate=2101632.0kbiframe= 55 fps= 12 q=24.0 size= 257kB time=00:00:00.10 bitrate=20808.2kbitsframe= 59 fps= 11 q=24.0 size= 289kB time=00:00:00.23 bitrate=10145.0kbitsframe= 64 fps= 11 q=24.0 size= 289kB time=00:00:00.40 bitrate=5894.7kbits/frame= 70 fps= 11 q=24.0 size= 289kB time=00:00:00.60 bitrate=3933.1kbits/frame= 72 fps= 10 q=24.0 size= 289kB time=00:00:00.66 bitrate=3549.2kbits/frame= 77 fps=9.8 q=24.0 size= 289kB time=00:00:00.83 bitrate=2837.7kbits/frame= 80 fps=9.6 q=24.0 size= 289kB time=00:00:00.93 bitrate=2533.5kbits/frame= 85 fps=9.3 q=24.0 size= 289kB time=00:00:01.10 bitrate=2146.9kbits/frame= 89 fps=9.3 q=24.0 size= 289kB time=00:00:01.23 bitrate=1917.1kbits/frame= 92 fps=9.1 q=24.0 size= 289kB time=00:00:01.33 bitrate=1773.3kbits/frame= 96 fps=9.0 q=24.0 size= 289kB time=00:00:01.46 bitrate=1612.4kbits/frame= 99 fps=8.8 q=24.0 size= 321kB time=00:00:01.56 bitrate=1676.8kbits/frame= 104 fps=8.7 q=24.0 size= 321kB time=00:00:01.73 bitrate=1515.2kbits/frame= 109 fps=5.3 q=24.0 Lsize= 1093kB time=00:00:03.56 bitrate=2511.5kbits/s
video:1092kB audio:0kB subtitle:0 global headers:0kB muxing overhead 0.120198%
[libx264 @ 0xab87320] frame I:3 Avg QP:18.93 size:142610
[libx264 @ 0xab87320] frame P:43 Avg QP:20.79 size: 15751
[libx264 @ 0xab87320] frame B:63 Avg QP:23.75 size: 195
[libx264 @ 0xab87320] consecutive B-frames: 21.1% 1.8% 11.0% 66.1%
[libx264 @ 0xab87320] mb I I16..4: 50.0% 21.1% 28.9%
[libx264 @ 0xab87320] mb P I16..4: 6.1% 0.9% 3.2% P16..4: 5.5% 1.2% 0.6% 0.0% 0.0% skip:82.5%
[libx264 @ 0xab87320] mb B I16..4: 0.4% 0.1% 0.0% B16..8: 2.9% 0.1% 0.0% direct: 0.0% skip:96.5% L0:40.7% L1:57.0% BI: 2.3%
[libx264 @ 0xab87320] 8x8 transform intra:14.5% inter:46.1%
[libx264 @ 0xab87320] coded y,u,v intra: 33.5% 24.1% 25.4% inter: 0.9% 0.4% 0.4%
[libx264 @ 0xab87320] i16 v,h,dc,p: 70% 26% 1% 3%
[libx264 @ 0xab87320] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 11% 21% 30% 5% 7% 5% 7% 4% 10%
[libx264 @ 0xab87320] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 35% 12% 2% 4% 3% 4% 3% 5%
[libx264 @ 0xab87320] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0xab87320] ref P L0: 57.0% 5.6% 26.8% 10.6%
[libx264 @ 0xab87320] ref B L0: 69.4% 22.6% 8.0%
[libx264 @ 0xab87320] ref B L1: 93.7% 6.3%
[libx264 @ 0xab87320] kb/s:2460.40
Source: (StackOverflow)