【问题标题】:Play video file from certain timestamps in Matlab?在 Matlab 中播放某些时间戳的视频文件?
【发布时间】:2015-06-04 00:14:06
【问题描述】:

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

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

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

【问题讨论】:

  • 我不确定我是否听懂了你的问题。 “从 3 秒开始播放”是什么意思?你想有 3 秒的空白帧,然后开始播放视频吗?
  • 我想点击播放并让它从 t=3sec 开始播放,就像我将 t=1m14s 放在我的 youtube 链接末尾,它会从视频开始 1m14 秒开始播放。
  • 好的。我似乎不明白你想做什么。您是否编写了代码来帮助我们了解您迄今为止所做的尝试?

标签: matlab video video-streaming video-processing


【解决方案1】:

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

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

【讨论】:

  • 什么是抽样因子?
  • 通过更改采样因子并运行代码。您将了解采样因子。
  • 你为什么不给出一个描述抽样因子是什么的一句话答案?...除非你自己不知道?我不想找到视频文件,复制并粘贴此代码,然后运行它来弄清楚。如果你在它的作用上加上一个小句子,它会对你的帖子有所帮助。
  • 采样因子是给定时间段内记录的数据的样本数。假设总帧数为 160 帧,持续 4 秒。那么采样因子是 8 那么现在因子的总数是 160 / 8 = 20 。在捕获帧期间,read(xyloObj, (k-1)*sampling_factor+1) 在这个 (k-1)*sampling_factor+1 公式中计算帧索引。我认为这个解释就足够了。如果您需要更多告诉我,我会给出更多解释
  • @KathickRajan 所以它定义了播放帧率?或者,如果我以 30fps 的速度录制视频,并以 10Hz 的频率收集数据,我可以将采样因子设置为 10,从而将数据图表(我已动画绘制)与视频输出同步吗?
【解决方案2】:

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

所以你可以说

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2023-03-06
    • 1970-01-01
    • 2019-04-18
    • 2018-01-13
    • 1970-01-01
    • 2011-05-26
    相关资源
    最近更新 更多