Every day we come across useless one-gigabyte software installations. At the same time, useful one-megabyte command-line tools do exist. It’s the main reason why the software industry is so dynamic.
One of these powerful and useful tools—that reminds us why they’ve been called utilities in the first place—is MP4Box, and it can be utilized to fix one of the most annoying aspects of digital media, encoded by almost everyone—yes, wrong aspect ratios.
When re-encoding is out of the question, you can modify the container pixel aspect ratio (PAR) of MP4 files—such as MPEG-4 AVC (H.264) video clips—using the following MP4Box command:
mp4box -add input.mp4#video:par=X:Y -add input.mp4#audio output.mp4
Please note the second add tag (-add input.mp4#audio
) is to include the audio stream, so it’s obvious it should be dropped if the MPEG-4 clip doesn’t contain an audio stream. Also, if you know the track ID of the video stream, you can alternatively use the following MP4Box command:
mp4box -par TrackID=X:Y inputoutput.mp4
For example, we have a “George_Carlin.mp4” encoded with 480×360 pixels—meaning a storage aspect ratio (SAR) of 1.333:1. But we know—out of George’s squeezed body—that it should be a 16:9 video—meaning a wide display aspect ratio (DAR) of 1.778:1.
Display Aspect Ratio = Storage Aspect Ratio × Pixel Aspect Ratio
or simply
DAR = SAR × PAR
To calculate pixel aspect ratio, when we want to force a video with a fixed SAR (this cannot be changed, because it’s actually video’s own width to height ratio) to be displayed with a specific DAR—commonly normal (4:3) or widescreen (16:9):
PAR = DAR ÷ SAR
So, let’s calculate
DAR: 1.78 (16:9), because video looked horizontally squeezed
SAR = W ÷ H = 480 ÷ 360 = 1.33 (4:3)
PAR = DAR ÷ SAR = 1.78 ÷ 1.33 = 1.33 (4:3)
Now, use MP4Box with the calculated 4:3 PAR to force the container’s PAR over bitstream’s:
mp4box -add George_Carlin.mp4#video:par=4:3 -add George_Carlin.mp4#audio George_Carlin_fixed.mp4
Please note it doesn’t reset the output file, so if your output file exists, it will simply add those audio/video streams to the file, resulting in a strange file, with multiple audio/video streams. So don’t blame “MP4Box.exe” for ending up with two movie clip windows rendered simultaneously in your media player! Make sure the output file doesn’t exist.
Alternatively, considering video stream’s track ID to be “1”, the following “MP4Box.exe” command fixes the PAR, as well:
mp4box –par 1=4:3 George_Carlin.mp4
Please note the latter overwrites the input file without confirmation, so make a backup beforehand. Specifying a wrong track ID will result in “Error: Bad Parameter.”
Supported Container Formats:
- ISO-Media, including, but not limited to MPEG-4 Part 12, MPEG-4 Part 14 (.mp4, .3gp)
- AVI (.avi)
- MPEG-2 PS/Program Stream (.mpg, .mpeg, .vob, .dat, .vcd, .svcd)
- MPEG-2 TS/Transport Stream (.ts, .m2t)
- QCP (.qcp)
- OGG (.ogg)
Supported Video Formats:
- MPEG-1 Video (.m1v)
- MPEG-2 Video (.m2v)
- MPEG-4 Video (.cmp, .m4v)
- H.263 Video (.263, .h263)
- MPEG-4 Part 10/AVC/H.264 Video (.h264, .h26l, .264, .26l)
Supported Audio Formats:
- MPEG-1 Audio (.mp3, .m1a)
- MPEG-2 Audio (.m2a)
- ADTS-AAC Audio (.aac)
- AMR Audio (.amr, .awb)
- EVRC Audio (.evc)
- SMV Audio (.smv)
How to fit a widescreen film in to 35mm frame? How to fit 800×640 in to normal – wide – cinema frame?
To squeeze a wide 16:9 to 35 mm film 3:2 or convert from SAR 16:9 (1.78:1) to DAR 3:2 (1.50:1), modify the PAR to 27:32
800×640 → 5:4
To convert from SAR 5:4 (1.25:1) to DAR 4:3 (1.33:1), modify the PAR to 16:15
To convert from SAR 5:4 (1.25:1) to DAR 16:9 (1.78:1), modify the PAR to 64:45
To convert from SAR 5:4 (1.25:1) to DAR 37:20 (1.85:1), modify the PAR to 37:25
To convert from SAR 5:4 (1.25:1) to DAR 2:1, modify the PAR to 8:5
How about if I have a 720×480 original mp4 that doesn’t have the proper PAR assigned. The DAR of this file is 16:9. 720/480 = 1.5, so 1.5 is the SAR, right? What should the PAR be?
Yes. SAR = 1.5:1 (3:2). If you want to modify it so it’s rendered at wide 1.778:1 (DAR 16:9), then the PAR must be set to 32:27
mp4box.exe -add input.mp4#video:par=32:27 -add input.mp4#audio output.mp4
Calculation: PAR = DAR ÷ SAR = 16:9 ÷ 3:2 = 32:27
Im new to this program (using v0.4.5) and am trying to figure out how to change a 4:3 into a 16:9. the file is incorrectly encoded and looks wrong.
Im having some issues with the above syntax
“mp4box -add input.mp4#video:par=X:Y -add input.mp4#audio output.mp4”
IS THIS SYNTAX ACTUALLY CORRECT?
Affirmative, it works with MP4Box – GPAC version 0.4.5 (build 33 – Dec 11 2008).
Affirmative, that’s the command if a) your MP4 container format, video stream format, and audio stream format are SUPPORTED by MP4Box, and b) your MP4 container contains AT LEAST ONE VIDEO STREAM and ONE AUDIO STREAM.
«I’m having some issues with the above syntax» doesn’t help the person who’s trying to help you. BE MORE SPECIFIC about the nature of the issue. Capture the Command-Prompt screen and paste it in your post. Get information about your MP4 file using MediaInfo.
SAR 4:3 → DAR 16:9 requires PAR 4:3
Real life Command-Prompt screen capture for skeptics:
——————————————————————————-
C:\Program Files\MP4Box>mp4box -version
MP4Box – GPAC version 0.4.5 (build 33 – Dec 11 2008) – compiled by Kurtnoise
GPAC Copyright: (c) Jean Le Feuvre 2000-2005
(c) ENST 2005-200X
C:\Program Files\MP4Box>mp4box -add “4-3 SAR Video.mp4″#video:par=4:3 -add “4-3
SAR Video.mp4″#audio “16-9 PAR Video.mp4”
IsoMedia import – track ID 2 – Video (size 480 x 360)
IsoMedia import – track ID 1 – Audio (SR 44100 – 2 channels)
Saving to 16-9 PAR Video.mp4: 0.500 secs Interleaving
——————————————————————————-
sorry about the lack of specificity, i did not intend to be rude, only question the subtleties of the syntax.
the mistake was mine. as i figured it would be.
i added an extra space between the : and par command. took a fresh set of eyes to see it.
F:\video tools\mp4box>mp4box -add wrong.mp4#video: par=4:3 -add wrong.mp4#audio
output_file.mp4
Error – 2 input names specified, please check usage
one thing i couldn’t get to work was the direct overwrite. but thats because the video was on track 2. i know you stated that such an error would occur if the wrong track was selected. is there a way to tell which track the video is on before processing?
F:\video tools\mp4box>mp4box -par 1=4:3 c:\wrong.mp4
Error: Bad Parameter
F:\video tools\mp4box>mp4box -par 2=4:3 c:\wrong.mp4
Saving c:\wrong.mp4: 0.500 secs Interleaving
Great post and thanks for the help!!
very detail and easy to follow, now I can work on my video collections, thanks !
Thanks for the help. I’ve been trying to convert MPG files into MP4 files without recoding but the video size kept getting messed up. But I say in your sample how to pull the video directly from the original. I had been demuxing and them adding the parts. But now I have it working perfectly.
Thanks again.
If anyone is interested here is what I’m using
It extracts the audio because MP4Box didn’t seem to want to do that right. It also adds the subtitles from a SRT file into the MP4 Container.
—–
@echo off
if [%1]==[] goto :eof
:loop
echo “Processing %~dpn1.mp4”
:: Remove any existing mp4 file
echo y | del “%~dpn1.mp4”
:: *** Extract the parts
:: Extract the Video
:: *** We don’t need the video we will pull it from the mpg directly.
:: mpegdemux -d -s 0xe0 %1 “C:\tmp\%~nx1.m2v”
:: Extract the Audio
mpegdemux -d -s 0xbd -p 0x80 –ac3 %1 “C:\tmp\%~nx1.ac3”
:: ***Put the parts back together
:: Video directly from MPG Andio from extracted AC3 subtitles from SRT file
MP4Box -add “%~dpn1.mpg#video” -add “C:\tmp\%~nx1.ac3” -add “%~dpn1.srt” -new “%~dpn1.mp4”
:: Delete the Parts
::echo y | del C:\tmp\%~nx1.m2v
echo y | del C:\tmp\%~nx1.ac3
::Move the orig files into a backup folder
move %1 “%~dp1\Backup\%~nx1”
move “%~dpn1.srt” “%~dp1\Backup\%~n1.srt”
shift
if not [%1]==[] goto loop
Hi, I really need to understand this, let’s say I have a video with a SAR of 1920×800 px and for some reason it has a DAR of 16:9 instead of 1:2.4
How can I set the right -par parameter? which one is the equation?
I finally solve it by setting “none” to the -par parameter to remove the aspect ratio correction, but I really like to know the math behind this trick because I know it is very useful.
Your original video size is 1920×800, so it has a SAR of 2.4:1 (not 1:2.4).
If you want to display it at its original size—with “DAR = SAR × PAR” in mind—you need to remove any PAR set; or in other words, a PAR of 1:1 will preserve its original size.
Now let’s try another example. If you want to display it in 16:9—that will stretch its height—you need to make it 1920×1080 when played; that’s a 16:9 DAR. With “PAR = DAR ÷ SAR” in mind, you will need to set a PAR of 20:27.
The math behind: PAR = 1.78 ÷ 2.4 = 20:27
I still don’t get it, 1.78/2.4=0.7416666666666667…
Where the 20 came from and where the 27 came from?
Here is another example I’m trying to understand with simple math, I have a video with 720×480 SAR, the DAR has to be 16:9
“PAR = DAR ÷ SAR”
DAR is 16/9 which is = 1.777777777777778
SAR is 720/480 which is = 1.5
PAR is 1.78/1.5 which is = to 1.186666666666667
¿?¿?¿?¿?
Rodrigo, if one asks you give me the fraction of 0.5, you’d say ½. For 0.75, you’d say ¾. For 0.3333~, you’d say 1/3. And for 0.7416666~, well, it’s not that easy. In fact, it’s not fine to work with fixed-point arithmetic. Fractions are more precise: lossless. To be honest, there’s nothing as “simple math.” To crack this, simple mathematic subjects you need to know include:
Vulgar fraction (common fraction / simple fraction)—e.g., 2/5
—Proper fraction—e.g., 2/5
—Improper fraction (top-heavy fraction)—e.g., 5/2
Equivalent fractions—e.g., 1/2 = 2/4 = 18/36 = 100/200
Complex fraction—e.g., ½/¾
Reducing fractions (simplifying the fraction)—e.g., 4/6 → 2/3
A fraction in which the numerator and denominator are coprime—this means they have no factors in common other than 1—is said to be irreducible or in its lowest or simplest terms. Any fraction can be fully reduced to its lowest terms by dividing both the numerator and denominator by their GCD (greatest common divisor). For example, the greatest common divisor of 800 and 1080 is 40, therefore, the fraction 800/1080 can be fully reduced by dividing the numerator and denominator by 40:
800/1080 = 20/27 or aspect-ratio-ly speaking 20:27
The Euclidean algorithm (also called Euclid’s algorithm) can be used for computing the greatest common divisor (GCD), also known as the greatest common factor (GCF) or highest common factor (HCF).
So, this is the math behind 20:27—displaying a 1920×800 picture as vertically-stretched 1920×1080:
PAR = DAR ÷ SAR
PAR = (1920/1080) ÷ (1920/800) = 1536000/2073600 = 20/27
PAR = 20:27
In the second example you brought up—displaying a 720×480 picture in 16:9 format:
PAR = 16/9 ÷ 720/480 = 7680/6480 = 32/27
PAR = 32:27
Hehehehe I feel so dumb, I assumed that the “/” was the division operator not the sign to indicate fractions, now it’s very clear, and thank you very very much for the long and very well done explanation, seriously, I appreciated.
I did a JavaScript code to obtain the PAR having the SAR and the DAR:
function getGCD(x,y){
var w;
while (y != 0){
w = x % y;
x = y;
y = w;
}
return x;
}
function getPAR(SAR_width,SAR_height,ASPECT_width,ASPECT_height){
/*
var SAR_width = 720;
var SAR_height = 544;
var ASPECT_width = 16;
var ASPECT_height = 9;
*/
var DAR_width = SAR_width;
var DAR_height = (SAR_width/ASPECT_width)*ASPECT_height;
var BigPar_width = SAR_height * DAR_width;
var BigPar_height = SAR_width * DAR_height;
var Par_GCD = getGCD(BigPar_width,BigPar_height);
BigPar_width = (BigPar_width/Par_GCD);
BigPar_height = (BigPar_height/Par_GCD);
return BigPar_width+’:’+BigPar_height;
}
console.log(getPAR(720,544,16,9));
Thanks a lot for the help, I have added a tool for this adding credits to you: http://tools.rodrigopolo.com/mp4box_aspect_fix/
BTW check the other tools:
http://tools.rodrigopolo.com
Nice work. Thanks.
I want to change the aspect ratio of some mp4 videos form 4:3 to 16:9 and I listen that MP4box command line can do this. I have downloaded MP4Box 0.4.6 and launch MP4Box.exe file contained in the zip folder. A ms dos window opened and closed forthwith and now i am stuck here that how to start the MP4Box command line. what’s the reason of this.
Google, Wikipedia and YouTube are your friends:
http://www.youtube.com/watch?v=foYsmpDrYjY
Just type what you want to learn!
The reason it immediately opens and closes is because it has nothing to do upon execution with no parameter to process when clicked upon. This is what happens:
• Double-clicking on an exe file makes Windows Explorer (My Computer) attempt to run it.
• If it is a Console application (i.e., no GUI window) the Explorer will call Command Prompt (cmd.exe) passing the Console application as a parameter (e.g., %SystemRoot%\system32\cmd.exe “D:\MP4Box 0.4.6\MP4Box.exe”).
• Hereafter, it’s the Command Prompt who’s in control of the execution. MP4Box.exe requires its own parameters to process, and with nothing for MP4Box.exe to do, it’s soon ended, resulting the DOS-like window to close immediately.
So, to be able to see what’s really happening before the window is being closed, you’ll have to open a command prompt, navigate the command processor to MP4Box location, and then run it:
• Click on Command Prompt, or simply run cmd.exe.
• Using CD (change directory) command, navigate to the drive/folder you have previously extracted the files for MP4Box. A /d switch is required if you’re changing the drive comparing to your current Prompt drive. Enclose the path in double quotations if it contains any space character (e.g., cd /d “D:\MP4Box 0.4.6”).
• Now, enter the executable name and press enter (e.g., MP4Box).
• The file executes, and you can see the result. Now try it with parameters discussed above in the article.
• After you’re done with it, close the Shell using the exit command.
Now, if you’re using Windows 7, there’s a faster shortcut into opening and navigating a CMD into a specific folder: Navigate to that folder in Windows Explorer (My Computer). While holding the shift key down, right-click on a blank portion of the folder content (or even as a faster alternative, use Shift+Menu on the keyboard). Amazingly, the popup menu contains different items, including an Open command windows here. Try that one.
THANK YOU SO MUCH both Komeil and Rodrigo Polo for your excellent effort to provide with such a precious tutorial. Rodrigo by the way your java tool (aspect fix) made things soooo much easier plus it works like a charm!!!
Thanks again so much….you both kick ass !!
P.S. only problem I have on the new changed aspect mp4 file is that when you skip forward the video it gets blockie – big pixel blocks come up in different areas on the screen while playing the video after fast forwarding it. Any idea how to fix this will be very appreciated. Thank you
Sounds like a key frame problem.
In video compression, a keyframe (aka intra-frame) is a frame in which a complete image is stored in the data stream. In order to greatly reduce the amount of information stored compressedly, only changes that occur from one frame to the next are stored in the data stream.
Except for keyframes themselves, because video compression only stores incremental changes between frames, it is not possible to easily fast forward or rewind to any arbitrary spot in the video stream—since the data for a given frame only represents how that frame was different from the preceding frame.
Now, your video with the blocks; sounds like your video player has a bug in its rewind/fast forward algorithm as it is not able to determine all the proper changes comparing to both the keyframe and prior frames between the sought frame itself and its preceding keyframe.
If the problem is only with the fixed clip and not with the one with the wrong ratio, then it’s the player algorithm’s fault. Try another player (software / home cinema system). Be advised cheap home systems tend to ignore the ratio flag. Home Blu-ray Disc players based on MediaTek MT8555 are a good example of those respecting the flag. (Pioneer BD players BDP-LX55 & BDP-440)
I use VLC where I noticed that issue as well but in Gom player it plays the file like the file its corrupt with distorted image during play and the most important is that the 16:9 file that was changed to 4:3 is still being shown into 16:9 plus after changing the aspect ratio it adds those corrupt issues in the file.
Do you know if there is any way to fix this in the MP4Box or do you know any other aspect changing program without encoding the mp4 file?
Or the third alternative for a solution would be if you know any other program that is 100% compatible with the MP4Box converted files and can play-rewind-fast forward the changed aspect file normally without any bad algorithm issues which cause distorted image during play.
What do you suggest?
Thank you
This is actually an encoding issue, to be able to “scrub” you have to encode your video specifying a keyframe rate, I use one per second so I can scrub between seconds, about the aspect ratio, once you manage to fix the aspect ratio with MP4Box it works, it does, if it doesn’t work with your video maybe you are miscalculating the aspect ratio, an easy way I use to know the right aspect ratio is by playing the video on VLC, changing the video to the wright aspect ratio, taking a snapshot and then I check the dimensions of the snapshot to have the DAP (display aspect ratio) then knowing the SAR (source aspect ratio) by checking the video information with MediaInfoRaw I do the math to know the PAR (picture aspect ratio).
Hey Komeil, thanks for this article. I have some videos I downloaded from Youtube that have the wrong aspect ratio. I opened them with MplayerX (I’m on a Mac) and determined that they should be 5:4 and not the original 16:9. I first tried using ffmpeg to simply change the aspect ratio in the header but some of them did not display the change even though it was there. I quickly learned that changing the PAR with mp4box was much more reliable so I tried it using your math in Excel, formatting the result to a fraction. That worked great. But I wanted a bash script to do all the work for me. That became a bit of a project since I didn’t know how to get the original SAR and didn’t know how to do the math in bash. I tried various methods I found on the net to get the SAR. I finally settled on using the CLI version of mediainfo:
mediainfo –Inform=”Video;%DisplayAspectRatio/String%” “video file.mp4”
This gives:
16:9
Great, so I set a variable to the output. Next, the math. I can’t believe it but I struggled with this a bit before it dawned on me that it was ridiculously simple. I see that you and a few other people went through similar contortions before you settled on the same solution that I did. Well almost… I first tried just using the decimal PAR but, though mp4box did not complain about it, it did not change the DAR. So I did some googling and found really nothing useful for bash. I finally found the perl module Math::BigRat (funny name huh?) that will convert a decimal value to a reduced fraction and return numerator and denominator. This worked great but I couldn’t find any convenient way to incorporate it into a bash script and pass the values to the script. I went to bed tired and ‘slept on it’. When I awoke, the simple, obvious (funny how the obvious eludes us sometimes) solution was simply to get numerator and denominator using high school algebra, similar to what you show in one of your replies, which I hadn’t read yet. I use the SAR and the desired DAR, instead of the pixel height and width as you do, to get the PAR for mp4box. I don’t worry about reducing it because it is too much trouble and I think mp4box can handle it anyway.
PAR = DAR / SAR
nPAR / dPAR = (nDAR / dDAR) / (nSAR / dSAR) = (nDAR / dDAR) * (dSAR / nSAR)
nPAR = nDAR * dSAR
dPAR = dDAR * nSAR
Where ‘n’ refers to ‘numerator’ and ‘d’ to ‘denominator’.
This is what I needed.
So here is my bash script, ‘changeaspect.sh’, which reads the desired DAR from the command line, loops through all the files in a directory, gets the SAR of each file, changes the DAR using mp4box, and outputs the new file, with the DAR appended to its name, to a subdirectory with the new DAR as its name:
shopt -s nullglob
IFS=’:’ read -a DAR <<< "$@" # read DAR from command line
nDAR=${DAR[0]} # numerator
dDAR=${DAR[1]} # denominator
outdir="${nDAR}to${dDAR}"
if [ ! -d "$outdir" ]; then
mkdir "$outdir"
fi
for f in "$arg"*.{mp4,avi,3gp,mpg,mpeg,vob,dat,vcd,svcd,ts,m2t}
do
p="${f##*/}" # file
n="${p%.*}" # name
e="${p##*.}" # extension
i=`mediainfo –Inform="Video;%DisplayAspectRatio/String%" "$p"` # get original DAR:
echo ''
echo "Original DAR: $i"
echo ''
IFS=':' read -a SAR <<< "$i"
nSAR=${SAR[0]} # numerator
dSAR=${SAR[1]} # denominator
nPAR=$(($nDAR * $dSAR)) # numerator – nPAR = nDAR * dSAR
dPAR=$(($dDAR * $nSAR)) # denominator – dPAR = dDAR * nSAR
outfile="$n (${nDAR}to${dDAR}).$e"
outpath="$outdir/$outfile"
if [ -e "$outpath" ]; then
rm "$outpath"
fi
mp4box -new -add "$p#video:par=$nPAR:$dPAR" -add "$p#audio" "$outpath"
echo ''
i=`mediainfo –Inform="Video;%DisplayAspectRatio/String%" "$outpath"`
echo "PAR: ${nPAR}:${dPAR}"
echo "Modified DAR: $i"
done
echo ''
Even though '-new' causes mp4box to overwrite existing files, I had to delete them to get consistent results.
This is run from the command line:
$ changeaspect.sh 5:4
Example output:
Original DAR: 16:9
IsoMedia import – track ID 1 – Video (size 1280 x 720)
IsoMedia import – track ID 2 – Audio (SR 44100 – 2 channels)
Saving 5to4/James Potter and the Hall of Elders' Crossing Part 1 (HQ) (5to4).mp4: 0.500 secs Interleaving
PAR: 45:64
Modified DAR: 5:4
Original DAR: 16:9
IsoMedia import – track ID 1 – Video (size 1280 x 720)
IsoMedia import – track ID 2 – Audio (SR 44100 – 2 channels)
Saving 5to4/James Potter and the Hall of Elders' Crossing Part 2 (HQ) (5to4).mp4: 0.500 secs Interleaving
PAR: 45:64
Modified DAR: 5:4
…
I only tried this on mp4 files so far so I don't know how it will behave on the other container formats, but I included all the formats you mentioned for thoroughness. This also assumes there is only one video track.
You could use this script with cygwin on Windows I assume, with some modifications.
Hey Komeil, thanks for this article. I have some JPG files want to combine it into MEPG-4. Please advise how can we use MP4BOX to do it.
I didn’t know about the -par argument. Thanks to this I was able to fix my issues. Had a weird mpg video that wasn’t encoded correctly and when I tried to put it into an mp4, it kept flattening the video. My fix ended up being to specify -par 1=1:1 and now the video is coming out without being flattened. This was driving me crazy for months. Now I have to go back and re-encode a bunch of similarly badly encoded videos this same way. Cause the other way I was doing it was leading to videos working only on certain programs.
Thanks! Very helpful. I haven’t found this info presented this clearly anywhere else online.