0% found this document useful (0 votes)
29 views

Read and Write Audio Files in Python Using FFMPEGzulko-github-io

Uploaded by

Sathi Natarajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Read and Write Audio Files in Python Using FFMPEGzulko-github-io

Uploaded by

Sathi Natarajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/8/24, 11:24 PM Read and write audio files in Python using FFMPEG - __del__( self )

__del__( self )
Eaten by the Python.
RSS

Search

Navigate…

Blog
Archives

Read and Write Audio Files in Python Using FFMPEG


Oct 4th, 2013 | Comments

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:

1 FFMPEG_BIN = "ffmpeg" # on Linux


2 FFMPEG_BIN = "ffmpeg.exe" # on Windows

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

1 pipe = sp.Popen([FFMPEG_BINARY,"-i", 'mySong.mp3', "-"],


2 stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
3 pipe.stdout.readline()
4 pipe.terminate()
5 infos = proc.stderr.read()

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:

1 pipe = sp.Popen([ FFMPEG_BIN,


2 '-y', # (optional) means overwrite the output file if it already exists.
3 "-f", 's16le', # means 16bit input
4 "-acodec", "pcm_s16le", # means raw 16bit input
5 '-r', "44100", # the input will have 44100 Hz
6 '-ac','2', # the input will have 2 channels (stereo)
7 '-i', '-', # means that the input will arrive from the pipe
8 '-vn', # means "don't expect any video input"
9 '-acodec', "libfdk_aac" # output audio codec
10 '-b', "3000k", # output bitrate (=quality). Here, 3000kb/second
11 'my_awesome_output_audio_file.mp3'],
12 stdin=sp.PIPE,stdout=sp.PIPE, stderr=sp.PIPE)

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.

Posted by Zulko Oct 4th, 2013

Post

« Read and write video frames in Python using FFMPEG Delay differential equations in Python »

Comments
Recent Posts

An Algorithm to Extract Looping GIFs From Videos


Data Animations With Python and MoviePy
Things You Can Do With Python and POV-Ray
Vector Animations With Python
A Python Script Controlled via Twitter

GitHub Repos

moviepy

Video editing with Python

pianoputer

Use your computer keyboard as a "piano".

bricks_and_scissors

Blog on the computational side of synthetic biology and DNA manufacturing

sheet-music--Gershwin-sweet-and-lowdown

Transcription of the Sweet and Lowdown piano roll by George Gershwin

sheet-music--tarantelle-minute

A "minute tarentella"

@Zulko on GitHub

Copyright © 2015 - Zulko - Powered by Octopress

https://zulko.github.io/blog/2013/10/04/read-and-write-audio-files-in-python-using-ffmpeg/ 3/3

You might also like