在MATLAB中将音频和视频组合为视频文件

时间:2013-08-22 07:47:51

标签: matlab audio video video-processing matlab-cvst

我想在MATLAB中将音频和视频组合为视频文件。我写了以下代码: 但它给了我错误!?!谁能指导我?

[filename pathname]=uigetfile({'*.*'},'Video Selector');
fulpathname=strcat(pathname,filename);
videoFReader = vision.VideoFileReader(fulpathname);
[AUDIO,Fs] = audioread(fulpathname);
videoFWriter = vision.VideoFileWriter('myFile.avi','FrameRate',videoFReader.info.VideoFrameRate);

for i=1:50
videoFrame = step(videoFReader);
step(videoFWriter, videoFrame,AUDIO);
end

release(videoFReader);
release(videoFWriter);

4 个答案:

答案 0 :(得分:1)

使用'videoFReader.SampleRate'代替“videoFReader.info.VideoFrameRate”错误将被删除

答案 1 :(得分:1)

如果您想使用vision.VideoFileWriter同时编写音频和视频,则应将AudioInputPort选项设置为true。这默认为false,对象只需要输入视频数据。如果设置为true,则可以将视频和音频作为输入发送到步骤方法。

答案 2 :(得分:1)

编写音频和视频的示例


% It is assumed that audio is stored in "data" variable

% Idea is simple: Just divide length of the audio sample by the number of frames to be written in the video frames. ( it is equivalent to saying that what audio you   % want to have with that particular frame)

% First make AudioInputPort property true (by default this is false)

writerObj = vision.VideoFileWriter('Guitar.avi','AudioInputPort',true);

% total number of frames
nFrames   = 250;

% assign FrameRate (by default it is 30)

writerObj.FrameRate =  20;

% length of the audio to be put per frame

val = size(data,1)/nFrames;

% Read one frame at a time

for k = sf : nFrames
    % reading frames from a directory
    Frame=(imread(strcat('frame',num2str(k),'.jpg')));
    % adding the audio variable in the step function
    step(writerObj,Frame,data(val*(k-1)+1:val*k,:)); % it is 2 channel that is why I have put (:)

end

% release the video

release(writerObj)

答案 3 :(得分:0)

正如Navan回答的那样,你必须首先添加AudioInputPort。您的videoFrame必须是Frames的结构。音频也必须是与视频帧数相同长度的结构。您的音频采样率明显高于帧数。 为此,我建议您将音频样本数除以帧速率并将值四舍五入。 这些步骤对我有用。