ffmpeg video recording freezes after "Invalid data found when processing input"
I am trying to record my old VHS tapes using a USB video capture device and the following command:
ffmpeg -f alsa -ac 2 -i default -f v4l2 -standard pal -i /dev/video0 -c:v libx264 -qp 0 -preset fast -strict -2 zzz.mp4
but sometimes, unexpectedly during the recording, ffmpeg freezes the recording of video and remains on the last frame (audio continues to be recorded). The error message is something like:
[video4linux2,v4l2 @ 0x23e51a0] Dequeued v4l2 buffer contains 414720 bytes, but 829440 were expected. Flags: 0x00002005.
/dev/video0: Invalid data found when processing input
I found almost zero information on the internet about this. I found the problem described as a feature request here: https://trac.ffmpeg.org/ticket/4795
Also, I had some trouble with a warning message before this error, which doesn't seem to be related and doesn't seem to affect the video recording, but I also wonder what it means:
[video4linux2,v4l2 @ 0x23e51a0] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[alsa @ 0x23cc360] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
Using trial-and-error, it seems that the warnings disappear when I set
-f v4l2 -thread_queue_size 32
and -f alsa -thread_queue_size 2048
. I don't know why.
When I raise the thread queue size for the video input, for example to 512 I have no warning but I have dropped frames.
I wish I can use ffmpeg for my recordings. VLC has no problem recording, but I am also having trouble with the parameters, I haven't studied them enough yet. If someone can suggest an alternative command for vlc, to record lossless, I will switch to vlc...
The full output of ffmpeg is:
$ ffmpeg -f alsa -ac 2 -i default -f v4l2 -standard pal -i /dev/video0 -c:v libx264 -qp 0 -preset fast -strict -2 zzz.mp4
ffmpeg version 2.7.6-0ubuntu0.15.10.1 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.2.1 (Ubuntu 5.2.1-22ubuntu2) 20151010
configuration: --prefix=/usr --extra-version=0ubuntu0.15.10.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --enable-shared --disable-stripping --enable-avresample --enable-avisynth --enable-frei0r --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-openal --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libxvid --enable-libzvbi --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-libssh --enable-libsoxr --enable-libx264 --enable-libopencv --enable-libx265
libavutil 54. 27.100 / 54. 27.100
libavcodec 56. 41.100 / 56. 41.100
libavformat 56. 36.100 / 56. 36.100
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 16.101 / 5. 16.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.100 / 1. 2.100
libpostproc 53. 3.100 / 53. 3.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, alsa, from 'default':
Duration: N/A, start: 1457422230.571465, bitrate: 1536 kb/s
Stream #0:0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
Input #1, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 4454.267377, bitrate: 165888 kb/s
Stream #1:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 720x576, 165888 kb/s, 25 fps, 25 tbr, 1000k tbn, 1000k tbc
File 'zzz.mp4' already exists. Overwrite ? [y/N] y
No pixel format specified, yuv422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x231cd00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 LZCNT
[libx264 @ 0x231cd00] profile High 4:4:4 Predictive, level 3.0, 4:2:2 8-bit
[libx264 @ 0x231cd00] 264 - core 146 r2555 0c21480 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=2 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=6 psy=0 mixed_ref=1 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=0 chroma_qp_offset=0 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=1 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc=cqp mbtree=0 qp=0
Output #0, mp4, to 'zzz.mp4':
Metadata:
encoder : Lavf56.36.100
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv422p, 720x576, q=-1--1, 25 fps, 12800 tbn, 25 tbc
Metadata:
encoder : Lavc56.41.100 libx264
Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc56.41.100 aac
Stream mapping:
Stream #1:0 -> #0:0 (rawvideo (native) -> h264 (libx264))
Stream #0:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[video4linux2,v4l2 @ 0x231a180] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[alsa @ 0x2301360] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[video4linux2,v4l2 @ 0x231a180] Dequeued v4l2 buffer contains 414720 bytes, but 829440 were expected. Flags: 0x00002005.
/dev/video0: Invalid data found when processing input
frame= 1962 fps= 17 q=-1.0 Lsize= 409086kB time=00:01:53.53 bitrate=29516.7kbits/s
video:407234kB audio:1777kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.018333%
[libx264 @ 0x231cd00] frame I:346 Avg QP: 0.00 size:186290
[libx264 @ 0x231cd00] frame P:1616 Avg QP: 0.00 size:218162
[libx264 @ 0x231cd00] mb I I16..4: 92.3% 0.0% 7.7%
[libx264 @ 0x231cd00] mb P I16..4: 76.1% 0.0% 22.8% P16..4: 0.2% 0.5% 0.4% 0.0% 0.0% skip: 0.0%
[libx264 @ 0x231cd00] 8x8 transform intra:0.0% inter:76.8%
[libx264 @ 0x231cd00] coded y,uvDC,uvAC intra: 100.0% 100.0% 100.0% inter: 100.0% 100.0% 100.0%
[libx264 @ 0x231cd00] i16 v,h,dc,p: 0% 100% 0% 0%
[libx264 @ 0x231cd00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 10% 78% 3% 1% 2% 3% 1% 3% 1%
[libx264 @ 0x231cd00] i8c dc,h,v,p: 1% 98% 0% 0%
[libx264 @ 0x231cd00] Weighted P-Frames: Y:31.7% UV:20.6%
[libx264 @ 0x231cd00] ref P L0: 52.8% 47.2%
[libx264 @ 0x231cd00] kb/s:42508.35
Solution 1:
This is resolved in FFmpeg v4.3 by https://github.com/FFmpeg/FFmpeg/commit/b761ae072a169eb183abe0785a258b9787e267d3 which makes this error a warning and seems to skip the frame instead.
v4.3 was released on June 15th.
Solution 2:
I couldn't find any solution to this ffmpeg problem, and I recently found this article saying:
As it seems, the stk1160 device sends these broken frames through to the v4l2 device and once they reach ffmpeg, it chokes and reports invalid frames. Although that is part of the “honest” quality of ffmpeg, I consider it a bug here as it exits the transcoding process and makes ffmpeg unsuitable for the task at hand.
So, I started to use mencoder instead. This blog helped me to start using mencoder: http://easycap.blogspot.com/p/command-line-tv.html
I compiled MPlayer (which contains mencoder) using the source code given at http://www.mplayerhq.hu
Solution 3:
This is still a bug in ffmpeg 3.3. Even though ffplay correctly recovers after this error (which for me happens on scene changes / fast forwarding), ffmpeg just stops processing frames. I had the same problem with this device, and I couldn't get mencoder to use the exact parameters (esp. regarding deinterlacing) I wanted, so I used a combination of mencoder (raw AVI to stdout) and ffmpeg (yadif deinterlace, map audio, encode x264 and mux to mkv) which works great.
You could of course store the raw AVI data directly, but with ~70GByte/h I didn't have enough space for that.
(Partially taken from here)
mencoder -endpos 1:40:00 tv:// -tv device=/dev/video0:input=4:norm=PAL:width=720:height=576:alsa:adevice=hw.3:forceaudio:buffersize=128 -oac pcm -ovc copy -o - -really-quiet | ffmpeg -i - -vf yadif=mode=send_field:parity=tff -map_channel 0.1.1 -c:a flac -c:v libx264 -preset veryfast -crf 16 011.mkv