Convert between audio formats
Convert in CLI
sox new.wav new.mp3
Convert to a different bit depth and sampling rate
ffmpeg -i input.wav -ac 1 -ar 16000 -sample_fmt s16 output.wav
Available bit depth optons:
ffmpeg -sample_fmts
Convert in Python
import subprocess
in_file = "test.webm"
out_file = "test.wav"
subprocess.run(
[
"ffmpeg",
"-i",
in_file,
"-ar",
"16000",
"-ac",
"1",
"-sample_fmt",
"s16",
out_file,
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
Convert Numpy to WAV with wavio
wavio.write
writes a numpy array to a WAV file, optionally using a specified sample width.
Create a numpy array of 16-bit sine wave
import numpy as np
frequency = 440 # Our played note will be 440 Hz
fs = 44100 # 44100 samples per second
seconds = 3 # Note duration of 3 seconds
t = np.linspace(0, seconds, seconds * fs, False)
note = np.sin(frequency * t * 2 * np.pi)
audio = note * (2**15 - 1) / np.max(np.abs(note)) # Ensure that highest value is in 16-bit range
my_np_array = audio.astype(np.int16) # Convert to 16-bit data
Save to wav
file
import wavio
wavio.write('/tmp/myfile.wav', my_np_array, fs, sampwidth=2)
!ls /tmp/myfile*.wav
/tmp/myfile.wav
Convert with pydub
Can convert between all ffmpeg formats
from pydub import AudioSegment
sound = AudioSegment.from_file('/tmp/myfile.wav', format='wav')
sound.export('/tmp/myfile.mp3', format='mp3')
<_io.BufferedRandom name='/tmp/myfile.mp3'>
!ls /tmp/myfile*
/tmp/myfile.mp3 /tmp/myfile.wav
Convert with soundfile
Can convert between all libsndfile formats
import soundfile as sf
# Extract audio data and sampling rate from file
data, fs = sf.read('/tmp/myfile.wav')
# Save as FLAC file at correct sampling rate
sf.write('/tmp/myfile.flac', data, fs)
!ls /tmp/myfile*
/tmp/myfile.flac /tmp/myfile.mp3 /tmp/myfile.wav