Write video in wide screen format

12 views (last 30 days)
Seven Eden
Seven Eden on 20 Mar 2019
Answered: Seven Eden on 23 Mar 2019
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
  6 Comments
Guillaume
Guillaume on 20 Mar 2019
Edited: Guillaume on 20 Mar 2019
It looks to me that VideoOut has got the correct aspect ratio for a 720x576 image. On the other hand, VideoIn looks like it is around twice as wide as tall, so does not match a 720x576 aspect ratio.
If you play the input video in matlab (with implay) what aspect ratio does it have?
Seven Eden
Seven Eden on 20 Mar 2019
It also looks constrained.
You are right, the original video doesn't seem to be display in a player with the actual aspect ratio the file has, my guess is that there is something in the file header that tells the player that it should play the video wider. But when I'm writing the new file I'm not putting this on it. The thing is that I don't know how to do it.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 20 Mar 2019
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.

More Answers (1)

Seven Eden
Seven Eden on 23 Mar 2019
I ended up resizing the images before putting them inside the movie:
myVideo = VideoWriter('videoOut.mp4','MPEG-4');
myVideo.FrameRate = 25;
myVideo.Quality = 100;
vidHeight = 576; %this is the value in which it should reproduce
vidWidth = 1024; %this is the value in which it should reproduce
open(myVideo);
videoObject = VideoReader('videoIn.mp4');
framesToCopy = 1:50;
for nFrame = 1:length(framesToCopy)
I = readFrame(videoObject);
J = imresize(I,[vidHeight vidWidth]);
mov(nFrame).cdata = J;
writeVideo(myVideo,mov(nFrame).cdata);
end
close(myVideo);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!