Created
May 9, 2020 16:41
-
-
Save lambdan/3a90d48622261db8e7200f3f3d560c35 to your computer and use it in GitHub Desktop.
Create SRT with timecodes to overlay speedrun for timing
This file contains hidden or 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
| from datetime import datetime | |
| import os | |
| # input your times | |
| # HH:MM:SS.FFF | |
| video_length = "00:06:42.984" | |
| start_time = "00:00:20.649" | |
| end_time = "00:06:05.899" | |
| fps = 60 | |
| outfile = 'time.srt' | |
| # remove old srt | |
| if os.path.isfile(outfile): | |
| os.remove(outfile) | |
| # convert hhmmss.fff to secs | |
| def hhmmssff_to_secs(t): # https://stackoverflow.com/a/50069650 | |
| timezero = datetime.strptime('00:00:00.000', '%H:%M:%S.%f') | |
| return (datetime.strptime(t, '%H:%M:%S.%f') - timezero).total_seconds() | |
| # write to srt | |
| def write_to_srt(f, iter, start, end, msg): | |
| with open(f, "a") as srt: | |
| srt.write( str(iter) +'\n' ) | |
| srt.write( str(start) + ' --> ' + str(end) +'\n') | |
| srt.write( str(msg) + '\n\n') | |
| time = 0.0 | |
| runtime = 0.0 | |
| i = 1 | |
| # before run starts print 00:00:00 | |
| while time <= hhmmssff_to_secs(start_time): | |
| start_string = datetime.utcfromtimestamp(time).strftime('%H:%M:%S,%f')[:-3] | |
| msg = datetime.utcfromtimestamp(runtime).strftime('%H:%M:%S,%f')[:-3] | |
| time += (1/fps) | |
| # dont increment runtime here | |
| end_string = datetime.utcfromtimestamp(time).strftime('%H:%M:%S,%f')[:-3] | |
| write_to_srt(outfile, i, start_string, end_string, msg) | |
| i+=1 | |
| # during run | |
| while time <= hhmmssff_to_secs(end_time): | |
| start_string = datetime.utcfromtimestamp(time).strftime('%H:%M:%S,%f')[:-3] | |
| msg = datetime.utcfromtimestamp(runtime).strftime('%H:%M:%S,%f')[:-3] | |
| time += (1/fps) | |
| runtime += (1/fps) | |
| end_string = datetime.utcfromtimestamp(time).strftime('%H:%M:%S,%f')[:-3] | |
| write_to_srt(outfile, i, start_string, end_string, msg) | |
| i += 1 | |
| # after run print final time | |
| while time <= hhmmssff_to_secs(video_length): | |
| start_string = datetime.utcfromtimestamp(time).strftime('%H:%M:%S,%f')[:-3] | |
| msg = datetime.utcfromtimestamp(runtime).strftime('%H:%M:%S,%f')[:-3] | |
| time += (1/fps) | |
| # dont increment runtime here | |
| end_string = datetime.utcfromtimestamp(time).strftime('%H:%M:%S,%f')[:-3] | |
| write_to_srt(outfile, i, start_string, end_string, msg) | |
| i+=1 | |
| # then to overlay it in ffmpeg: | |
| # ffmpeg -i video.mp4 -vf subtitles=time.srt out.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment