Search code examples
ffmpegencodetranscodevaapi

ffmpeg vaapi - scale a video keeping the aspect ratio and padding with black lines


I'm trying to use my Vega 11 GPU to perform hardware accelerated transcoding of some video files to a resolution of 1280x720. I would like to keep the original aspect ratio and add black bars to the borders as necessary. I can get the scaling to work with the following:

ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -i input.mp4 -codec:v h264_vaapi -vf scale_vaapi=w=1280:h=720,setsar=1:1 output.mp4

But some of the image gets stretched with that, and the original aspect ratio is not preserved.

On the Raspberry Pi I can get the effect I want with the following:

ffmpeg -i input.mp4 -b:v 2M -vf scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:'(ow-iw)/2':'(oh-ih)/2',setsar=1 -vcodec h264_v4l2m2m -num_capture_buffers 128 output.mp4

But when I try to use the force_original_aspect_ratio and pad options with the vaapi codec, I get the following error:

ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -i input.mp4 -codec:v h264_vaapi -vf scale_vaapi=w=1280:h=720,pad=1280:720:'(ow-iw)/2':'(oh-ih)/2',setsar=1:1 output.mp4

Impossible to convert between the formats supported by the filter 'Parsed_scale_vaapi_0' and the filter 'auto_scaler_0'
Error reinitializing filters!
Failed to inject frame into filter network: Function not implemented
Error while processing the decoded data for stream #0:0

Is there another option I should use to get the padding and keep the original aspect ratio?


Solution

  • Hy although this is an old thread, this is my solution:

    -vf scale_vaapi=-2:720

    "If one and only one of the values is -n with n >= 1, the scale filter will use a value that maintains the aspect ratio of the input image, calculated from the other specified dimension. After that it will, however, make sure that the calculated dimension is divisible by n and adjust the value if necessary."(http://ffmpeg.org/ffmpeg-filters.html#scale )

    So TLDR, replace ome part of the aspect ratio with a lower than 0 number, and it will autoscale for it