Reputation: 13
I am trying to convert .mp3 file to .wav format. when I am using below command in "command prompt" I am able to get desired output. it gives me output file in .wav format
sox "C:\Users\Desktop\Audio File\Call.mp3" --rate 16k --bits 16 --channels 1 "C:\Users\Mayank\Desktop\Audio File\Call.wav"
I tried to do the same thru Python. Below is the code for that :
import subprocess
retcode = subprocess.call(['sox', 'C:\Users\Desktop\Audio File\Call.mp3',
'--rate 16k', '--bits 16', '--channels 1',
'C:\Users\Desktop\Audio File\Call.wav'])
I am getting below error while doing so. I am new with python, please let me know how to achieve this :
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
I tried using double "\" instead of "\" then I am getting below error
FileNotFoundError: [WinError 2] The system cannot find the file specified
I tried all below options also.. all of them gives same error "cannot find the file specified"..
import subprocess
retcode = subprocess.call(['sox', "C:\\Users\\Desktop\\Audio File\\Call.mp3",
'--rate 16k', '--bits 16', '--channels 1',
"C:\\Users\\Desktop\\Audio File\\Call.wav"])
import subprocess
retcode = subprocess.call(['sox', r'C:\Users\Desktop\Audio File\Call.mp3',
'--rate 16k', '--bits 16', '--channels 1',
r'C:\Users\Desktop\Audio File\Call.wav'])
import subprocess
retcode = subprocess.call(['sox', "C:/Users/Mayank/Desktop/Audio File/Call.mp3",
'--rate 16k', '--bits 16', '--channels 1',
"C:/Users/Desktop/Audio File/Call.wav"])
Upvotes: 1
Views: 8613
Reputation: 41
sox doesn't support mp3 to wav conversion. To convert mp3 to wav you can the below mentioned commands. It will work perfectly.
pip install pydub
apt-get install ffmpeg
MP3 to WAV conversion
from os import path
from pydub import AudioSegment
# files
src = "transcript.mp3"
dst = "test.wav"
# convert wav to mp3
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")
check out this link if it doesn't work
Upvotes: 3
Reputation: 13
at last it worked as below
import subprocess
sox = 'C:/Program Files (x86)/sox-14-4-2/sox.exe'
infile = 'C:/Users/Desktop/Audio File/Call.mp3'
outfile = 'C:/Users/Desktop/Audio File/Call.wav'
extra = '--rate 16k --bits 16 --channels 1'
command = """{0} "{1}" {2} "{3}" """.format(sox,infile,extra,outfile)
subprocess.call(command)
Upvotes: 0