Skip to content

Instantly share code, notes, and snippets.

@amarburg
Created August 16, 2017 19:17
Show Gist options
  • Save amarburg/471d9f18b0202a16d4eac6f9b39e282e to your computer and use it in GitHub Desktop.
Save amarburg/471d9f18b0202a16d4eac6f9b39e282e to your computer and use it in GitHub Desktop.
import argparse
import logging
import datetime
from os import path
parser = argparse.ArgumentParser(description='Generate .str file for a set of image files')
parser.add_argument('input', metavar='inputfiles', nargs='+',
help='Image files to process')
parser.add_argument('--output', dest='srtfile', nargs='?', default='subtitles.srt', help='Name of output .srt file')
args = parser.parse_args()
images_per_sec = 10.0
dt = datetime.timedelta(seconds=(1.0/images_per_sec))
with open(args.srtfile, 'w') as srt:
count = 1
this_time = datetime.datetime(1,1,1)
for f in args.input:
srt.write("%d\n" % count)
next_time = this_time + dt
srt.write("%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n" %
(this_time.hour, this_time.minute, this_time.second, this_time.microsecond/1000,
next_time.hour, next_time.minute, next_time.second, next_time.microsecond/1000))
# Write the actual subtitle
srt.write("%s\n" % path.basename(f) )
# Blank line between entries
srt.write("\n")
this_time = next_time
count = count + 1
## To add subtitles to an existing file (makes a new copy)
# ffmpeg -i input.mp4 -i subtitles.srt -scodec mov_text subtitled.mp4
#
# This '-scodec mov_text' converts the .srt file to mov_text, which I guess
# the mp4 version of subtitle data. Not necessary if using a different
# container (?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment