Reputation: 7319
I have directories that each have several short (~10 second) .avi
videos. Does anybody know how I can concatenate all of the videos in a specific directory in alphabetical order to form one single video?
I would try to use VLC, but I have to do this for over a thousand different directories. I didn't realize this would be so difficult, but not able to find anything on Google.
More specifics:
For each directory I want to perform this action on, all videos are guaranteed to be:
.avi
,MJPG
,20fps
,640x480 resolution
,no audio
,between less than 1 second to 15 seconds long
I'd like the single video file to play just as if I played the individuals back-to-back.
If there's any other specifics I missed please let me know.
The combined videos are intended to all be put into the same directory and given to another person to perform their own video processing on with Matlab. They'll be doing something with either crosscorrelation or machine learning to try and identify a particular object in the videos.
Upvotes: 2
Views: 1614
Reputation: 3138
You can use a combination of the VideoReader
and VideoWriter
(see doc for more examples). Iterate through your video files in alphabetical order and "stream" them into a new file.
I threw together some (untested) code. I have no idea how fast this is, though:
cd(VIDEO_DIRECTORY);
tmp = dir('*.avi'); % all .avi video clips
videoList = {tmp.name}'; % sort this list if necessary! sort(videoList) might work
% create output in seperate folder (to avoid accidentally using it as input)
mkdir('output');
outputVideo = VideoWriter(fullfile(workingDir,'output/mergedVideo.avi'));
% if all clips are from the same source/have the same specifications
% just initialize with the settings of the first video in videoList
inputVideo_init = VideoReader(videoList{1}); % first video
outputVideo.FrameRate = inputVideo_init.FrameRate;
open(outputVideo) % >> open stream
% iterate over all videos you want to merge (e.g. in videoList)
for i = 1:length(videoList)
% select i-th clip (assumes they are in order in this list!)
inputVideo = VideoReader(videoList{i});
% -- stream your inputVideo into an outputVideo
while hasFrame(inputVideo)
writeVideo(outputVideo, readFrame(inputVideo));
end
end
close(outputVideo) % << close after having iterated through all videos
Upvotes: 2