I guess I’m learning Python

I just wrote the first script I’ve written in years and it works beautifully. It’s very dirty, and I’m sure I’ll rewrite it to be more robust, but for now it gets the job done. The job, so to speak, is relatively simple. Take a large single audiobook file and break it up into smaller, regular tracks. Using python, ffprobe, ffmpeg and very liberal use of google, I got it done in just few hours total time. But this is starting pretty much from scratch. I couldn’t remember how to start a python script. It’s amazing what one is able to remember when the need arises. I seemingly intuitively knew to use str() and float() to correct a concat error? That one surprised me. Anyway, here it is in all it’s glory.


#!/usr/bin/python

## this is a very quick and dirty file splitter
## i think this only works for non-chapter .m4b
## quicksplit.py author title filename

import sys
import subprocess
import os

author = sys.argv[1]
title = sys.argv[2]
filename = sys.argv[3]

start = 0
end = 0
track = 1
segment = 1800 # time in seconds

probe_command = "ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 '" + filename + "'"

print("Audiobook " + title + " written by " + author)

## get the total duration of the file and convert to float
duration = float(subprocess.check_output(probe_command, shell=True))

while start < duration : end = start + segment ## get a new endpoint by adding the segment length if end > duration : ## check of we've iterated past the actual duration
end = duration
print("Break from " + str(start) + " to " + str(end) + " until " + str(duration))
split_command = "ffmpeg -i '" + filename + "' -ss " + str(start) + " -to " + str(end) + " -acodec copy '" + author + " - " + title + " - " + str(track) + ".mp4' #print(split_command)
subprocess.call(split_command, shell=True)
start = end ## iterate start to where we left off
track = track + 1