silence/generate.py

43 lines
1.3 KiB
Python
Raw Normal View History

2024-04-15 08:51:06 +00:00
from pydub import AudioSegment
import sys
import random
def main(argv: list[str]):
if len(argv) < 7:
print(f"Usage: {argv[0]} <seconds> <seed> <min gap> <max gap> <output> <...audio files>")
exit(1)
duration = int(argv[1]) * 1000
seed = int(argv[2])
min_gap = int(argv[3]) * 1000
max_gap = int(argv[4]) * 1000
output_name = argv[5]
files = argv[6:]
random.seed(seed)
print(files)
clips = list(map(lambda filename: AudioSegment.from_file(filename), files))
output = AudioSegment.silent(0)
total_duration = 0
while total_duration < duration:
sfx = random.choice(clips)
print(duration, total_duration, len(sfx), max_gap)
remaining = duration - total_duration
if remaining < len(sfx) or remaining < min_gap:
silence_duration = remaining
else:
silence_duration = random.randint(min_gap, min(remaining, max_gap))
clip = AudioSegment.silent(silence_duration)
output = output.append(clip, crossfade=0)
total_duration += len(clip)
if total_duration < duration:
output = output.append(sfx, crossfade=0)
total_duration += len(sfx)
output.export(output_name, format="mp3")
if __name__ == '__main__':
main(sys.argv)