0

I want to convert mp3 files to wav 16khz (these are files from Mozilla Common Voice)

My function

def one_sample(mp3_filename):
    """ Take an audio file, and optionally convert it to 16kHz WAV """
    if not os.path.splitext(mp3_filename.lower())[1] == ".mp3":
        mp3_filename += ".mp3"
    # Storing wav files next to the mp3 ones - just with a different suffix
    wav_filename = os.path.splitext(mp3_filename)[0] + ".wav"
    if not os.path.exists(wav_filename):
        transformer = sox.Transformer()
        transformer.convert(samplerate=SAMPLE_RATE, n_channels=CHANNELS)
        try:
            transformer.build(mp3_filename, wav_filename)
        except sox.core.SoxError:
            pass

I get an error for all files:

Traceback (most recent call last):
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sox\core.py", line 147, in soxi
    shell_output = subprocess.check_output(
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['sox', '--i', '-c', 'clips/common_voice_ru_23425218.mp3']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/dip/ru/fox.py", line 93, in <module>
    one_sample(path4)
  File "D:/dip/ru/fox.py", line 17, in one_sample
    _maybe_convert_wav(mp3_filename, wav_filename)
  File "D:/dip/ru/fox.py", line 57, in _maybe_convert_wav
    transformer.build(mp3_filename, wav_filename)
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sox\transform.py", line 593, in build
    input_format, input_filepath = self._parse_inputs(
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sox\transform.py", line 496, in _parse_inputs
    input_format['channels'] = file_info.channels(input_filepath)
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sox\file_info.py", line 82, in channels
    output = soxi(input_filepath, 'c')
  File "C:\Users\Ksavich\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sox\core.py", line 153, in soxi
    raise SoxiError("SoXI failed with exit code {}".format(cpe.returncode))
sox.core.SoxiError: SoXI failed with exit code 1

Process finished with exit code 1

I downloaded sox-14-4-2 and added it to the PATH And I use PyCharm

sun_say
  • 89
  • 10

2 Answers2

0
  1. First reason
  1. Second - need full path of file
sun_say
  • 89
  • 10
0

Try running sox from the command line. It will give you more informative errors.

$ sox --i -c data/stereo-test.mp3 

In my case, it told me I didn't have the libraries for mp3 files:

$ sox --i -c data/stereo-test.mp3 
sox FAIL formats: no handler for file extension `mp3'

On a Windows system, you'll have to install the correct DLLs.

On a linux system:

$ sudo apt-get install libsox-fmt-mp3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libsox-fmt-mp3
0 upgraded, 1 newly installed, 0 to remove and 133 not upgraded.
Need to get 56.6 kB of archives.
After this operation, 96.3 kB of additional disk space will be used.
Get:1 http://mirrors.ocf.berkeley.edu/raspbian/raspbian buster/main armhf libsox-fmt-mp3 armhf 14.4.2+git20190427-1 [56.6 kB]
Fetched 56.6 kB in 1s (71.8 kB/s)   
Selecting previously unselected package libsox-fmt-mp3:armhf.
(Reading database ... 103401 files and directories currently installed.)
Preparing to unpack .../libsox-fmt-mp3_14.4.2+git20190427-1_armhf.deb ...
Unpacking libsox-fmt-mp3:armhf (14.4.2+git20190427-1) ...
Setting up libsox-fmt-mp3:armhf (14.4.2+git20190427-1) ...

After that, I got more satisfying results:

$ sox --i -c data/stereo-test.mp3 
2
Wes Modes
  • 2,024
  • 2
  • 22
  • 40