Skip to content

Commit

Permalink
adds webvtt format options
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed May 10, 2023
1 parent ecb9955 commit 35058e4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/video_subtitles/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def open_folder(path):
class MainWidget(QMainWindow): # pylint: disable=too-many-instance-attributes
"""Main widget."""

def __init__(self, on_drop_callback):
def __init__(self, on_drop_callback): # pylint: disable=too-many-statements
super().__init__()
self.setWindowTitle("Video Subtitle Generator")
self.resize(720, 480)
Expand Down Expand Up @@ -86,8 +86,20 @@ def __init__(self, on_drop_callback):
model_layout.addWidget(self.model_select)
model_layout.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)

# Add the convert_to_webvtt dropdown menu
webvtt_layout = QHBoxLayout()
self.subtitle_format = QLabel(self)
self.subtitle_format.setText("Subtitle Format:")
self.webvtt_select = QComboBox(self)
self.webvtt_select.addItems(["WEBVTT", "SRT"])
self.webvtt_select.setCurrentText(settings.subtitle_format())
webvtt_layout.addWidget(self.subtitle_format)
webvtt_layout.addWidget(self.webvtt_select)
webvtt_layout.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeft)

header_layout.addLayout(deepl_layout)
header_layout.addLayout(model_layout)
header_layout.addLayout(webvtt_layout)

# Add translation output label and text field
output_layout = QHBoxLayout()
Expand Down Expand Up @@ -130,6 +142,8 @@ def save_settings(self) -> None:
languages = self.output_text.text().strip().split(",")
languages = [lang.strip() for lang in languages]
settings.set_languages(languages)
subtitle_format = self.webvtt_select.currentText().strip()
settings.set_subtitle_format(subtitle_format)
settings.save() # save settings to file

def dropEvent(self, event):
Expand All @@ -140,9 +154,10 @@ def dropEvent(self, event):
model = self.model_select.currentText().strip()
languages = self.output_text.text().strip().split(",")
languages = [lang.strip() for lang in languages]
convert_to_webvtt = self.webvtt_select.currentText().strip() == "WEBVTT"
for f in files:
self.on_drop_callback(
f, deepl_api_key, languages, model
f, deepl_api_key, languages, model, convert_to_webvtt
) # pass api key to callback


Expand All @@ -153,7 +168,11 @@ def run_gui() -> None:
thread_processor = ThreadProcessor()

def callback(
videofile: str, deepl_api_key: str | None, languages: list[str], model: str
videofile: str,
deepl_api_key: str | None,
languages: list[str],
model: str,
convert_to_webvtt: bool,
):
# path, _ = os.path.splitext(videofile)
# os.makedirs(path, exist_ok=True)
Expand All @@ -163,7 +182,8 @@ def callback(

# Open folder in the OS
def _generate_subtitles(
videofile: str, deeply_api_key: str | None, languages: list[str], model: str
videofile: str, deeply_api_key: str | None, languages: list[str], model: str,
convert_to_webvtt: bool
):
# perform the actual work here
os.chdir(os.path.dirname(videofile))
Expand All @@ -174,7 +194,7 @@ def _generate_subtitles(
deepl_api_key=deeply_api_key,
out_languages=languages,
model=model,
convert_to_webvtt=True,
convert_to_webvtt=convert_to_webvtt,
)
except Exception as e: # pylint: disable=broad-except
print(e)
Expand All @@ -183,11 +203,12 @@ def _generate_subtitles(
open_folder(out)
print("Generating subtitles for", videofile)
voicename = os.path.basename(videofile).split(".")[0].replace("_", " ")
say(f"Attention: {voicename} has completed subtitle generation")
format = "WEBVTT" if convert_to_webvtt else "SRT"
say(f"Attention: {voicename} has completed subtitle generation in {format} format")

thread = Thread(
target=_generate_subtitles,
args=(videofile, deepl_api_key, languages, model),
args=(videofile, deepl_api_key, languages, model, convert_to_webvtt),
daemon=True,
)
thread_processor.add(thread)
Expand Down
9 changes: 9 additions & 0 deletions src/video_subtitles/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def set_languages(self, languages: list[str]) -> None:
"""Set the languages."""
self.data["languages"] = languages

def subtitle_format(self) -> str:
"""Return the subtitle format."""
return self.data.get("subtitle_format", "WEBVTT") # type: ignore

def set_subtitle_format(self, subtitle_format: str) -> None:
"""Set the subtitle format."""
assert subtitle_format in ["WEBVTT", "SRT"]
self.data["subtitle_format"] = subtitle_format

def save(self) -> None:
"""Save the settings."""
# dump json to file
Expand Down

0 comments on commit 35058e4

Please sign in to comment.