Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Silence with pyDub

I've found pyDub, and it seems like just what I need:

http://pydub.com/

The only issue is with generating silence. Can pyDub do this?

Essentially the workflow I want is:

  1. Take all the WAV files in a directory
  2. Piece them together in filename order with 1 sec of silence in between
  3. Generate a single MP3 of the result

Is this possible? I realize I could create a WAV of silence and do it that way (spacer GIF flashback, anyone?), but I'd prefer to generate the silence programmatically, because I may want to experiment with the duration of silence and/or the bitrate of the MP3.

I greatly appreciate any responses.

like image 598
user3643227 Avatar asked May 16 '14 03:05

user3643227


People also ask

How to use pydub library to play audio files?

For accessing input Sound files click here. Let’s see the code for some functionalities of pydub library: 1) Playing Audio File: This is done using play () method. Use Up/Down Arrow keys to increase or decrease volume. 2) Knowing about .wav file: for this we will use attributes of audio file object.

What are the different functions of pydub?

Following are some functionalities that can be performed by pydub: Playing audio file. We can get certain information of file like length channels. Increase/Decrease volume of given .wav file. Merging two or more audio files. Exporting an audio file. Splitting an audio file.

What is second_of_silence in pydub?

now second_of_silence would be an AudioSegement just like song in the example and could be manipulated, composed, etc. with no blank audio files needed. @Jiaaro As someone who has just been made aware of pydub, allow me to complement you on the clarity of the API; it makes me wish I had audio compositing to do.

How do I make audio segments silent in Python?

pydub.AudioSegment = class AudioSegment (__builtin__.object) | AudioSegments are *immutable* objects representing segments of audio | that can be manipulated using python code. … | silent (cls, duration=1000) from __builtin__.type | Generate a silent audio segment. | duration specified in milliseconds (default: 1000ms).


1 Answers

The pydub sequences are composed of pydub.AudioSegment instances. The pydub quickstart documentation only shows how to create AudioSegments from files.

However, reading the source, or even more easily, running pydoc pydub.AudioSequence reveals

pydub.AudioSegment = class AudioSegment(__builtin__.object)
 |  AudioSegments are *immutable* objects representing segments of audio
 |  that can be manipulated using python code.
 …
 |  silent(cls, duration=1000) from __builtin__.type
 |      Generate a silent audio segment. 
 |      duration specified in milliseconds (default: 1000ms).

which would be called like (following the usage in the quick start guide):

from pydub import AudioSegment
second_of_silence = AudioSegment.silent() # use default
second_of_silence = AudioSegment.silent(duration=1000) # or be explicit

now second_of_silence would be an AudioSegement just like song in the example

song = AudioSegment.from_wav("never_gonna_give_you_up.wav")

and could be manipulated, composed, etc. with no blank audio files needed.

like image 142
msw Avatar answered Oct 05 '22 23:10

msw