Problems capturing desktop video encoded by h264 with ffmpeg

I cannot get ffmpeg to capture a video of X11 desktop, encoded by h264.

I can capture the desktop without specifying an encoder, with ffmpeg -y -f x11grab -s wuxga -r 5 -i :1 output.avi -loglevel repeat+level+debug:

[info] ffmpeg started on 2019-09-16 at 10:59:13
Report written to "ffmpeg-20190916-105913.log"
Command line:
ffmpeg -y -f x11grab -s wuxga -r 5 -i :1 output.avi -loglevel repeat+level+debug -report
[info] ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers
[info]   built with gcc 7 (SUSE Linux)
[info]   configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --incdir=/usr/include/ffmpeg --docdir=/usr/share/doc/packages/ffmpeg --extra-cflags='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection' --optflags='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection' --enable-pic --disable-stripping --enable-shared --disable-static --enable-runtime-cpudetect --enable-libxcb --enable-libxcb-shm --enable-libxcb-xfixes --enable-libxcb-shape
[info]   libavutil      56. 14.100 / 56. 14.100
[info]   libavcodec     58. 18.100 / 58. 18.100
[info]   libavformat    58. 12.100 / 58. 12.100
[info]   libavdevice    58.  3.100 / 58.  3.100
[info]   libavfilter     7. 16.100 /  7. 16.100
[info]   libswscale      5.  1.100 /  5.  1.100
[info]   libswresample   3.  1.100 /  3.  1.100
...
...
[info] Output #0, avi, to 'output.avi':
[info]   Metadata:
[info]     ISFT            : Lavf58.12.100
[info]     Stream #0:0, 0, 1/5: Video: mpeg4, 1 reference frame (FMP4 / 0x34504D46), yuv420p, 1920x1200, 0/1, q=2-31, 200 kb/s, 5 fps, 5 tbn, 5 tbc
[info]     Metadata:
[info]       encoder         : Lavc58.18.100 mpeg4
[info]     Side data:
[info]       cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
...
...
[info] video:811kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.711410%
[verbose] Input file #0 (:1):
[verbose]   Input stream #0:0 (video): 9 packets read (82944000 bytes); 9 frames decoded; 
[verbose]   Total: 9 packets (82944000 bytes) demuxed
[verbose] Output file #0 (output.avi):
[verbose]   Output stream #0:0 (video): 9 frames encoded; 9 packets muxed (830464 bytes); 
[verbose]   Total: 9 packets (830464 bytes) muxed
[debug] 9 frames successfully decoded, 0 decoding errors
[AVIOContext @ 0x563d11c6aac0] [debug] Statistics: 8 seeks, 11 writeouts

After adding the encoder option -c:v h264_v4l2m2m the capture fails:

[info] ffmpeg started on 2019-09-16 at 10:59:04
Report written to "ffmpeg-20190916-105904.log"
Command line:
ffmpeg -y -f x11grab -s wuxga -r 5 -i :1 -c:v h264_v4l2m2m output.avi -loglevel repeat+level+debug -report
[info] ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers
[info]   built with gcc 7 (SUSE Linux)
[info]   configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --incdir=/usr/include/ffmpeg --docdir=/usr/share/doc/packages/ffmpeg --extra-cflags='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection' --optflags='-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection' --enable-pic --disable-stripping --enable-shared --disable-static --enable-runtime-cpudetect --enable-libxcb --enable-libxcb-shm --enable-libxcb-xfixes --enable-libxcb-shape
[info]   libavutil      56. 14.100 / 56. 14.100
[info]   libavcodec     58. 18.100 / 58. 18.100
[info]   libavformat    58. 12.100 / 58. 12.100
[info]   libavdevice    58.  3.100 / 58.  3.100
[info]   libavfilter     7. 16.100 /  7. 16.100
[info]   libswscale      5.  1.100 /  5.  1.100
[info]   libswresample   3.  1.100 /  3.  1.100
...
...
[debug] Applying option c:v (codec name) with argument h264_v4l2m2m.
...
...
[info] Stream mapping:
[info]   Stream #0:0 -> #0:0 (rawvideo (native) -> h264 (h264_v4l2m2m))
...
...
[h264_v4l2m2m @ 0x5605d58c0100] [error] Could not find a valid device
[h264_v4l2m2m @ 0x5605d58c0100] [error] can't configure encoder
[error] Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
[AVIOContext @ 0x5605d58c1280] [debug] Statistics: 0 seeks, 0 writeouts
[info] Conversion failed!

What am I doing wrong? (For completeness, I have a custom build of ffmpeg, but the encoder seems to be included…)

Listings are shortened because of SE politics, links to the full versions:

Answer

Attribution
Source : Link , Question Author : Peter R. , Answer Author : Community

Leave a Comment