Convert Audio flac to wav in Python – Python Tutorial

By | June 8, 2022

In this tutorial, we will introduce you how to convert a flac audio to wav in python. We will use python soundfile to implement it.

Convert Audio flac to wav in Python - Python Tutorial

Convert Audio flac to wav

It is easy to convert, here is an example:

  1. import soundfile
  2. import numpy
  3. wav_file = r'drama-02-005.flac'
  4. audio, sr = soundfile.read(wav_file)
  5. soundfile.write('drama-02-005.wav', audio, sr, 'PCM_16')
import soundfile
import numpy

wav_file = r'drama-02-005.flac'
audio, sr = soundfile.read(wav_file)
soundfile.write('drama-02-005.wav', audio, sr, 'PCM_16')

Then, we can convert drama-02-005.flac to drama-02-005.wav.

However, we can find an interesting thing.

Read flac and wav audio using soundfile

We can read flac and wav audio data and compare them.

Read flac

  1. wav_file = r'drama-02-005.flac'
  2. audio, sr = soundfile.read(wav_file)
  3. print(audio[200:220])
  4. print(audio.shape)
  5. print(sr)
wav_file = r'drama-02-005.flac'

audio, sr = soundfile.read(wav_file)
print(audio[200:220])
print(audio.shape)
print(sr)

Run this code, we will get:

  1. [ 0.01165771 0.00906372 0.01480103 0.02606201 0.03927612 0.03259277
  2. 0.01361084 0.00549316 0.01748657 0.02841187 0.03866577 0.04016113
  3. 0.02236938 0.00088501 0.00335693 0.01687622 0.00473022 -0.01751709
  4. -0.02236938 -0.01037598]
  5. (21177,)
[ 0.01165771  0.00906372  0.01480103  0.02606201  0.03927612  0.03259277
  0.01361084  0.00549316  0.01748657  0.02841187  0.03866577  0.04016113
  0.02236938  0.00088501  0.00335693  0.01687622  0.00473022 -0.01751709
 -0.02236938 -0.01037598]
(21177,)

Read wav file

  1. wav_file = r'drama-02-005.wav'
  2. audio, sr = soundfile.read(wav_file)
  3. print(audio[200:220])
  4. print(audio.shape)
  5. print(sr)
wav_file = r'drama-02-005.wav'
audio, sr = soundfile.read(wav_file)
print(audio[200:220])
print(audio.shape)
print(sr)

We also will get:

  1. [ 0.01165771 0.00906372 0.01480103 0.02606201 0.03927612 0.03259277
  2. 0.01361084 0.00549316 0.01748657 0.02841187 0.03866577 0.04016113
  3. 0.02236938 0.00088501 0.00335693 0.01687622 0.00473022 -0.01751709
  4. -0.02236938 -0.01037598]
  5. (21177,)
  6. 16000
[ 0.01165771  0.00906372  0.01480103  0.02606201  0.03927612  0.03259277
  0.01361084  0.00549316  0.01748657  0.02841187  0.03866577  0.04016113
  0.02236938  0.00088501  0.00335693  0.01687622  0.00473022 -0.01751709
 -0.02236938 -0.01037598]
(21177,)
16000

We can find: audo data from flac and wav are the same.

Leave a Reply