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.
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:
-aspect 16:9
parameter in the ffmpeg command-vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1"
None of these give me the correct aspect ratio.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?
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...
1280x1280
-vf "crop=1280:720"
.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]