Skip to content

Instantly share code, notes, and snippets.

@AmirAref
Created August 27, 2022 19:22
Show Gist options
  • Save AmirAref/ddc223195cae9f41b2466d76ace374d7 to your computer and use it in GitHub Desktop.
Save AmirAref/ddc223195cae9f41b2466d76ace374d7 to your computer and use it in GitHub Desktop.
a simple cli tool to change the subtitle files delay and fix the encoding.
import pysubs2
from optparse import OptionParser
from charset_normalizer import detect
def main():
# parse the options
parser = OptionParser("python %prog [options] --shift 2s FILE.srt")
parser.add_option("-s", "--shift", dest="shift", type=int,
help="shift the subtitle time forward.")
# check the options values
options, args = parser.parse_args()
if not args:
parser.error("Please give a subtitle file path.")
# open file and detect encoding
subtitle_file = args[0]
with open(subtitle_file, 'rb') as file:
encoding = detect(file.read())['encoding']
# open with pysub
subs = pysubs2.load(subtitle_file, encoding=encoding)
if options.shift:
# shift the seconds
subs.shift(s=int(options.shift))
# save the output
subs.save(subtitle_file, encoding="utf-8")
#delay = int(input("Enter the subtitle delay : "))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment