Extracting audio from video in matlab. The audio is de-accelerated - matlab

The input video length is 1min 56sec and Ouput audio length comes out to be 2 min 47 sec
file1='vipmen1.wav'; %o/p file name
hmfr=video.MultimediaFileReader(file_fullpath,'AudioOutputPort',true,'VideoOutputPort',false);
hmfw = video.MultimediaFileWriter(file1,'AudioInputPort',true,'FileFormat','WAV');
while ~isDone(hmfr)
audioFrame = step(hmfr);
step(hmfw,audioFrame);
end
close(hmfw);
close(hmfr);

You have to use the same sample rate for your output. Read the sample rate from the input and use this rate to write the output.

Related

MATLAB *.pcm to *.wav converting

How can I convert *.pcm audio file to *.wav audio File in MATLAB-Code?
I just need to insert a header, but how it is work?
Thank you very much!
Since you didn't specify it, I'm assuming you are using Matlab 2018b, so I will point you to the most recent documentation about audioread:
As you can see, PCM is not on the supported format list.
You should try to look if you can parameter your AudioRecorder to record your audio to another format within the ones in the supported list: .wav, .ogg, .flac, .au, .aif, .aifc, mp3, .mp4...
An alternative option, without using audioread, would be to import pcm data like any other data file, then convert it to 16 bit wav. I assume sample rate is 44100 Hz.
fid = fopen('audioFile.pcm'); % Open raw pcm file
audio = int16(fread(fid, Inf, 'int16')); % Convert data into 16 bit
fclose(fid); % Close pcm file
audiowrite('audioFile.wav', audio, 44100,'BitsPerSample', 16); % Write wav

How to get the length of a time vector in seconds?

I load an audio file and get sample and sampleRate with
[sample, sampleRate] = audioread(audiofile);
I want to get the length of the audio in seconds, how do I proceed?
Having the sampleRate, the inverse of it is the sampling time.
Ts=1/sampleRate;
So the time vector for your sampled data would be
time=(0:(length(sample)-1))*Ts;

Segmented speech data, save it as a .wav file and play it in Matlab

I have speech data, which I segment into some parts. I am trying to create a .wav file for each segmented data and play this .wav file.
For example, suppose the speech data is an 1x1000 array called data. I segment this data into 4 parts using the indices of seg_data.
seg_data
1 250
251 500
501 750
751 1000
Code:
for i=1:size(seg_data(:,1))
w(i)=data(seg_data(i,1): seg_data(i,2));
wavwrite(w(i),'file_%d \n ',i);
end
First thing I need to create one folder in which the .wav file that is created in every iteration is stored. Then, I can read the stored file one by one and can play it.
If you don't know the sampling frequency Fs, you have to use the signature wavwrite(y,filename) as presented in the reference site.
for i=1:size(seg_data(:,1))
w(i,:)=data(seg_data(i,1): seg_data(i,2));
wavwrite(w(i,:),['file_',num2str(i)]);
end
BTW, the documentation suggests to use audiowrite instead.

fft on samples of an audio file in matlab

I'm trying to extract information from a sound file in order to use it in a video classification algorithm I'm working on.
My problem is that I don't know how to work exactly with audio files in Matlab.
Below is what I need to accomplish:
open the audio file and get the sampling rate/frequency
I need to work on a window of 2 seconds so I have to loop over the file and get each 2 seconds as a window and then do the ftt (Fast-Fourier-Transform) on each window.
After that it is my turn to use these values to do what I want
any help would be appreciated
Thanks.
Following code may only give you some idea. You may need to determine another fft size, a windowing function like hamming etc.
To read a wav file:
[data, Fs] = wavread('path.wav');
Splitting to 2 sec windows and getting fft:
frameFFT = [];
timeStep = Fs*2;
for i=1:timeStep:length(data)-timeStep
frameFFT = [frameFFT; fft(data(i:i+timeStep-1),1024)];
end

Continuous Video Recording in Matlab, Saving/Restarting On a Memory Cap, Ending on a Flag

I would like to record a continuous video in Matlab until some other flag changes, allowing matlab to continue performing other tasks during video acquisition (like deciding whether or not the flag should be set). Since these recordings could last upwards of 3 hours, perhaps closing the recording every hour, writing to a file video_1, then recording for another hour and dumping to video_2, etc for as long as the flag isn't set. However, from what I've seen using Matlab's Image Processing Toolbox, you have to specify some kind of number of frames to capture, or frames per trigger, etc. I'm not really too sure how to proceed.
Some simple code to record video I have is:
% create video obj
video = videoinput('winvideo',1);
% create writer obj
writerObj = VideoWriter('output.avi');
% set video properties
video.LoggingMode = 'disk';
video.DiskLogger = writerObj;
% start recording video
start(video);
% wait
wait(video, inf)
% save video
close(video.DiskLogger);
delete(video);
clear video;
However, the output video is only .3 seconds long. I've followed the following tutorial to get a 30 second recording down to a 3 second video available here but I can't figure out how to make it go on continuously.
Any help would be appreciated!
aviObject = avifile('myVideo.avi'); % Create a new AVI file
for iFrame = 1:100 % Capture 100 frames
% ...
% You would capture a single image I from your webcam here
% ...
F = im2frame(I); % Convert I to a movie frame
aviObject = addframe(aviObject,F); % Add the frame to the AVI file
end
aviObject = close(aviObject); % Close the AVI file
source: How do I record video from a webcam in MATLAB?