AudioClip

AudioClip

class moviepy.audio.AudioClip.AudioClip(make_frame=None, duration=None)[source]

Bases: moviepy.Clip.Clip

Base class for audio clips.

See SoundClip and CompositeSoundClip for usable classes.

An AudioClip is a Clip with a make_frame attribute of the form `` t -> [ f_t ]`` for mono sound and t-> [ f1_t, f2_t ] for stereo sound (the arrays are Numpy arrays). The f_t are floats between -1 and 1. These bounds can be trespassed wihtout problems (the program will put the sound back into the bounds at conversion time, without much impact).

Parameters:

make_frame :

A function t-> frame at time t. The frame does not mean much for a sound, it is just a float. What ‘makes’ the sound are the variations of that float in the time.

nchannels :

Number of channels (one or two for mono or stereo).

Examples

>>> # Plays the note A (a sine wave of frequency 404HZ)
>>> import numpy as np
>>> make_frame = lambda t : 2*[ np.sin(404 * 2 * np.pi * t) ]
>>> clip = AudioClip(make_frame, duration=5)
>>> clip.preview()
iter_chunks(chunksize=None, chunk_duration=None, fps=None, quantize=False, nbytes=2, progress_bar=False)

Iterator that returns the whole sound array of the clip by chunks

to_audiofile(*a, **kw)[source]

The function to_audiofile is deprecated and is kept temporarily for backwards compatibility. Please use the new name, write_audiofile, instead.

to_soundarray(tt=None, fps=None, quantize=False, nbytes=2, buffersize=50000)[source]

Transforms the sound into an array that can be played by pygame or written in a wav file. See AudioClip.preview.

Parameters:

fps :

Frame rate of the sound for the conversion. 44100 for top quality.

nbytes :

Number of bytes to encode the sound: 1 for 8bit sound, 2 for 16bit, 4 for 32bit sound.

write_audiofile(filename, fps=44100, nbytes=2, buffersize=2000, codec=None, bitrate=None, ffmpeg_params=None, write_logfile=False, verbose=True)

Writes an audio file from the AudioClip.

Parameters:

filename :

Name of the output file

fps :

Frames per second

nbyte :

Sample width (set to 2 for 16-bit sound, 4 for 32-bit sound)

codec :

Which audio codec should be used. If None provided, the codec is determined based on the extension of the filename. Choose ‘pcm_s16le’ for 16-bit wav and ‘pcm_s32le’ for 32-bit wav.

bitrate :

Audio bitrate, given as a string like ‘50k’, ‘500k’, ‘3000k’. Will determine the size and quality of the output file. Note that it mainly an indicative goal, the bitrate won’t necessarily be the this in the output file.

ffmpeg_params :

Any additional parameters you would like to pass, as a list of terms, like [‘-option1’, ‘value1’, ‘-option2’, ‘value2’]

write_logfile :

If true, produces a detailed logfile named filename + ‘.log’ when writing the file

verbose :

If True, displays informations

AudioFileClip

class moviepy.audio.io.AudioFileClip.AudioFileClip(filename, buffersize=200000, nbytes=2, fps=44100)[source]

Bases: moviepy.audio.AudioClip.AudioClip

An audio clip read from a sound file, or an array. The whole file is not loaded in memory. Instead, only a portion is read and stored in memory. this portion includes frames before and after the last frames read, so that it is fast to read the sound backward and forward.

Parameters:

snd :

Either a soundfile name (of any extension supported by ffmpeg) or an array representing a sound. If the soundfile is not a .wav, it will be converted to .wav first, using the fps and bitrate arguments.

buffersize: :

Size to load in memory (in number of frames)

temp_wav: :

Name for the temporary wav file in case conversion is required. If not provided, the default will be filename.wav with some prefix. If the temp_wav already exists it will not be rewritten.

Examples

>>> snd = SoundClip("song.wav")
>>> snd = SoundClip("song.mp3", fps = 44100, bitrate=3000)
>>> snd = SoundClip(mySoundArray,fps=44100) # from a numeric array

Attributes

nbytes Number of bits per frame of the original audio file.
fps Number of frames per second in the audio file
buffersize See Parameters.
coreader()[source]

Returns a copy of the AudioFileClip, i.e. a new entrance point to the audio file. Use copy when you have different clips watching the audio file at different times.

CompositeAudioClip

class moviepy.audio.AudioClip.CompositeAudioClip(clips)[source]

Bases: moviepy.audio.AudioClip.AudioClip

Clip made by composing several AudioClips.

An audio clip made by putting together several audio clips.

Parameters:

clips :

List of audio clips, which may start playing at different times or together. If all have their duration attribute set, the duration of the composite clip is computed automatically.