Resize a video with FFmpeg for Mastodon

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

© 2022 Paolo Melchiorre CC BY-SA “Noah Alorwu warming up the audience (with Mauro Gaspari) at Ubuntu Summit 2022 in Prague (Czech Republic)”
© 2022 Paolo Melchiorre CC BY-SA “Noah Alorwu warming up the audience (with Mauro Gaspari) at Ubuntu Summit 2022 in Prague (Czech Republic)”
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

$ ffmpeg \
-i input.mp4 \
-vf scale=1920:-1 \
-vcodec libvpx-vp9 \
-crf 31 \
-b:v 1800k \
output.webm

Premise

Last week I attended the Ubuntu Summit 2022 in Prague (Czech Republic) and as for other conferences I shared some moments live on Mastodon.

At the start of day three Noah Alorwu animated the audience with a fun little dance which I filmed, but when I tried to upload the file to a post, Mastodon gave me an error message.

Media restrictions

Although not well documented I have found the restrictions that Mastodon applies to videos which I try to summarize below:

FFmpeg command

The video in mp4 format I recorded was only 12 seconds long but had a size of 65 MB and a resolution of 3840 x 2160 pixels.

To be able to upload it to Mastodon I had to reduce the last two parameters using FFmpeg as follows:

$ ffmpeg \
-i input.mp4 \
-vf scale=1920:-1 \
-vcodec libvpx-vp9 \
-crf 31 \
-b:v 1800k \
output.webm

Extract a frame

With ffmpeg you can extract the first frame as WebP image from the video to use it as video preview.

$ ffmpeg \
-i input.mp4 \
-vf scale=1920:-1 \
-vframes 1 \
output.webp

Mastodon toot

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

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

References

To learn more, you can read:

Updates

2022-11-27

After sharing the article on Lobsters 🦞 I received many comments inviting me to use more open formats for the container and codecs. Thanks to these comments I discovered the “Web video codec guide” on MDN which suggested using:

A WebM container using the VP9 codec for video and the Opus codec for audio. These are all open, royalty-free formats which are generally well-supported …”

I found on the FFMpeg VP9 wiki page reasonable parameters to produce good quality video files for online distribution with considerable size scaling and used the values suggested for Full HD videos on Google’s guide for VODs.

2023-01-26

Added a section on extracting the first frame as an image from the original video.

2023-05-03

Removed useless settings from the command line, fixed first frame image format, ad added article series.

2024-01-18

Fixed command line instructions.