读入带音频的mp4视频文件,编辑帧并将其写入带音频的新文件

时间:2019-04-16 21:48:54

标签: matlab video-processing

我正在MATLAB中读取filename.mp4视频文件。我想编辑图像,但是我想保持音频完整。使用VideoReaderVideoWriter仅将图像分开。我使用了vision.VideoFileReader和'vision.VideoFileWriter'。我阅读了视频和音频文件,然后拍摄图像并在其旁边添加图片。然后写框架和与之关联的音频。最后的视频显示了我添加的图片,而不显示原始图像。任何帮助表示赞赏。

v = VideoReader('movie.mp4');
nfr = v.NumberofFrames;
clear v;
vR = vision.VideoFileReader('movie.mp4','AudioOutputPort',1);
fr = vR.info.VideoFrameRate;
vW = vision.VideoFileWriter('filename.avi','AudioInputPort',1,'FrameRate',fr);

pic = imread('picture.png');%read picture
[a1,b1,~] = size(pic);% get picture size to be resized.


for i = 1:nfr
    [I,audio] = vR();
    I = permute(I,[2,1,3]);%rotate 90 degrees
    if i == 1%resize the picture
        [a,b,~] = size(I);
        pic = imresize(pic,[a,a/a1*b1]);
    end
    I = [I pic];%combine picture and movie frame
    vW(I,audio);%write frame and audio
end
release(vR);
release(vW);

1 个答案:

答案 0 :(得分:0)

我知道了。

v = VideoReader('movie.mp4');
nfr = v.NumberofFrames;
clear v;
vR = vision.VideoFileReader('movie.mp4','AudioOutputPort',1,'VideoOutDataType','uint8');
%default of VideoOutDataType is 'single', converting it to a similar format is essential
fr = vR.info.VideoFrameRate;
vW = vision.VideoFileWriter('filename.avi','AudioInputPort',1,'FrameRate',fr);

pic = imread('picture.png');%read picture
[a1,b1,~] = size(pic);% get picture size to be resized.


for i = 1:nfr
    [I,audio] = vR();
    I = permute(I,[2,1,3]);%rotate 90 degrees
    if i == 1%resize the picture
        [a,b,~] = size(I);
        pic = imresize(pic,[a,a/a1*b1]);%resizing the pic to same height as movie frame %with proportional width
    end
    I = [I pic];%combine picture and movie frame
    vW(I,audio);%write frame and audio
end
release(vR);
release(vW);