Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Fix Aspect Ratio of MKV Files

The steps below can be used to fix a video having the incorrect aspect ratio. I don't know what causes such issues, but here is how to fix it.

Prerequisites

This tutorial requires mkvmerge which can be installed with:

sudo apt install mkvtoolnix

Steps

Firstly, it is useful to know what aspect ratio you need. You could guess, but if the "bad" video is part of a series, then I would get the information from one of the other episodes using:

ffmpeg -i input.mkv

Then look for text similar to below in the output:

Stream #0:0(eng): Video: h264 (High), yuv420p(tv, bt709/unknown/unknown, progressive), 1920x872, SAR 1:1

In this case, my desired aspect ratio is 1920 (width) by 872 (height). We can now plug this information into mkvmerge to create a copy of our bad video file, with a corrected aspect ratio.

mkvmerge \
--aspect-ratio $VIDEO_TRACK_ID:$WIDTH/$HEIGHT \
-o output.mkv \
input.mkv

If you are unsure of the video track ID, use mkvmerge -i input.mkv and that will output the tracks and their IDs.

For my example, the following command creates the correct file with the name output.mkv from the input.mkv file.

mkvmerge \
--aspect-ratio 0:1920/872 \
-o output.mkv \
input.mkv
Last updated: 16th August 2018
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch