Saving to .mp4 file in MATLAB
Asked Answered
A

2

5

I'm editing all frames of an existing mp4 video in MATLAB (doing it in a for loop). After I'm done editing, I want to save the new set of frames to a new output video file, but in mp4 rather than .avi (that seems to be the default). I thought changing the filename extension is sufficient, but apparently it's not. Any ideas?

newVid = VideoWriter(outputfilename);
newVid.FrameRate = fps;
newVid.Quality = 100;
open(newVid)
for...
writeVideo(newVid,imgs{i})%within the for loop saving one frame at a time
end
close(newVid)
Airing answered 10/8, 2017 at 19:29 Comment(0)
T
9

Renaming the file is not sufficient. You also need to specify the codec you want. In your case, you need to include an additional parameter into the VideoWriter constructor that consists of the codec you want to use as a MATLAB string. In your case, specify 'MPEG-4':

newVid = VideoWriter(outputfilename, 'MPEG-4'); % New
newVid.FrameRate = fps;
newVid.Quality = 100;
open(newVid);
for ...
% Rest of your code here

BTW, have a look at the documentation in the future. It clearly shows you what to do if you want to save to a new format, and not AVI: https://www.mathworks.com/help/matlab/ref/videowriter.html#input_argument_d0e1094625

Trek answered 10/8, 2017 at 19:35 Comment(2)
thanks! I looked at the documentation actually but wasn't clear exactly where does the mpeg4 come in.Airing
@Airing No problem! I'll admit that it's a bit confusing. I just know from experience that what you do above is what you need to do :).Trek
L
2

You need to pass a profile argument to matlab's videowriter.

From Matlab Help, VideoWriter(filename,profile) creates a VideoWriter object and applies a set of properties tailored to a specific file format (such as 'MPEG-4' or 'Uncompressed AVI').

In your case, you need to pass the string MPEG-4 to the profile argument.

Liz answered 10/8, 2017 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.