2013-08-22 123 views
2

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

哪些错误? (您似乎将音频读入“y”,然后不做任何操作 - “AUDIO”从哪里来?) – nkjt

+0

我很抱歉发生这个错误。 AUDIO是需要与视频结合的音频文件。在这里我只是想知道如何在MATLAB中结合音频和视频。 –

+1

@amir:究竟是什么错误? –

回答

1

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

+1

为什么?他犯了什么错误? –

+1

这应该是一个评论而不是答案。 – neuronet

1

如果你想写音频和视频使用vision.VideoFileWriter你应该AudioInputPort选项设置为真正。这是默认的错误,对象仅需要视频数据输入。如果设置为true,则可以将视频和音频作为输入发送到步骤方法。

0

由于Navan的回答是你必须首先在AudioInputPort中添加。你的视频帧必须是帧的结构。音频也必须是与视频帧数相同长度的结构。您的音频采样率将明显高于帧数。 为此,我建议您将音频采样的数量除以帧频并舍入值。 这些步骤适用于我。

+0

添加一个实际的例子总是有助于在这些情况下。 – nKn

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)