How to add a specific number of silent samples to an audio file

23 views Asked by At

I am manipulating audio with pydub for use with video. I want to pad the end of an audio segment with silent frames so it fills a frame of video. At an audio sampling rate of 48kHz and a video sampling rate of 25 fps, I need each audio segment to have a multiple of 1920 samples (= 48 000 / 25).

I followed this thread to add silent samples at the end of a segment:

import pydub

segment = pydub.AudioSegment.from_wav("jingle.wav")
sampling_rate = segment.frame_rate
assert 48000 == sampling_rate
num_samples = len(segment.get_array_of_samples())

num_samples_to_add = 1920 - num_samples % 1920

silence = pydub.AudioSegment.silent(duration=1000, frame_rate=sampling_rate)
silence_samples = silence.get_array_of_samples()
silence_padding = silence._spawn(silence_samples[:num_samples_to_add])

padded = segment + silence_padding

print("Segment: %d samples, %dms" % (len(segment.get_array_of_samples()), len(segment)))
print("Silence: %d samples, %dms" % (len(silence_padding.get_array_of_samples()), len(silence_padding)))
print("Padded: %d samples, %dms" % (len(padded.get_array_of_samples()), len(padded)))

print("Padded should have %d frames instead of %d" % (num_samples + num_samples_to_add, len(padded.get_array_of_samples())))

The result is:

Segment: 230544 samples, 2402ms
Silence: 1776 samples, 37ms
Padded: 234096 samples, 2438ms
Padded should have 232320 frames instead of 234096

The number of milliseconds in the result is right, but the number of samples is not. The code worked on one WAV file but doesn't on this one. I suspect it's because of improper use of ._spawn, but I could not find documentation and I cannot figure out why the discrepancy between duration in seconds and in samples.

How can I add a specific number of silent samples at the end of an audio file?

0

There are 0 answers