2

我想在 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

4 回答 4

1

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

于 2013-10-24T15:34:00.187 回答
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)
于 2015-04-14T08:18:12.313 回答
1

使用“videoFReader.SampleRate”而不是“videoFReader.info.VideoFrameRate”错误将被删除

于 2013-09-07T02:20:49.597 回答
0

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

于 2014-02-06T18:54:24.273 回答