pythonaudiomp3wavpydub

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.


Solution

  • 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.