How can we merge two separate video files into one?

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);

 Akzeptierte Antwort

Image Analyst
Image Analyst am 12 Sep. 2020
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);

9 Kommentare

Parham Babakhani Dehkordi
Parham Babakhani Dehkordi am 13 Sep. 2020
Bearbeitet: Parham Babakhani Dehkordi am 13 Sep. 2020
Thank you very much. By resizing the Frames before merging, the problem was solved. However, two videos are being shown simultaneously side by side. How can I force the code to run the first video, and when it is finished, the second video is shown in continuation of the first one.?
Have two separate loops, first one that reads the first input video and copies it to the output video, then another loop that does the same thing with the second video.
vid1 = VideoReader('first.avi');
% new video
outputVideo = VideoWriter('newvideo.avi');
outputVideo.FrameRate = vid1.FrameRate;
open(outputVideo);
while hasFrame(vid1)
img1 = readFrame(vid1); % play video
imgt = img1;
% record new video
writeVideo(outputVideo, imgt);
end
[rows1, columns1, numColors1] = size(img1);
vid2 = VideoReader('second.avi');
while hasFrame(vid2)
img2 = readFrame(vid2); % play video
imgt = imresize(img2, rows1, columns1);
% record new video
writeVideo(outputVideo, imgt);
end
close(outputVideo);
Dear Walter,
regrding your last code, still not working. The error is as follows:
Error using imresize>parsePreMethodArgs (line
381)
Invalid input syntax; input argument # 3 is
unrecognized
Error in imresize>parseInputs (line 260)
[params.A, params.map, params.scale,
params.output_size] = ...
Error in imresize (line 146)
params = parseInputs(args{:});
Error in combined_video (line 53)
imgt = imresize(img2, rows1, columns1);
imgt = imresize(img2, [rows1, columns1]);
Hello Walter,
I tried your code above and it works perfectly. However, audio is not included. Is there a way include it?
Thanks for your help!
Sven
You need the Computer Vision Toolbox in order to write combined audio and video.
You cannot switch audio rates in the middle of a file, so you would need to examine the audio rates of both files in advance and decide which rate you are going to convert the samples to.
You should also be interpolating video frame rates since you cannot change frame rates in the middle of writing. At the moment I am not sure what a good video frame rate conversion method is.
Thank you for your answer Walter! I will give it a try! :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 12 Sep. 2020

0 Stimmen

You cannot change the size of the frame in the middle of a video.
You will need to do one of the following:
  • pad the smaller frames to be the same size as the larger
  • crop the larger frames to be the same size as the smaller
  • imresize the smaller frames to be the same size as the larger
  • imresize the larger frames to be the same size as the smaller
  • imresize() all frames to be a fixed size
Note that imresize() distorts proportions unless the scalling factor happens to be the same horizontally and vertically (same aspect ratio).
If you pad you probably want to center the image rather than padding only on one edge. https://www.mathworks.com/help/images/ref/padarray.html

Kategorien

Mehr zu Audio I/O and Waveform Generation finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by