Created
January 16, 2024 02:33
-
-
Save techforum-repo/8ddaf80af661def3124aab15f2f94cc8 to your computer and use it in GitHub Desktop.
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
import json | |
# Function to convert chunks to SRT entries | |
def chunks_to_srt(chunks): | |
srt_entries = [] | |
sequence_number = 1 | |
for chunk in chunks: | |
start_time = chunk["timestamp"][0] | |
end_time = chunk["timestamp"][1] | |
text = chunk["text"] | |
srt_entry = f"{sequence_number}\n" | |
srt_entry += f"{format_time(start_time)} --> {format_time(end_time)}\n" | |
srt_entry += f"{text}\n\n" | |
srt_entries.append(srt_entry) | |
sequence_number += 1 | |
return srt_entries | |
# Function to format time in SRT format (00:00:00,000) | |
def format_time(seconds): | |
hours, seconds = divmod(seconds, 3600) | |
minutes, seconds = divmod(seconds, 60) | |
milliseconds = int((seconds - int(seconds)) * 1000) | |
return f"{int(hours):02}:{int(minutes):02}:{int(seconds):02},{milliseconds:03}" | |
# Read data from JSON file | |
with open("Content-Security-Policy-in-NodeJS_Express.json", "r", encoding="utf-8") as json_file: | |
data = json.load(json_file) | |
# Convert chunks to SRT format | |
srt_entries = chunks_to_srt(data["chunks"]) | |
# Write SRT entries to a file | |
with open("output.srt", "w", encoding="utf-8") as srt_file: | |
srt_file.write("\n".join(srt_entries)) | |
print("SRT file 'output.srt' has been created.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment