Skip to content

Instantly share code, notes, and snippets.

@zmarffy
Last active February 17, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmarffy/d1f7599411204be8ced016195e3655b5 to your computer and use it in GitHub Desktop.
Save zmarffy/d1f7599411204be8ced016195e3655b5 to your computer and use it in GitHub Desktop.
Split an audio file by start times using `ffmpeg`
import argparse
import subprocess
parser = argparse.ArgumentParser()
parser.add_argument("file", help="file to split")
parser.add_argument("name_and_time", type=lambda x: x.rsplit(
"_", 1), nargs="+", help="[name]_[time to split on]")
args = parser.parse_args()
file_extension = args.file.split(".")[-1]
for index, (file_name, start_time) in enumerate(args.name_and_time):
f = f"{file_name}.{file_extension}"
if index == len(args.name_and_time) - 1:
subprocess.check_call(
["ffmpeg", "-i", args.file, "-acodec", "copy", "-ss", start_time, f])
else:
subprocess.check_call(["ffmpeg", "-i", args.file, "-acodec", "copy",
"-ss", start_time, "-to", args.name_and_time[index + 1][1], f])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment