2

我有 2 个视频我想在分屏中并排播放。它们具有相同的持续时间和维度。我发现了几年前开发的代码来完成这项工作。问题是,它充满了错误,可能是因为我使用的是较新的 Matlab 版本(2014a)。错误从 (%name of the new avi file) 开始。

任何人都可以尝试修复它:

% select two files:
[filename1,pathname1] = uigetfile('.avi','pick first AVI file');
[filename2,pathname2] = uigetfile('.avi','pick second AVI file');
file1 = fullfile(pathname1,filename1);
file2 = fullfile(pathname2,filename2);  
pdMovie1 = aviread(file1);
pdMovie2 = aviread(file2);
fileinfo1 = aviinfo(file1);
fileinfo2 = aviinfo(file2);

% check if AVI files have the same length and height:
if fileinfo1.NumFrames~=fileinfo2.NumFrames || ...
    fileinfo1.Height~=fileinfo2.Height
errordlg('files are not compatible!')
else
% inspired by Herbert Ramoser in Message-ID:
% <art0c0$l9fip$1@ID-148798.news.dfncis.de>
for i=1:size(pdMovie1,2)
  output(i).cdata = [pdMovie1(i).cdata, pdMovie2(i).cdata];
  output(i).colormap = pdMovie1(i).colormap;
end;

% name of the new avi file:
[pathstr,name,ext,versn] = fileparts(filename1);
newmoviename = [pathname1,name,'_combined', ...
                num2str(fileinfo1.FramesPerSecond;),ext];

% create the avi file:
movie2avi(output, newmoviename, ...
          'fps', fileinfo1.FramesPerSecond;, ...
          'compression', 'none');
close
end
4

2 回答 2

5

如果只是为了并排播放视频,这个更简单的代码可以工作,

close all
clc
clear

vid1 = vision.VideoFileReader('video1.avi');
vid2 = vision.VideoFileReader('video2.avi');
vidP = vision.VideoPlayer;

while ~isDone(vid1)
   frame1 = step(vid1);
   frame2 = step(vid2);

   frame = horzcat(frame1, frame2);

   step(vidP,frame);
end

release(vid1);
release(vid2);
release(vidP);

更新:我假设两个输入视频具有相同的长度和帧尺寸。

好的,现在如果你想从前2个开始录制一个新的视频,和之前的帧率一样,最好使用下面的代码,

close all
clc
clear

vid1 = VideoReader('video1.avi');
vid2 = VideoReader('video2.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);
于 2015-06-11T16:55:01.213 回答
0

如果您有两个具有不同帧速率和分辨率的视频剪辑,或者您想加入/剪切成对的视频剪辑,请使用下面的代码。

例如:我的行车记录仪能够同时在汽车的前部和后部录制视频。但是前后视频是分开的文件。我想将它们并排展示,并将 6 个文件(3 对)合并到一个文件中。

%Read video
clc
clear
vidObj_f = VideoReader('video1.mp4');
FrameRate_f = vidObj_f.FrameRate;
vidObj_b = VideoReader('video2.mp4');
FrameRate_b = vidObj_b.FrameRate;

%New video
outputVideo = VideoWriter('combinedvideo.avi');
outputVideo.FrameRate = 24;
open(outputVideo);

skip = 95; %first seconds
j=skip*24;
try
while 1
    front = read(vidObj_f,1+round(j*FrameRate_f*1/24));
    back = read(vidObj_b,1+round(j*FrameRate_b*1/24));
    front = imresize(front,[720 1280]);
    videoframe = [front;back];
    writeVideo(outputVideo, videoframe);
    j = j+1;
end
catch
    disp('...end 1');
end

vidObj_f = VideoReader('video3.mp4');
FrameRate_f = vidObj_f.FrameRate;
vidObj_b = VideoReader('video4.mp4');
FrameRate_b = vidObj_b.FrameRate;

skip = 0; %first seconds
j=skip*24;
try
while 1
    front = read(vidObj_f,1+round(j*FrameRate_f*1/24));
    back = read(vidObj_b,1+round(j*FrameRate_b*1/24));
    front = imresize(front,[720 1280]);
    videoframe = [front;back];
    writeVideo(outputVideo, videoframe);
    j = j+1;
end
catch
    disp('...end 2');
end

vidObj_f = VideoReader('video5.mp4');
FrameRate_f = vidObj_f.FrameRate;
vidObj_b = VideoReader('video6.mp4');
FrameRate_b = vidObj_b.FrameRate;

skip = 0; %first seconds
j=skip*24;
cut = 30; %after playing 'cut' seconds
try
while 1
    front = read(vidObj_f,1+round(j*FrameRate_f*1/24));
    back = read(vidObj_b,1+round(j*FrameRate_b*1/24));
    front = imresize(front,[720 1280]);
    videoframe = [front;back];
    writeVideo(outputVideo, videoframe);
    if j>cut*24
        disp('...end 3 (cut)');
        break
    end
    j = j+1;

end
catch
    disp('...end 3');
end

close(outputVideo);
于 2016-07-16T10:54:43.110 回答