Dup Ver Goto 📝

Make Timer Subtitles .srt

To
62 lines, 164 words, 1391 chars Page 'MakeTimerSrt' does not exist.

See MakeTimerSubtitesASS_1. See SRT_Notes.

#!/usr/bin/env python

import sys
import os
import math

from subprocess import run, DEVNULL
import json

def getvinfo(fn):
  fn = str(fn)
  m = run(["ffprobe","-print_format","json","-show_streams","-show_format",fn],capture_output=True,stdin=DEVNULL)
  if m.returncode != 0:
    print(f"ffprobe on {fn} returned {m.returncode}")
    return None
  return json.loads(m.stdout.decode())

args = sys.argv[1:]
if len(args) == 0:
  print(f"{sys.argv[0]} <fn> [<fn> ...]")
  exit()

def tohms(x):
  h, x = divmod(x,3600)
  m, s = divmod(x,60)
  return h,m,s

for fn in args:
  if not os.path.isfile(fn):
    print(f"File {s} does not exist or is not a file.")
    continue
  try:
    info = getvinfo(fn)
  except Exception as e:
    print(f"Exception {e} ({type(e)}) getting video info for {fn}")
    continue
  ofn = f"{fn}.timer.srt"
  fmt = info['format']
  dur = math.ceil(float(fmt['duration']))
  out = []
  vh, vm, vs = tohms(dur)
  for i in range(dur):
    j = i + 1
    h,m,s = tohms(i)
    t = f"{m:02d}:{s:02d}"
    if vh > 0:
      t = f"{h:02d}:{t}"
    t0 = f"{h:02d}:{m:02d}:{s:02d},0"
    h,m,s = tohms(i+1)
    t1 = f"{h:02d}:{m:02d}:{s:02d},0"
    out.append(str(j))
    out.append(f"{t0} --> {t1}")
    out.append(t)
    out.append("")
  out = "\n".join(out)
  with open(ofn,"wt") as f:
    print(out,file=f)
    print(ofn)