amir nemat
amir nemat

Reputation: 105

Combined audio and video as video file in MATLAB

I want to combine audio and video as video file in MATLAB. I have wrote following code: But it gives me error !?! can anyone guide me ?

[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);

Upvotes: 2

Views: 7873

Answers (4)

Akshay Bhandari
Akshay Bhandari

Reputation: 21

Example for writing both audio and video


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

Upvotes: 2

Abhay Prasad
Abhay Prasad

Reputation: 9

As Navan answered you have to add in the AudioInputPort to ture first. Your videoFrame has to be a structure of Frames. The audio must also be a structure of the same length as the Number of Video frames. Your audio sampling rate will be obviously more than the number of frames. For this i suggest you to divide the number of audio samples by the frame rate and round off the value. These steps works for me.

Upvotes: 0

Navan
Navan

Reputation: 4477

If you want to write both audio and video using vision.VideoFileWriter you should set AudioInputPort option to true. This is by default false and the object expects only video data input. If you set to true, then you can send both video and audio as inputs to step method.

Upvotes: 2

Ankur Mali
Ankur Mali

Reputation: 11

use 'videoFReader.SampleRate' instead of "videoFReader.info.VideoFrameRate" the error will be removed

Upvotes: 1

Related Questions