Reputation: 11
I'm trying to convert a bunch of mp4 files to avis using the code below. I know that the conversion works as I've verified it on single files. However when I try to read in all mp4s from a directory the 'files' struct I've tried to create is empty. I'm relatively new to programming in MatLab so I'm sure I'm making many silly errors. How can I properly load in and iterate over multiple mp4 files in order to convert them all to avis? Thanks!
NB: I have tried different methods of conversion, but MatLab seems to be the best at ensuring no size or quality distortion.
files = dir(fullfile('E:\Heather\Summer2013\*.mp4'));
for i = 1:length(files)
filename = files(i).name;
%create objects to read and write the video
readerObj = VideoReader('files(i).name.mp4');
writerObj = VideoWriter('files(i).name.avi','Uncompressed AVI');
%open AVI file for writing
open(writerObj);
%read and write each frame
for k = 1:readerObj.NumberOfFrames
img = read(readerObj,k);
writeVideo(writerObj,img);
end
close(writerObj);
end
Upvotes: 1
Views: 4805
Reputation: 30589
A call to dir
produces a struct array where file names are stored as strings in the name
field. That is, files(i).name
contains a string. To use it,
readerObj = VideoReader(files(i).name); % no quotes, no extension
To write with a different file extension, determine the file base with fileparts
and add the .avi extension. For example:
>> str = 'mymovie.mp4';
>> [~,fileBase,fileExt] = fileparts(str)
fileBase =
mymovie
fileExt =
.mp4
>> writerObj = VideoWriter([fileBase '.avi'],'Uncompressed AVI');
Upvotes: 2
Reputation: 36720
You are trying to read the file with the name ('files(i).name.mp4')
which obviously not exists. filename
should be the corret parameter for the reader, for the writer you have to replace the extension.
Upvotes: 0