Quickly resize a video with FFmpeg/Vaapi for Mastodon

How to quickly resize a video with FFmpeg before uploading it to Mastodon to comply its resolution and size restrictions.

© 2022 Paolo Melchiorre CC BY-SA “Samuel, Sebastian and Michael at PyCon US 2023 in Salt Lake City, Utah (USA)”
© 2022 Paolo Melchiorre CC BY-SA “Samuel, Sebastian and Michael at PyCon US 2023 in Salt Lake City, Utah (USA)”
Video editing with FFmpeg (2 part series)
  1. Resize a video with FFmpeg for Mastodon
  2. Quickly resize a video with FFmpeg/Vaapi for Mastodon

TL;DR

^ Read the disclaimer below.

$ ffmpeg \
-hwaccel vaapi \
-hwaccel_device /dev/dri/renderD128 \
-hwaccel_output_format vaapi \
-i input.mp4 \
-vf scale_vaapi=1920:-1 \
-codec:v vp9_vaapi \
-b:v 2M \
output.webm

Premise

Last week I attended the PyCon US 2023 in Salt Lake City, Utah (USA) and as for other conferences, I shared some moments live on Mastodon.

While working on the slides for my talk, I was in the speakers’ room and unexpectedly, I witnessed a recording of an episode of Michael Kennedy’s podcast which hosted Samuel Colvin, creator of Pydantic, and Sebastian Ramirez, creator of FastAPI.

I took the opportunity to immortalize the interview and make a short recording with my phone, which produced a file too large to be uploaded directly to Mastodon.

Media restrictions

Mastodon’s limitations on media files that can be uploaded have changed in recent months, but still remain stringent for media produced by some devices.

Here are the new limits:

FFmpeg command

The video in .mp4 format I recorded with my phone was only 25 seconds long but had a size of 135 MB and a resolution of 3840 x 2160 pixels.

To be able to upload the file to Mastodon I had to reduce its size and in parallel, I chose to reduce the resolution so as not to degrade the quality too much using FFmpeg as follows:

$ ffmpeg \
-i input.mp4 \
-vf scale=1920:-1 \
-vcodec libvpx-vp9 \
-crf 31 \
-b:v 2M \
output.webm
...
frame=737 fps=6.9 q=40.0 Lsize=6842kB time=00:00:24.53 bitrate=2284.8kbits/s speed=0.229x

Although the generated file allowed me to upload it to Mastodon, the transcoding time of the video was very long and the load on my CPU was very high.

After a bit of research, I found a solution for these two problems which consists in exploiting the potential of the GPU (in my case only an integrated Intel graphic card) directly from FFmpeg with VA-API (Video Acceleration API).

$ ffmpeg \
-hwaccel vaapi \
-hwaccel_device /dev/dri/renderD128 \
-hwaccel_output_format vaapi \
-i input.mp4 \
-vf scale_vaapi=1920:-1 \
-codec:v vp9_vaapi \
-b:v 2M \
output.webm
...
frame=737 fps=154 q=-0.0 Lsize=6490kB time=00:00:24.53 bitrate=2167.1kbits/s speed=5.14x

The command was slightly longer, but transcoding time went from more than 4x the length of the movie to just a fifth: a 22x speedup.

During the transcoding, the load on the CPU remained almost zero allowing me to continue using the PC for other operations.

Disclaimer

Much more than in other cases, the use of the GPU directly from FFmpeg is strictly linked to the hardware architecture, the operating system, and also software versions.

For example, if you don’t have the encoding function for the VP9 format directly in your hardware architecture, you can fall back on the VP8 format.

You may have other types of limitations and you will have to look for information online to get around them.

Mastodon toot

The resulting file from the conversion had a size of 6.5 megabytes and a resolution of 1920 x 1080 pixels which I was able to upload to Mastodon.

https://fosstodon.org/@paulox/110245038967758877

References

To learn more, you can read:

Updates

2024-01-18

Fixed command line instructions, and updated to VP9 codec.