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



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

如果您想使用视觉编写音频和视频。VideoFileWriter您应该将AudioInputPort选项设置为true。默认情况下,这是false,并且对象只期望输入视频数据。若设置为true,则可以将视频和音频作为输入发送到步骤方法。

编写音频和视频的示例


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

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

当Navan回答时,您必须首先将AudioInputPort添加到ture中。您的视频帧必须是帧的结构。音频也必须是与"视频帧数"长度相同的结构。您的音频采样率将明显大于帧数。为此,我建议您将音频样本的数量除以帧速率,并对该值进行四舍五入。这些步骤对我有效。

最新更新