how to merge 2 videos into 1 video without losing FPS?

9 views (last 30 days)
muhammad choudhry
muhammad choudhry on 31 Aug 2021
Answered: yanqi liu on 27 Sep 2021
Hi,
I have 2 x 10 mins videos, both with the 59.94 FPS. Since the videos holding the same experimental phenomena in 2 videos. I want to merge them two videos without losing any frame, (I achieve this by using the window 10 but losing my frame rate means losing data). I also have tried the code below but what this code is doing putting both videos side by side this is not what I am after. I am looking to merge 2 videos into 1. If anyone can help that be great!
Code:
close all; clear all; clc;
vid1 = VideoReader('test.avi');
vid2 = VideoReader('test1.avi');
videoPlayer = vision.VideoPlayer;
% new video
outputVideo = VideoWriter('finaltest.avi');
outputVideo.FrameRate = vid1.FrameRate;
open(outputVideo);
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);
while hasFrame(vid1) && hasFrame(vid2)
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);
% play video
step(videoPlayer, imgt);
% record new video
writeVideo(outputVideo, imgt);
end
release(videoPlayer);
close(outputVideo);

Answers (1)

yanqi liu
yanqi liu on 27 Sep 2021
sir, may be use the follows code to ref
clc
close all
clear all
vid1 = VideoReader('xylophone.mp4');
vid2 = VideoReader('traffic.avi');
% new video
outputVideo = VideoWriter('finaltest.avi');
outputVideo.FrameRate = vid1.FrameRate;
open(outputVideo);
sz1 = [vid1.Height vid1.Width];
sz2 = [vid2.Height vid2.Width];
while hasFrame(vid1)
img = readFrame(vid1);
writeVideo(outputVideo, img);
end
while hasFrame(vid2)
img = readFrame(vid2);
if ~isequal(sz1,sz2)
img=imresize(img,sz1,'bilinear');
end
writeVideo(outputVideo, img);
end
close(outputVideo);
% display new video
vidObj = VideoReader('finaltest.avi');
while(hasFrame(vidObj))
frame = readFrame(vidObj);
imshow(frame);
title(sprintf('Current Time = %.3f sec', vidObj.CurrentTime));
pause(2/vidObj.FrameRate);
end

Community Treasure Hunt

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

Start Hunting!

Translated by