ffmpegraspberry-pivideo-streamingh.264

FFmpeg h264_v4l2m2m encoder changing aspect ratio from 16:9 to 1:1 with black bars


When switching from libx264 to h264_v4l2m2m encoder in FFmpeg for YouTube streaming, the output video's aspect ratio changes from 16:9 to 1:1 with black bars on the sides, despite keeping the same resolution settings.

Original working command (with libx264):

ffmpeg -f v4l2 \
    -input_format yuyv422 \
    -video_size 1280x720 \
    -framerate 30 \
    -i /dev/video0 \
    -f lavfi \
    -i anullsrc=r=44100:cl=stereo \
    -c:v libx264 \
    -preset ultrafast \
    -tune zerolatency \
    -b:v 2500k \
    -c:a aac \
    -b:a 128k \
    -ar 44100 \
    -f flv rtmp://a.rtmp.youtube.com/live2/[STREAM-KEY]

When I replaced libx264 with h264_v4lm2m, it always produce a square resolution, and it automatically adds black bars to the top and the bottom of the sides of the camera. I currently using a Rasberry Pi 4 model B, with a webcam that I believe supports the 16:9 ratio (I've verified using v4l2-ctl --list-formats-ext -d /dev/video0 command)

I've tried the follows:

How can I make the h264_v4l2m2m encoder maintain the original 16:9 aspect ratio without adding black bars? Is this a known limitation of the encoder, or am I missing some required parameters?


Solution

  • Looks like your input only gives a 1:1 aspect ratio with a square size range from 16 up 16376.
    Coincidentally 16 is the smallest picture size for h264 encodings (eg: it is one macroblock size).

    Possible solution:
    Extract your expected size from a larger "nearest" size.

    For example: If you want 1280x720 then...

    Try as:

    ffmpeg -f v4l2 \
        -input_format yuyv422 \
        -video_size 1280x1280 \
        -framerate 30 \
        -i /dev/video0 \
        -f lavfi \
        -i anullsrc=r=44100:cl=stereo \
        -c:v libx264 \
        -vf "crop=1280:720"
        -preset ultrafast \
        -tune zerolatency \
        -b:v 2500k \
        -c:a aac \
        -b:a 128k \
        -ar 44100 \
        -f flv rtmp://a.rtmp.youtube.com/live2/[STREAM-KEY]