Skip to content

Instantly share code, notes, and snippets.

@david-wm-sanders
Created February 18, 2018 23:39
Show Gist options
  • Save david-wm-sanders/52df3ad3e0561790d612c6f9a3ae2840 to your computer and use it in GitHub Desktop.
Save david-wm-sanders/52df3ad3e0561790d612c6f9a3ae2840 to your computer and use it in GitHub Desktop.
import csv
import subprocess
import sys
from datetime import timedelta
from pathlib import Path
ffmpeg_exe = Path("C:\\ffmpeg\\ffmpeg.exe")
field_headers = ["begin", "end", "text", "filename"]
mp3_a, csv_a = sys.argv[1], sys.argv[2]
fudge_a, pad_a = sys.argv[3], sys.argv[4]
mp3_p = Path(__file__).parent / mp3_a
csvi_p = Path(__file__).parent / csv_a
out_pd = Path(__file__).parent / f"{mp3_p.stem}"
if not out_pd.exists():
out_pd.mkdir(parents=True, exist_ok=True)
csvo_p = out_pd / f"{csvi_p.stem}_out.csv"
with csvi_p.open(mode="r", encoding="utf-8") as csvi_f, \
csvo_p.open(mode="w", encoding="utf-8", newline="") as csvo_f:
reader = csv.DictReader(csvi_f)
writer = csv.writer(csvo_f, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(field_headers)
for i, r in enumerate(reader, 1):
begin_s, end_s, text = r["begin"], r["end"], r["text"]
# Parse begin and end times into timedeltas
hours, minutes, seconds = begin_s[0:8].split(":")
hours, minutes, seconds = int(hours), int(minutes), int(seconds)
begin = timedelta(hours=hours, minutes=minutes, seconds=seconds)
hours, minutes, seconds = end_s[0:8].split(":")
hours, minutes, seconds = int(hours), int(minutes), int(seconds)
end = timedelta(hours=hours, minutes=minutes, seconds=seconds)
# Fudge the times to re-align them with the mp3 properly
fudge = int(fudge_a)
begin = begin - timedelta(seconds=fudge)
end = end - timedelta(seconds=fudge)
# Apply the padding to the times
pad = int(pad_a)
begin = begin - timedelta(seconds=pad)
end = end + timedelta(seconds=pad)
# Create the output mp3 path
out_pf = out_pd / f"{i}.mp3"
# Write an updated entry to the CSV subtitle extraction list
writer.writerow([begin, end, text, out_pf.name])
# Form the ffmpeg command
command = [str(ffmpeg_exe), "-y",
"-i", str(mp3_p),
"-ss", f"{begin}",
"-to", f"{end}",
str(out_pf)]
# Run the command
subprocess.run(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment