Skip to content

Commit 0d67c6a

Browse files
committed
Address division by zero and invalid duration
Addresses #93
1 parent 05c1507 commit 0d67c6a

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

Scripts/auth.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
# Google Cloud Globals
2323
token_file_name = 'token.pickle'
2424
youtube_token_filename = 'yt_token.pickle'
25-
GOOGLE_TTS_API = None
26-
GOOGLE_TRANSLATE_API = None
25+
GOOGLE_TTS_API:Any = None
26+
GOOGLE_TRANSLATE_API:Any = None
27+
YOUTUBE_API:Any = None
2728

2829
# deepl Globals
2930
DEEPL_API = None
@@ -125,7 +126,7 @@ def get_authenticated_service(youtubeAuth = False):
125126
def youtube_authentication():
126127
global YOUTUBE_API
127128
try:
128-
YOUTUBE_API: Any = get_authenticated_service(youtubeAuth = True) # Create authentication object
129+
YOUTUBE_API = get_authenticated_service(youtubeAuth = True) # Create authentication object
129130
except JSONDecodeError as jx:
130131
print(f" [!!!] Error: " + str(jx))
131132
print(f"\nDid you make the client_secrets.json file yourself by copying and pasting into it, instead of downloading it?")

Scripts/enums.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ class SubsDictKeys(str, Enum):
9292
speed_factor = "speed_factor"
9393

9494
def __str__(self):
95-
return self.value
95+
return self.value
96+
97+
class VariousDefaults():
98+
defaultSpeechRateGoal:float = 20
99+

Scripts/load_configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Config:
7474
increase_max_chars_for_extreme_speeds: bool
7575
subtitle_gap_threshold_milliseconds: int
7676
prioritize_avoiding_fragmented_speech: bool
77-
speech_rate_goal: Union[str, int] # 'Auto' or int
77+
speech_rate_goal: Union[str, int, float] # 'Auto' or int
7878
debug_mode: bool
7979
youtube_autosync_languages: list[str]
8080

Scripts/shared_imports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
LangDataKeys = enums.LangDataKeys
4444
LangDictKeys = enums.LangDictKeys
4545
SubsDictKeys = enums.SubsDictKeys
46+
VariousDefaults = enums.VariousDefaults
4647

4748
# ---------------------------------------------------------------------------------------
4849

@@ -72,7 +73,8 @@
7273
FormalityPreference,
7374
LangDataKeys,
7475
LangDictKeys,
75-
SubsDictKeys
76+
SubsDictKeys,
77+
VariousDefaults
7678
]
7779

7880
# Export all objects

Scripts/translate.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ def set_translation_info(languageBatchDict):
600600
def combine_subtitles_advanced(inputDict, maxCharacters=200):
601601
# Set gap threshold, the maximum gap between subtitles to combine
602602
gapThreshold = config.subtitle_gap_threshold_milliseconds
603+
charRateGoal:float
603604

604605
if (config.speech_rate_goal == 'auto'):
605606
# Calculate average char rate goal by dividing the total number of characters by the total duration in seconds from the last subtitle timetsamp
@@ -608,10 +609,21 @@ def combine_subtitles_advanced(inputDict, maxCharacters=200):
608609
for key, value in inputDict.items():
609610
totalCharacters += len(value[SubsDictKeys.translated_text])
610611
totalDuration = int(value[SubsDictKeys.end_ms]) / 1000 # Just ends up staying as last subtitle timestamp
611-
charRateGoal = totalCharacters / totalDuration
612-
charRateGoal = round(charRateGoal, 2)
612+
613+
# If the duration is zero there's a problem. Print a warning and try to continue
614+
if totalDuration == 0:
615+
print("ERROR: Total duration of subtitles is zero. Something may be wrong with your subtitles file or it's empty. The script will try to continue but don't expect good results.")
616+
charRateGoal = VariousDefaults.defaultSpeechRateGoal
617+
else:
618+
charRateGoal = totalCharacters / totalDuration
619+
charRateGoal = round(charRateGoal, 2)
613620
else:
614-
charRateGoal = config.speech_rate_goal
621+
# Check if it can be converted to a float, if not set to default
622+
try:
623+
charRateGoal = float(config.speech_rate_goal)
624+
except ValueError:
625+
print(f"WARNING: Invalid value for 'speech_rate_goal' in config. Setting to default value of {VariousDefaults.defaultSpeechRateGoal}")
626+
charRateGoal = VariousDefaults.defaultSpeechRateGoal
615627

616628
# Don't change this, it is not an option, it is for keeping track
617629
noMorePossibleCombines = False

0 commit comments

Comments
 (0)