50

Please, help to choose solution for converting any mp3 file to special .wav - I'm a newbie with Linux command line tools, so It's hard for me right now.

I need to get wav with 16khz mono 16bit sound properties from any mp3 file. I was trying

ffmpeg -i 111.mp3 -ab 16k out.wav,

but I got wav with the same rate as mp3 (22k).

Please, help to construct right command line

Alve
  • 1,315
  • 2
  • 17
  • 16

3 Answers3

108

kdazzle's solution is almost there - it still output a stereo wav, here is a slightly modified version that generate mono:

ffmpeg -i 111.mp3 -acodec pcm_s16le -ac 1 -ar 16000 out.wav

also, if this is for pre-processing speech data for sphinx 4 see here: Convert audio files for CMU Sphinx 4 input

Community
  • 1
  • 1
Bill
  • 1,271
  • 1
  • 10
  • 13
  • 1
    will this mix both channels or just split and use the 1st one? – Aquarius Power Dec 07 '14 at 20:51
  • 2
    This will mix the two channels into one - I just confirmed it. BTW, looks like on current Ubuntu 14.10, ffmpeg is now renamed to avconv – Bill Dec 08 '14 at 15:54
  • 3
    if needed, we must balance the channels before the mix as one may become too low volume, but in general it works great! – Aquarius Power Dec 08 '14 at 16:32
  • 2
    Here's some more info on how ffmpeg handles manipulating audio channels: https://trac.ffmpeg.org/wiki/AudioChannelManipulation – siannopollo Oct 21 '16 at 18:37
  • Can't get this working for some reason, the -ar changes Hz no problem, but -acodec pcm_s16e doesn't seem to do anything – Garglesoap Aug 10 '19 at 19:30
  • looks like current ffmpeg (4.1.3) defaults to pcm_s16le output audio codec - i just tested both versions (with or without -acodec pcm_s16le) and compared the output file formats to be the same. thanks for pointing this out! – Bill Aug 11 '19 at 01:54
10

Try this:

ffmpeg -i 111.mp3 -acodec pcm_s16le -ar 16000 out.wav
kdazzle
  • 4,190
  • 3
  • 24
  • 27
3

Use this example:

import os 
from pydub import AudioSegment
import numpy as np 
from tqdm import tqdm 

for src in tqdm (mp3_files):
    
    des = src.replace('.mp3','.wav')
    try:
        sound = AudioSegment.from_mp3(src)
        sound.set_channels(1)
        sound = sound.set_frame_rate(16000)                
        sound = sound.set_channels(1)    
        sound.export(des, format="wav")

    except:
        print(src)
        continue
Wesam Na
  • 2,364
  • 26
  • 23