-
-
Save skumarlabs/63e6e2fe3bf2f92c5e80eca77341446a to your computer and use it in GitHub Desktop.
Convert standard srt file to Audacity labels format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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