Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated convert_output.py #232

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions convert_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os



class TxtFormatter:
@classmethod
def preamble(cls):
Expand All @@ -20,7 +21,7 @@ def preamble(cls):
return ""

@classmethod
def format_seconds(cls, seconds):
def format_seconds(cls, seconds): # this function nulls if seconds is None
whole_seconds = int(seconds)
milliseconds = int((seconds - whole_seconds) * 1000)

Expand All @@ -34,6 +35,8 @@ def format_seconds(cls, seconds):
def format_chunk(cls, chunk, index):
text = chunk['text']
start, end = chunk['timestamp'][0], chunk['timestamp'][1]
if end is None:
end = start + 10 # default value just to prevent last timestamp null; there probably is a better more elegant solution somewhere
start_format, end_format = cls.format_seconds(start), cls.format_seconds(end)
return f"{index}\n{start_format} --> {end_format}\n{text}\n\n"

Expand All @@ -44,7 +47,7 @@ def preamble(cls):
return "WEBVTT\n\n"

@classmethod
def format_seconds(cls, seconds):
def format_seconds(cls, seconds): # this function nulls if seconds is None
whole_seconds = int(seconds)
milliseconds = int((seconds - whole_seconds) * 1000)

Expand All @@ -58,6 +61,8 @@ def format_seconds(cls, seconds):
def format_chunk(cls, chunk, index):
text = chunk['text']
start, end = chunk['timestamp'][0], chunk['timestamp'][1]
if end is None:
end = start + 10 # default value just to prevent last timestamp null; there probably is a better more elegant solution somewhere
start_format, end_format = cls.format_seconds(start), cls.format_seconds(end)
return f"{index}\n{start_format} --> {end_format}\n{text}\n\n"

Expand Down Expand Up @@ -87,7 +92,7 @@ def convert(input_path, output_format, output_dir, verbose):
def main():
parser = argparse.ArgumentParser(description="Convert JSON to an output format.")
parser.add_argument("input_file", help="Input JSON file path")
parser.add_argument("-f", "--output_format", default="all", help="Format of the output file (default: srt)", choices=["txt", "vtt", "srt"])
parser.add_argument("-f", "--output_format", default="srt", help="Format of the output file (default: srt)", choices=["txt", "vtt", "srt"]) # all was not a valid choice for default, so srt is set as default (like what help said)
parser.add_argument("-o", "--output_dir", default=".", help="Directory where the output file/s is/are saved")
parser.add_argument("--verbose", action="store_true", help="Print each VTT entry as it's added")

Expand Down