2015-06-11 319 views
2

我有兩個視頻我想在分屏中並排播放。它們的持續時間和尺寸相同。我發現幾年前開發的代碼來完成這項工作。問題是,它充滿了錯誤,可能是由於我使用了更新的Matlab版本(2014a)。錯誤從(新的avi文件的%名稱)開始。Matlab - 將兩個視頻合併爲一個分屏視頻

誰能請嘗試修復它:

% 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: 
% <[email protected]> 
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

如果只是播放視頻並排側,這simplker代碼將工作,

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

謝謝,回放完美,但我怎麼把它保存爲.avi文件? – Mosawi

+0

我編輯了我的答案。 – SamuelNLP

+0

再次感謝,但現在我收到以下錯誤,它有可能被修復嗎?用於'VideoReader'類型的輸入參數的未定義函數'hasFrame'。 錯誤videosidebyside(第11行) 而同時hasFrame(VID1)&& hasFrame(VID2) – Mosawi

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); 
相關問題