Question

I have a folder of jpg files and I want to make them into a movie. I am using this script:

% Create video out of list of jpgs
clear 
clc

% Folder with all the image files you want to create a movie from, choose this folder using:
ImagesFolder = uigetdir;

% Verify that all the images are in the correct time order, this could be useful if you were using any kind of time lapse photography. We can do that by using dir to map our images and create a structure with information on each file.
jpegFiles = dir(strcat(ImagesFolder,'\*.jpg'));

% Sort by date from the datenum information.
S = [jpegFiles(:).datenum]; 
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% The sub-structures within jpegFilesS is now sorted in ascending time order.
% Notice that datenum is a serial date number, for example, if you would like to get the time difference in hours between two images you need to subtract their datenum values and multiply by 1440.

% Create a VideoWriter object, in order to write video data to an .avi file using a jpeg compression.
VideoFile = strcat(ImagesFolder,'\MyVideo');
writerObj = VideoWriter(VideoFile);

% Define the video frames per second speed (fps)
fps = 1; 
writerObj.FrameRate = fps;

% Open file for writing video data
open(writerObj);

% Running over all the files, converting them to movie frames using im2frame and writing the video data to file using writeVideo
for t = 1:length(jpegFilesS)
     Frame = imread(strcat(ImagesFolder,'\',jpegFilesS(t).name));
     writeVideo(writerObj,im2frame(Frame));
end

% Close the file after writing the video data 
close(writerObj);

(Courtesy of http://imageprocessingblog.com/how-to-create-a-video-from-image-files/)

But it gives me this error:

Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter.VideoWriter>VideoWriter.close at 289
In Movie_jpgCompilation at 37

I'm sure my jpg files are fine, and they are in the folder I specify. What is the problem?

Was it helpful?

Solution

(This is my first post ever, so I hope it helps).

If you're on Linux, don't the backslashes need to be forward slashes? When I ran it on my Mac, my jpegFiles was an empty Struct. When I changed them around it worked:

% Create video out of list of jpgs
clear 
clc

% Folder with all the image files you want to create a movie from, choose this folder using:
ImagesFolder = uigetdir;

% Verify that all the images are in the correct time order, this could be useful if you were using any kind of time lapse photography. We can do that by using dir to map our images and create a structure with information on each file.
jpegFiles = dir(strcat(ImagesFolder,'/*.jpg'));

% Sort by date from the datenum information.
S = [jpegFiles(:).datenum]; 
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% The sub-structures within jpegFilesS is now sorted in ascending time order.
% Notice that datenum is a serial date number, for example, if you would like to get the time difference in hours between two images you need to subtract their datenum values and multiply by 1440.

% Create a VideoWriter object, in order to write video data to an .avi file using a jpeg compression.
VideoFile = strcat(ImagesFolder,'/MyVideo.avi');
writerObj = VideoWriter(VideoFile);

% Define the video frames per second speed (fps)
fps = 1; 
writerObj.FrameRate = fps;

% Open file for writing video data
open(writerObj);

% Running over all the files, converting them to movie frames using im2frame and writing the video data to file using writeVideo
for t = 1:length(jpegFilesS)
     Frame = imread(strcat(ImagesFolder,'/',jpegFilesS(t).name));
     writeVideo(writerObj,im2frame(Frame));
end

% Close the file after writing the video data 
close(writerObj);

Edit: You can also use filesep so that the file separator is OS-specific. http://www.mathworks.com/help/matlab/ref/filesep.html

OTHER TIPS

It would be simpler to use Windows Movie Maker [windows] or iMovie [mac]. For your purposes though you should use PowerPoint.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top