Skip to content

Instantly share code, notes, and snippets.

@ScriptRaccoon
Created April 27, 2024 21:12
Show Gist options
  • Save ScriptRaccoon/41a53790980ca7fa288935adaa4367c3 to your computer and use it in GitHub Desktop.
Save ScriptRaccoon/41a53790980ca7fa288935adaa4367c3 to your computer and use it in GitHub Desktop.
script generating numbered timestamps based on clicks
import time
# This script generates numbered timestamps based on clicks
# This has been used for example in https://youtu.be/4t61xW8QIEg
def main():
print("Click whenever you want to mark a timestamp")
print("Press 'q' to quit and generate timestamps\n")
start_number = int(input("Input the start number: "))
last_click_time = time.time()
now = int(time.time())
file_name = f"subtitles_{now}.srt"
initial_timestamp = "00:00:00,000"
index = start_number
counter = 0
timestamps = []
while True:
counter += 1
user_input = input(f"Press 'Enter' to mark a timestamp ({counter}): ")
if user_input.lower() == "q":
break
current_time = time.time()
interval = current_time - last_click_time
timestamp = get_timestamp(interval)
timestamps.append(timestamp)
with open(file_name, "w") as file:
for index, current_timestamp in enumerate(timestamps):
previous_timestamp = (
timestamps[index - 1] if index > 0 else initial_timestamp
)
subtitle = get_subtitle(
index, start_number, previous_timestamp, current_timestamp
)
file.write(subtitle)
print(f"Subtitles generated and saved to '{file_name}'.")
def get_subtitle(index, start_number, previous_timestamp, current_timestamp):
sub = str(index + start_number + 1) + "\n"
sub += f"{previous_timestamp} --> {current_timestamp}\n"
sub += str(index + start_number) + "\n\n"
return sub
def get_timestamp(time):
hours = int(time / 3600)
minutes = int((time % 3600) / 60)
seconds = int(time % 60)
milliseconds = int((time % 1) * 1000)
return f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment