首页 文章

在MatLab上使用ffmpeg将未压缩的视频拆分为段?

提问于
浏览
0

我有一个视频序列(格式Y4M),我想将它分成具有相同GoP大小的sevral段 . GoP = 8;我如何使用FFMPEG在MatLab中做到这一点?

1 回答

  • 1

    在Matlab中表示视频的一种标准方法是4D矩阵 . 尺寸为高x宽x颜色通道x帧 . 一旦有了矩阵,就可以通过指定所需的帧范围来轻松获取时间片 .

    例如,您可以在for循环中一次抓取8个帧

    %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) .

    还有一些其他问题需要寻找这个问题的解决方案,包括

    我也会检查the Matlab File Exchange,但我没有任何这些方法的个人经验 .

相关问题