Read and Write Audio Files in Python Using FFMPEGzulko-github-io
Read and Write Audio Files in Python Using FFMPEGzulko-github-io
__del__( self )
Eaten by the Python.
RSS
Search
Navigate…
Blog
Archives
This article shows how easy it is to read or write audio files in a few lines Python, by calling the external
software FFMPEG through pipes. If you want a battle-tested and more sophisticated version, check out my
module MoviePy. Check also that other article for the same with video files.
Before we start, you must have FFMPEG installed on your computer and you must know the name (or path) of
the FFMPEG binary on your computer. It should be one of the following:
Reading
To read the audio file “mySong.mp3” we first ask FFMPEG to open this file and to direct its output to Python:
1 import subprocess as sp
2
3 command = [ FFMPEG_BIN,
4 '-i', 'mySong.mp3',
5 '-f', 's16le',
6 '-acodec', 'pcm_s16le',
7 '-ar', '44100', # ouput will have 44100 Hz
8 '-ac', '2', # stereo (set to '1' for mono)
9 '-']
10 pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)
In the code above -i mySong.mp3 indicates the input file, while s16le/pcm_s16le asks for a raw 16-bit sound
output. The - at the end tells FFMPEG that it is being used with a pipe by another program. In sp.Popen, the
bufsize parameter must be bigger than the biggest chunk of data that you will want to read (see below). It can
be omitted most of the time in Python 2 but not in Python 3 where its default value is pretty small.
https://zulko.github.io/blog/2013/10/04/read-and-write-audio-files-in-python-using-ffmpeg/ 1/3
7/8/24, 11:24 PM Read and write audio files in Python using FFMPEG - __del__( self )
Now you just have to read the output of FFMPEG. In our case we have two channels (stereo sound) so one
frame of out output will be represented by a pair of integers, each coded on 16 bits (2 bytes). Therefore one
frame will be 4-bytes long. To read 88200 audio frames (2 seconds of sound in our case) we will write:
1 raw_audio = pipe.proc.stdout.read(88200*4)
2
3 # Reorganize raw_audio as a Numpy array with two-columns (1 per channel)
4 import numpy
5
6 audio_array = numpy.fromstring(raw_audio, dtype="int16")
7 audio_array = audio_array.reshape((len(audio_array)/2,2))
You can now play this sound using for instance Pygame’s sound mixer:
1 import pygame
2 pygame.init()
3 pygame.mixer.init(44100, -16, 2) # 44100 Hz, 16bit, 2 channels
4 sound = pygame.sndarray.make_sound( audio_array )
5 sound.play()
Finally, you can get informations on a file (audio format, frequency, etc.) by calling
Now infos contains a text describing the file, that you would need to parse to obtain the relevant informations.
See section Going Further below for a link to an implementation.
Writing
To write an audio file we open FFMPEG and specify that the input will be piped and that it will consist in raw
audio data:
The codec can be any valid FFMPEG audio codec. For some codecs providing the output bitrate is optional.
Now you just have to write raw audio data into the file. For instance, if your sound is represented have a Nx2
Numpy array of integers, you will just write
https://zulko.github.io/blog/2013/10/04/read-and-write-audio-files-in-python-using-ffmpeg/ 2/3
7/8/24, 11:24 PM Read and write audio files in Python using FFMPEG - __del__( self )
1 audio_array.astype("int16").tofile(self.proc.stdin)
Going further
I tried to keep the code as simple as possible here. With a few more lines you can make useful classes to
manipulate video files, like FFMPEG_AudioReader and FFMPEG_AudioWriter that I wrote for my video
editing software. In these files in particular how to parse the information on the video, how to save/load pictures
using FFMPEG, etc.
Post
« Read and write video frames in Python using FFMPEG Delay differential equations in Python »
Comments
Recent Posts
GitHub Repos
moviepy
pianoputer
bricks_and_scissors
sheet-music--Gershwin-sweet-and-lowdown
sheet-music--tarantelle-minute
A "minute tarentella"
@Zulko on GitHub
https://zulko.github.io/blog/2013/10/04/read-and-write-audio-files-in-python-using-ffmpeg/ 3/3