Skip to content

Instantly share code, notes, and snippets.

@skumarlabs
Created February 6, 2022 09:44
Show Gist options
  • Save skumarlabs/63e6e2fe3bf2f92c5e80eca77341446a to your computer and use it in GitHub Desktop.
Save skumarlabs/63e6e2fe3bf2f92c5e80eca77341446a to your computer and use it in GitHub Desktop.
Convert standard srt file to Audacity labels format
import datetime as dt
import argparse
import os
def convert_timecode(timecode):
timecode = dt.datetime.strptime(timecode, '%H:%M:%S,%f')
return str((timecode.hour*3600) + (timecode.minute*60) + (timecode.second) + (timecode.microsecond/1000000))
def main(path):
output_dir = os.path.dirname(path)
with open(path, "r") as f:
data = f.read()
scenes = data.split("\n\n") # different scenes
reformatted_srt = ""
for scene in scenes:
try:
parts = scene.split('\n')
temp = (parts[1].replace(' --> ', '\t') + "\t" + " ".join(parts[2:])).split('\t')
reformatted_srt += f"{convert_timecode(temp[0])}\t{convert_timecode(temp[1])}\t{temp[2]}\n"
except:
print(scene)
with open(f"{path}.txt", "w") as f:
f.write(reformatted_srt)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert standard srt file to Audacity labels format"
"""
Example runs:
python srt_to_labels.py --srt /path/to/movie.srt
""",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("--srt", type=str, help="Path to srt file.", required=True)
args = parser.parse_args()
main(args.srt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment