17

I have a Radeon Vega 64, and installed AMDGPU from the official website, but I have no idea where to go from here.

I want to use hardware acceleration for converting video with FFmpeg. What is the corresponding FFmpeg command and how should I like the device/driver to FFmpeg?

2

3 Answers 3

21

I'll provide instructions for both Windows and Linux, as you've not indicated what platform you're on. Use these instructions that are applicable to your case.

If you're on Windows, you can access the related encoders via AMF, aptly named h264_amf and hevc_amf, whose usage can be viewed via:

ffmpeg -h encoder=h264_amf

ffmpeg -h encoder=hevc_amf

You'd need to build an FFmpeg binary for Windows, either natively, or as recommended, cross-compiling. This project is a great start on that subject.

If you use the cross-compile option, pass the arguments below for a build with the features you'll need:

./cross_compile_ffmpeg.sh --gcc-cpu-count=12  --build-libmxf=n --disable-nonfree=n --prefer-stable=y --compiler-flavors=multi

Note that the resulting build above is non-redistributable, and is only for personal use.

On Linux, using the opensource mesa amdgpu driver (not the proprietary package installed from AMD), you can access VAAPI-based encoders, aptly named h264_vaapi and hevc_vaapi, whose usage can be viewed via:

ffmpeg -h encoder=h264_vaapi

ffmpeg -h encoder=hevc_vaapi

To the best of my knowledge (correct me if I'm wrong), VP8 and VP9 encoding isn't exposed on AMD's side via VAAPI. You can confirm this by running:

vainfo | grep Slice

On your system and confirming what encoders are supported. It's most likely that FFmpeg with VAAPI enabled is the default on the likes of Ubuntu. However, you can also build a copy from source, if so needed. See the compilation guide on the FFmpeg Wiki, and adapt as needed. You may also find helpers, such as this, useful.

There's a possibility that the AMD AMF-based encoders will eventually land on Linux, using a Vulkan-based interop. Take this as a work in progress.

1
2

On MacOS Catalina via an external GPU over Thunderbolt 3, I had success using flag

-hwaccel videotoolbox

via the default brew installed copy. I could then use default libx265 encoder, at about 20x speed over encoder via CPU.

full:

ffmpeg 
  -hwaccel videotoolbox -i 'Forget to fly.mkv' \
  -c:v libx265 -preset medium -crf 28 \
  -c:a copy \
  'Forget to fly.x265.mkv'
0
0

Install VA-API drivers

# For Ubuntu
apt install mesa-va-drivers
# For Arch Linux
pacman -Sy libva-mesa-driver

If you don't install the driver, FFmpeg will produce an error:

Failed to initialise VAAPI connection: -1 (unknown libva error).

Convert videos

FFmpeg n7.0.1 successfully converted videos on the graphics card using these commands:

# AV1
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -vf 'format=vaapi,hwupload' -c:v av1_vaapi -q 160 output.mp4

# H.265 alias HEVC
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -vf 'format=vaapi,hwupload' -c:v hevc_vaapi -qp 32 ~/output.mp4

# H.264 alias AVC
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -i input.mp4 -vf 'format=vaapi,hwupload' -c:v h264_vaapi -qp 25 ~/output.mp4

Change the value after the -q and -qp options to affect the output quality. Lower numbers mean higher quality but increased file size.

You can use amdgpu_top to see GPU utilization during the video conversions.

Copy metadata

Add this to your FFmpeg command to include metadata like the video's creation time:

-movflags use_metadata_tags -map_metadata 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .