2017-03-10 91 views
0

我有一個視頻序列(格式爲Y4M),我想用相同的GoP大小將其分割爲七段。 GoP = 8; 我怎樣才能使用FFMPEG在MatLab中做到這一點?在MatLab上用ffmpeg將未壓縮的視頻分割成片段?

+0

所以,你想要每個片段8幀長?對不起,我不熟悉你的術語。 – Cecilia

+0

是的@Cecilia。 – SenDjasni

+0

你想使用FFMPEG的任何特定原因? Matlab有加載和處理視頻的其他方法。 – Cecilia

回答

1

在Matlab中表示視頻的一種標準方式是4D矩陣。尺寸是高x寬x顏色通道x框架。一旦你有矩陣,通過指定你想要的幀的範圍很容易獲得時間片。

例如,你可以抓住每次8幀在表示視頻的for循環

%Loads video as 4D matrix 
v = VideoReader('xylophone.mp4'); 
while hasFrame(v) 
    video = cat(4, video, readFrame(v)); 
end 

%iterate over the length of the movie with step size of 8 
for i=1:8:size(video, 4)-8 
    video_slice = video(:,:,:,i:i+7); %get the next 8 frames 

    % do something with the 8 frames here 

    % each frame is a slice across the 4th dimension 
    frame1 = video_slice(:,:,:,1); 
end 

%play movie 
implay(video) 

另一個最常用的方法是在一個結構陣列。您可以使用一定範圍的值來索引結構數組,以分割8個幀。我例子中的實際幀值存儲在結構元素cdata中。根據您的結構,元素可能會有不同的名稱;尋找具有3d矩陣值的元素。

% Loads video as structure 
load mri 
video = immovie(D,map); 
%iterate over the length of the movie with step size of 8 
for i=1:8:size(video, 4)-8 
    video_slice = video(i:i+7); %get the next 8 frames 

    % do something with the 8 frames here 

    % to access the frame values use cdata 
    frame1 = video_slice(1).cdata 
end 

%play movie 
implay(video) 

棘手的部分是你的視頻格式。 Matlab的VideoReader不支持Y4M,這是加載視頻最常用的方法。它也不支持FFmpeg Toolbox,它只提供幾種媒體格式(MP3,AAC,mpeg4,x264,動畫GIF)。

有跡象表明,尋找解決問題的對策其他幾個問題,包括

  1. how to read y4m video(get the frames) file in matlab
  2. How to read yuv videos in matlab?

我還要檢查the Matlab File Exchange,但我沒有親身經歷與任何這些方法。

+0

我會試試看看它是否解決了主要問題。謝謝@cecilia – SenDjasni

+0

另一個問題ID我可以,爲什麼一個4D矩陣? – SenDjasni

+0

Matlab是基於矩陣構建的,所以矩陣看起來像是一個自然的選擇,因爲視頻的元素(像素)是均勻的,密集的並且由數字表示。 – Cecilia