MATLAB: Write video in wide screen format

videovideo processing

I'm creating a video using MATLAB's video writer function but the video always shows in a different visual format. I'm creating the video from frames of another (videoIn.mp4) with resolution of 720 x 576.
When I reproduce videoIn.mp4 in a video reproducer, it shows correctly. But when I reproduce the one generated by MATLAB, it shows "compressed" on the sides (not wide enough).
Does anyone know how to fix it?
This is my code:
myVideo = VideoWriter('videoOut.mp4','MPEG-4');
myVideo.FrameRate = 25;
myVideo.Quality = 100;
open(myVideo);
videoObject = VideoReader('videoIn.mp4');
framesToCopy = 1:50;
for nFrame = 1:length(framesToCopy)
mov(nFrame).cdata = readFrame(videoObject);
writeVideo(myVideo,mov(nFrame).cdata);
end
close(myVideo);
This is a screenshot of both videos open with VLC
video2.jpg

Best Answer

Matlab assumes and always display the video with a pixel aspect ratio of 1:1.
My guess is that your original video specify a different aspect ratio, that VLC respects (hence it stretches the video horizontally). Unfortunaly, that property is not accessible within matlab. The MP4 format is sufficiently complex that extracting that information within matlab without the help of a library would be substantial work.
So, if you want to process that video in matlab, you'll be stuck with a final video with a 1:1 pixel aspect ratio. It must be noted that the video is not constrained. It's more that the original is artificially stretched from its actual 720 pixels width by VLC.
A quick search shows that a command line program mp4box would allow you to change the pixel aspect ratio (without recoding the video). See this page where it's discuss. Never having used it (or heard of it before today) I can't vouch for it.
Related Question