convert file from .wav to .mp3

30 views (last 30 days)
Niranjan Satam
Niranjan Satam on 25 Mar 2013
I am using following code to extract just the audio in .wav format from a video in .avi format
file='take.avi';
file1='vipmen1.wav'; %o/p file name
hmfr=video.MultimediaFileReader(file,'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);
Then i am using the following code in gui for conversion :
global pl;
global sf;
[FileName,PathName]=uigetfile('*.wav')
myFile=[PathName,FileName]
[mySong,sf]=wavread(myFile);
mp3write(mySong,sf,FileName)
it gives no error whatsoever but the mp3 file is not to be found.
Its not there in the working directory and the inital .wav file still is in same format.

Answers (1)

Jan
Jan on 25 Mar 2013
Edited: Jan on 26 Mar 2013
[FileName,PathName]=uigetfile('*.wav')
myFile=[PathName,FileName]
[mySong,sf]=wavread(myFile);
mp3write(mySong,sf,FileName)
Is this mp3write from the FileExchange? Please do not let us guess such important details. This function creates the file "Default_name.mp3" as default, when it is called with 3 inputs only.
What is the current directory during writing the file? Using absolute file names would be safer:
[dummy, Name] = fileparts(FileName);
mp3write(mySong, sf, FileName, fullfile(PathName, [Name, '.mp3']);
A general method to solve such problems is the debugger: Set a breakpoint in the line, which calls mp3write, then step into this function and see, what's going on.
  4 Comments
Niranjan Satam
Niranjan Satam on 26 Mar 2013
Jan Simon,
Apparently there is no Default_name.mp3 in that code so obviously i was using the wrong file.
I am now using mp3write from following link
with this file, following was my main program
global pl;
global sf;
[FileName,PathName]=uigetfile('*.wav')
myFile=[PathName,FileName]
[mySong,sf]=wavread(myFile);
mp3write(mySong, sf,16);
Now a second file with the same name however with an explicitly visible .wav extension (common music files of .wav format do not show the extension explicitly) is formed in the same directory.When i hover the cursor,it reads MP3 format sound with size 0 bytes.The most important part is it shows fatal error in command window.Following is the command window output :
FileName =
vipmen1.wav
PathName =
C:\Users\Satam\Documents\MATLAB\
myFile =
C:\Users\Satam\Documents\MATLAB\vipmen1.wav
File name = Default_name.mp3
Fixed bit rate, joint-stereo, 128 kb/s encoding
fatal error during initialization
Please note the temp file is formed and deleted as written in the code. Absolute naming wouldn't be necessary now since the file is formed in same directory.Am i correct on this one?
Niranjan Satam
Niranjan Satam on 27 Mar 2013
Jan Simon,
I did find a way around this. All i did was go to the command window:
=>dos('cmd') =>lame -h vipemen1.wav output.mp3
this seems to work out for me. Thank you for all your help.Aprreciate it.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!