MATLAB: How can we merge two separate video files into one

merging videovideovideo processing

Hello,
I have two separate video .avi files named as 'first' and 'second'. The first video contains 13 frames, and second video includes 18 frames. I need to combine these two video into one single file so that the first video comes first, followed by the second one with frame rate of 0.4.
I use the following code, however, the error said that 'Dimensions of arrays being concatenated are not consistent', which emphasize that two input files do not have the same dimensions, any help would be appreciated.
vid1 = VideoReader('first.avi');
vid2 = VideoReader('second.avi');
videoPlayer = vision.VideoPlayer;
% new video
outputVideo = VideoWriter('newvideo.avi');
outputVideo.FrameRate = vid1.FrameRate;
open(outputVideo);
while hasFrame(vid1) && hasFrame(vid2)
img1 = readFrame(vid1);
img2 = readFrame(vid2);
imgt = horzcat(img1, img2);
% play video
step(videoPlayer, imgt);
% record new video
writeVideo(outputVideo, imgt);
end
release(videoPlayer);
close(outputVideo);

Best Answer

Try calling imresize() to make them the same size:
img1 = readFrame(vid1);
[rows1, columns1, numColors1] = size(img1);
img2 = readFrame(vid2);
[rows2, columns2, numColors2] = size(img1);
img2 = imresize(img2, [rows1, rows2]);
imgt = horzcat(img1, img2);
Related Question