【问题标题】:Combined audio and video as video file in MATLAB在 MATLAB 中将音频和视频组合为视频文件
【发布时间】:2013-08-22 07:47:51
【问题描述】:

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

【问题讨论】:

  • 哪个错误? (您似乎正在将音频读入“y”,然后没有对它做任何事情——“AUDIO”从何而来?)
  • 我很抱歉这个错误。 AUDIO 是需要与视频结合的音频文件。这里只想了解一下MATLAB中音视频是如何结合的。
  • @amir:到底是什么错误?
  • @MohammadIzady 使用 vision.VideoFileWriter/step 时出错 输入参数过多;预期 1(除了对象句柄),得到 2。

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


【解决方案1】:

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

【讨论】:

  • 为什么?他犯了什么错误?
  • 这应该是评论而不是答案。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    同时编写音频和视频的示例

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 在这些情况下添加一个实际示例总是有帮助的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多