简体   繁体   中英

Convert MP3 to WAV

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"])

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM