I'm trying to split a 15-hour-long audiobook into shorter one-hour-long .mp3 files. Here's the function I made for this:
from pydub import AudioSegment
import re, os, math
def process(fname, title):
head_dir = title + ' (split)'
if head_dir not in os.listdir():
os.mkdir(head_dir)
book = AudioSegment.from_file(fname)
seconds = book.duration_seconds
iters = math.ceil(seconds / 3600)
one_hour = 3600_000
thirty_sec = 30_000
left_off = 0
for i in range(1,iters+1):
last_iter = i == iters
if not last_iter:
segment = book[left_off:one_hour+thirty_sec].fade_in(1_500).fade_out(3_000)
left_off += one_hour
else:
segment = book[left_off:].fade_in(1_500)
segment.export(f'{head_dir}/{i:02d}.mp3')
I've tried this with a file that's around one and a half hours long, and it worked, splitting it into one hour-long file and one thirty-minute-long one, and taking about 5-6 minutes. But running it with a 15-hour-long file doesn't work. It just keeps going forever it seems.
Are there other libraries I could use for this task?