I took a screen recording today with the intention of posting it on Mastodon. That's when I learned about the decentralized social network's file limitations. I've run into that on other social networks. Usually file size or length being the error-serving limitation. In this case, it was the actual dimensions of the video.
The official documentation sets the following constraints on video uploads:
- Filetype: MP4, M4V, MOV, WebM
- File size: up to 40MB
- Max bitrate: 1300kbps
- Max framerate: 60fps
Not mentioned is a resolution limit of 1920x1200. That's what tripped me up.
Some more Googling brought me to Paolo Melchiorre's blog, where he provides an FFmpeg command to transcode a video file according to the appropriate parameters:
ffmpeg -i input.mp4 -vf scale=1920:-1 -vcodec libvpx-vp9 -crf 31 -b:v 1800k -quality good -speed 0 output.webm
This is pretty good, but in the case of my cropped screenshare, the ratio didn't match 16:10. Meaning my height still exceeded the limit when the appropriate width was set.
Using the library's scaling filters, we can improve this by replacing
scale=1920:-1, which sets the width limit to 1920 and keeps the aspect ratio, with
scale=w=1920:h=1200:force_original_aspect_ratio=decrease. This looks messier, but will set a limit for both height and width while maintaining the original aspect ratio—only decreasing the size of the video dimensions when necessary to avoid upscaling.
That gives us this:
ffmpeg -i input.mp4 -vf scale=w=1920:h=1200:force_original_aspect_ratio=decrease -vcodec libx264 -crf 31 -b:v 1300k output.mp4
Optionally, you could add letterboxing to your video using:
ffmpeg -i input.mp4 -vf scale=w=1920:h=1200:force_original_aspect_ratio=decrease,pad=1920:1200:(ow-iw)/2:(oh-ih)/2" -vcodec libx264 -crf 31 -b:v 1300k output.mp4
That's it! Because Mastodon automatically transcodes uploads to H.264 MP4, I opted to switch the codec used in my commands to match. This removes the need for
Some additional notes on other the parameters:
-crf- a quality scale of 0-51. 23 is the default.
-b:v- video bitrate.
Comparing Paolo's VP9
webms to standard H.264
mp4s, I found the latter significantly quicker to transcode, upload, and process on the server. The time to upload and process the files was a full 10x faster.