从 Matlab 中的某些时间戳播放视频文件?

Play video file from certain timestamps in Matlab?

我正在尝试自定义我添加到 MatLab 的视频的播放,方法是将它们设置为从特定的起点开始播放,而不是从头开始播放。

通过使用 MathWorks 的 VideoReader,我可以确定目标起始帧、持续时间、帧速率等。

我如何告诉 MatLab 从 3 秒或 5 秒标记处开始播放我的视频?或者我选择的任何其他标记?

您可以使用 VideoReader 做一件事,您将获得包含帧和音频的整个视频。现在你像这样分开 让我们假设 10 秒的视频包含 200 帧,开始时间是 2 秒,结束时间是 4 秒。 所以 2 秒视频 = 40 帧和 4 秒视频 = 80 。现在为第 40 帧到第 80 帧放置一个循环,然后将其存储在临时变量中。然后使用电影播放该帧。我想下面的代码会为你所用。

sampling_factor = 8;
resizing_params = [100 120];

%%// Input video
xyloObj = VideoReader('xylophone.mpg');

%%// Setup other parameters
nFrames = floor(xyloObj.NumberOfFrame/sampling_factor); %%// xyloObj.NumberOfFrames;
vidHeight = resizing_params(1); %// xyloObj.Height;
vidWidth = resizing_params(1); %// xyloObj.Width;
% here i am play 4 sec movie to 2 to 3
info = get(xyloObj);
duration =info.Duration;
startframe =round( nFrames *2/duration); % 2 means starting duration in sec  
endframe = round( nFrames *4/duration); % 4 means ending duration in sec  
%// Preallocate movie structure.
temp(1:nFrames) = struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),'colormap',[]);
mov = temp(1:endframe-startframe) ;
indx =1;
%// Read one frame at a time.
for k = 1 :nFrames
    if k >=startframe && k <=endframe  
        IMG = read(xyloObj, (k-1)*sampling_factor+1);
    %// IMG = some_operation(IMG);
         mov(indx).cdata = imresize(IMG,[vidHeight vidWidth]);
         indx =indx +1;
    end    
end

%// Size a figure based on the video's width and height.
hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight])

%// Play back the movie once at the video's frame rate.
movie(hf, mov, 1, xyloObj.FrameRate);

如果您是 运行 14b 或更高版本,VideoReader 有一个名为 CurrentTime 的 属性 您可以设置。

所以你可以说

vidObj = VideoReader('xylophone.mpg');
vidObj.CurrentTime = 2; % 2 seconds;
readFrame(vidObj);

希望对您有所帮助。