Correcting aspect ratio of a mp4 file

tags : VideoMaking,Linux,Aspect Ratio,Ezcap 280,MP4Box

I have a nice Ezcap 280  that I plan to use to grab HDMI video in a live video production; the only thing I don't like of it is the 5 seconds delay in the REC starting.

The original plan was to go with the HD (720p) video resolution, but not all the camcorders we use can do this (they only do 576p or 1080i, the last is a no-opt), so I've stepped down to SD (576p).

The HDMI resolution for PAL SD video is 720x576@50, and the aspect ratio is varied changing the pixel's proportions, like in the analogic, so the Ezcap grabs the video at this resolution, ignoring the aspect ratio, while the usual resolution for 16:9 PAL videos is 1024x576.

To have the video played correctly on PC, TV and multimedia devices without re-encoding the whole file, it is necessary to change the aspect ratio in the MP4 file metadata. I've found MP4Box, a  multi-platform tool for performing many manipulations on multimedia files. It can change the aspect ratio of files generated by the Ezcap, but you must perform some calculation to find the correct fraction to use. I haven't the time to learn how to perform this calculations by myself, but I have found this on-line calculator where you can retrieve the setting you need. In my case it is 64:45.

Once you have the correct fraction, you need to find the video stream identifier in your file (TracKID, in bold below):

$ MP4Box -info file.mp4
* Movie Info *
    Timescale 1000 - Duration 00:00:06.300
    1 track(s)
    Fragmented File: no
    File suitable for progressive download (moov before mdat)
    File Brand isom - version 512
    Created: UNKNOWN DATE   Modified: UNKNOWN DATE
File has no MPEG4 IOD/OD

Track # 1 Info - TrackID 2 - TimeScale 50 - Media Duration 00:00:06.300
Media Info: Language "Undetermined" - Type "vide:avc1" - 316 samples
Visual Track layout: x=0 y=0 width=720 height=576
MPEG-4 Config: Visual Stream - ObjectTypeIndication 0x21
AVC/H264 Video - Visual Size 720 x 576
    AVC Info: 1 SPS - 1 PPS - Profile Baseline @ Level 3
    NAL Unit length bits: 32
    Pixel Aspect Ratio 1:1 - Indicated track size 720 x 576
Self-synchronized

Once you have bothdata (TrackID and Aspect Ratio fraction), you can change your file with this command:

$ MP4Box -par '2=64:45' file.mp4

The syntax of par parameter is TrackID=Num:Den.

Et voilà, the file is now correctly played in 16:9 in all my devices:

$ MP4Box -info file.mp4
* Movie Info *
    Timescale 1000 - Duration 00:00:06.300
    1 track(s)
    Fragmented File: no
    File suitable for progressive download (moov before mdat)
    File Brand isom - version 512
    Created: UNKNOWN DATE    Modified: UNKNOWN DATE
File has no MPEG4 IOD/OD

Track # 1 Info - TrackID 2 - TimeScale 50 - Media Duration 00:00:06.300
Media Info: Language "Undetermined" - Type "vide:avc1" - 316 samples
Visual Track layout: x=0 y=0 width=1024 height=576
MPEG-4 Config: Visual Stream - ObjectTypeIndication 0x21
AVC/H264 Video - Visual Size 720 x 576
    AVC Info: 1 SPS - 1 PPS - Profile Baseline @ Level 3
    NAL Unit length bits: 32
    Pixel Aspect Ratio 64:45 - Indicated track size 1024 x 576
Self-synchronized