forked from ThioJoe/Auto-Synced-Translated-Dubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
499 lines (423 loc) · 26 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# Imports
import auth
from utils import parseBool
import utils
import configparser
from operator import itemgetter
import sys
import copy
import os
import pathlib
import langcodes
import html
import re
# Get Configs
cloudConfig = configparser.ConfigParser()
cloudConfig.read('cloud_service_settings.ini')
config = configparser.ConfigParser()
config.read('config.ini')
batchConfig = configparser.ConfigParser()
batchConfig.read('batch.ini')
# Get settings from configs
googleProjectID = cloudConfig['CLOUD']['google_project_id']
# Translation Settings
originalLanguage = config['SETTINGS']['original_language']
formalityPreference = config['SETTINGS']['formality_preference']
preferredTranslateService = cloudConfig['CLOUD']['translate_service']
debugMode = parseBool(config['SETTINGS']['debug_mode'])
combineMaxChars = int(config['SETTINGS']['combine_subtitles_max_chars']) # Will combine subtitles into one audio clip if they are less than this many characters
# MOVE THESE INTO A DICTIONARY VARIABLE AT SOME POINT
# Get original video file path, also allow you to debug using a subtitle file without having the original video file
videoFilePath = batchConfig['SETTINGS']['original_video_file_path']
if debugMode and (videoFilePath == '' or videoFilePath.lower() == 'none'):
originalVideoFile = 'Debug.test'
else:
originalVideoFile = os.path.abspath(videoFilePath.strip("\""))
# Set output folder based on filename of original video file
outputDirectory = "Outputs"
outputFolder = os.path.join(outputDirectory , os.path.splitext(os.path.basename(originalVideoFile))[0])
# ---------------------------------------------------------------------------------------
# Add span tags around certain words to exclude them from being translated
noTranslateOverrideFile = os.path.join('SSML_Customization', 'dont_translate_phrases.txt')
dontTranslateList = utils.txt_to_list(noTranslateOverrideFile)
def add_notranslate_tags(text):
for word in dontTranslateList:
findWordRegex = rf'(\b["\']?{word}[.,!?]?["\']?\b)' # Find the word, with optional punctuation after, and optional quotes before or after
text = re.sub(findWordRegex, r' <span class="notranslate">\1</span> ', text, flags=re.IGNORECASE)
return text
def remove_notranslate_tags(text):
text = text.replace('<span class="notranslate">', '').replace('</span>', '')
return text
#======================================== Translate Text ================================================
# Note: This function was almost entirely written by GPT-3 after feeding it my original code and asking it to change it so it
# would break up the text into chunks if it was too long. It appears to work
def process_response_text(text):
text = html.unescape(text)
text = remove_notranslate_tags(text)
return text
# Translate the text entries of the dictionary
def translate_dictionary(inputSubsDict, langDict, skipTranslation=False):
targetLanguage = langDict['targetLanguage']
translateService = langDict['translateService']
formality = langDict['formality']
# Create a container for all the text to be translated
textToTranslate = []
for key in inputSubsDict:
originalText = inputSubsDict[key]['text']
# Add the text to the list of text to be translated, and also add the span tags around the words that shouldn't be translated
textToTranslate.append(add_notranslate_tags(originalText))
# Calculate the total number of utf-8 codepoints
codepoints = 0
for text in textToTranslate:
text = add_notranslate_tags(text)
codepoints += len(text.encode("utf-8"))
# If the codepoints are greater than 28000, split the request into multiple
# Google's API limit is 30000 Utf-8 codepoints per request, while DeepL's is 130000, but we leave some room just in case
if skipTranslation == False:
if translateService == 'google' and codepoints > 27000 or translateService == 'deepl' and codepoints > 120000:
# GPT-3 Description of what the following line does:
# If Google Translate is being used:
# Splits the list of text to be translated into smaller chunks of 100 texts.
# It does this by looping over the list in steps of 100, and slicing out each chunk from the original list.
# Each chunk is appended to a new list, chunkedTexts, which then contains the text to be translated in chunks.
# The same thing is done for DeepL, but the chunk size is 400 instead of 100.
chunkSize = 100 if translateService == 'google' else 400
chunkedTexts = [textToTranslate[x:x+chunkSize] for x in range(0, len(textToTranslate), chunkSize)]
# Send and receive the batch requests
for j,chunk in enumerate(chunkedTexts):
# Send the request
if translateService == 'google':
# Print status with progress
print(f'[Google] Translating text group {j+1} of {len(chunkedTexts)}')
response = auth.GOOGLE_TRANSLATE_API.projects().translateText(
parent='projects/' + googleProjectID,
body={
'contents': chunk,
'sourceLanguageCode': originalLanguage,
'targetLanguageCode': targetLanguage,
'mimeType': 'text/plain',
#'model': 'nmt',
#'glossaryConfig': {}
}
).execute()
# Extract the translated texts from the response
translatedTexts = [process_response_text(response['translations'][i]['translatedText']) for i in range(len(response['translations']))]
# Add the translated texts to the dictionary
# Divide the dictionary into chunks of 100
for i in range(chunkSize):
key = str((i+1+j*chunkSize))
inputSubsDict[key]['translated_text'] = process_response_text(translatedTexts[i])
# Print progress, ovwerwrite the same line
print(f' Translated with Google: {key} of {len(inputSubsDict)}', end='\r')
elif translateService == 'deepl':
print(f'[DeepL] Translating text group {j+1} of {len(chunkedTexts)}')
# Send the request
result = auth.DEEPL_API.translate_text(chunk, target_lang=targetLanguage, formality=formality)
# Extract the translated texts from the response
translatedTexts = [process_response_text(result[i].text) for i in range(len(result))]
# Add the translated texts to the dictionary
for i in range(chunkSize):
key = str((i+1+j*chunkSize))
inputSubsDict[key]['translated_text'] = process_response_text(translatedTexts[i])
# Print progress, ovwerwrite the same line
print(f' Translated with DeepL: {key} of {len(inputSubsDict)}', end='\r')
else:
print("Error: Invalid translate_service setting. Only 'google' and 'deepl' are supported.")
sys.exit()
else:
if translateService == 'google':
print("Translating text using Google...")
response = auth.GOOGLE_TRANSLATE_API.projects().translateText(
parent='projects/' + googleProjectID,
body={
'contents':textToTranslate,
'sourceLanguageCode': originalLanguage,
'targetLanguageCode': targetLanguage,
'mimeType': 'text/plain',
#'model': 'nmt',
#'glossaryConfig': {}
}
).execute()
translatedTexts = [process_response_text(response['translations'][i]['translatedText']) for i in range(len(response['translations']))]
# Add the translated texts to the dictionary
for i, key in enumerate(inputSubsDict):
inputSubsDict[key]['translated_text'] = process_response_text(translatedTexts[i])
# Print progress, overwrite the same line
print(f' Translated: {key} of {len(inputSubsDict)}', end='\r')
elif translateService == 'deepl':
print("Translating text using DeepL...")
# Send the request
result = auth.DEEPL_API.translate_text(textToTranslate, target_lang=targetLanguage, formality=formality)
# Add the translated texts to the dictionary
for i, key in enumerate(inputSubsDict):
inputSubsDict[key]['translated_text'] = process_response_text(result[i].text)
# Print progress, overwrite the same line
print(f' Translated: {key} of {len(inputSubsDict)}', end='\r')
else:
print("Error: Invalid translate_service setting. Only 'google' and 'deepl' are supported.")
sys.exit()
else:
for key in inputSubsDict:
inputSubsDict[key]['translated_text'] = process_response_text(inputSubsDict[key]['text']) # Skips translating, such as for testing
print(" ")
# # Debug export inputSubsDict as json for offline testing
# import json
# with open('inputSubsDict.json', 'w') as f:
# json.dump(inputSubsDict, f)
# # DEBUG import inputSubsDict from json for offline testing
# import json
# with open('inputSubsDict.json', 'r') as f:
# inputSubsDict = json.load(f)
combinedProcessedDict = combine_subtitles_advanced(inputSubsDict, combineMaxChars)
if skipTranslation == False or debugMode == True:
# Use video file name to use in the name of the translate srt file, also display regular language name
lang = langcodes.get(targetLanguage).display_name()
if debugMode:
if os.path.isfile(originalVideoFile):
translatedSrtFileName = pathlib.Path(originalVideoFile).stem + f" - {lang} - {targetLanguage}.DEBUG.txt"
else:
translatedSrtFileName = "debug" + f" - {lang} - {targetLanguage}.DEBUG.txt"
else:
translatedSrtFileName = pathlib.Path(originalVideoFile).stem + f" - {lang} - {targetLanguage}.srt"
# Set path to save translated srt file
translatedSrtFileName = os.path.join(outputFolder, translatedSrtFileName)
# Write new srt file with translated text
with open(translatedSrtFileName, 'w', encoding='utf-8-sig') as f:
for key in combinedProcessedDict:
f.write(str(key) + '\n')
f.write(combinedProcessedDict[key]['srt_timestamps_line'] + '\n')
f.write(combinedProcessedDict[key]['translated_text'] + '\n')
if debugMode:
f.write(f"DEBUG: duration_ms = {combinedProcessedDict[key]['duration_ms']}" + '\n')
f.write(f"DEBUG: char_rate = {combinedProcessedDict[key]['char_rate']}" + '\n')
f.write(f"DEBUG: start_ms = {combinedProcessedDict[key]['start_ms']}" + '\n')
f.write(f"DEBUG: end_ms = {combinedProcessedDict[key]['end_ms']}" + '\n')
f.write(f"DEBUG: start_ms_buffered = {combinedProcessedDict[key]['start_ms_buffered']}" + '\n')
f.write(f"DEBUG: end_ms_buffered = {combinedProcessedDict[key]['end_ms_buffered']}" + '\n')
f.write('\n')
return combinedProcessedDict
##### Add additional info to the dictionary for each language #####
def set_translation_info(languageBatchDict):
newBatchSettingsDict = copy.deepcopy(languageBatchDict)
# Set the translation service for each language
if preferredTranslateService == 'deepl':
langSupportResponse = auth.DEEPL_API.get_target_languages()
supportedLanguagesList = list(map(lambda x: str(x.code).upper(), langSupportResponse))
# # Create dictionary from response
# supportedLanguagesDict = {}
# for lang in langSupportResponse:
# supportedLanguagesDict[lang.code.upper()] = {'name': lang.name, 'supports_formality': lang.supports_formality}
# Fix language codes for certain languages when using DeepL to be region specific
deepL_code_override = {
'EN': 'EN-US',
'PT': 'PT-BR'
}
# Set translation service to DeepL if possible and get formality setting, otherwise set to Google
for langNum, langInfo in languageBatchDict.items():
# Get language code
lang = langInfo['translation_target_language'].upper()
# Check if language is supported by DeepL, or override if needed
if lang in supportedLanguagesList or lang in deepL_code_override:
# Fix certain language codes
if lang in deepL_code_override:
newBatchSettingsDict[langNum]['translation_target_language'] = deepL_code_override[lang]
lang = deepL_code_override[lang]
# Set translation service to DeepL
newBatchSettingsDict[langNum]['translate_service'] = 'deepl'
# Setting to 'prefer_more' or 'prefer_less' will it will default to 'default' if formality not supported
if formalityPreference == 'more':
newBatchSettingsDict[langNum]['formality'] = 'prefer_more'
elif formalityPreference == 'less':
newBatchSettingsDict[langNum]['formality'] = 'prefer_less'
else:
# Set formality to None if not supported for that language
newBatchSettingsDict[langNum]['formality'] = 'default'
# If language is not supported, add dictionary entry to use Google
else:
newBatchSettingsDict[langNum]['translate_service'] = 'google'
newBatchSettingsDict[langNum]['formality'] = None
# If using Google, set all languages to use Google in dictionary
elif preferredTranslateService == 'google':
for langNum, langInfo in languageBatchDict.items():
newBatchSettingsDict[langNum]['translate_service'] = 'google'
newBatchSettingsDict[langNum]['formality'] = None
return newBatchSettingsDict
#======================================== Combine Subtitle Lines ================================================
def combine_subtitles_advanced(inputDict, maxCharacters=200):
charRateGoal = 20 #20
gapThreshold = 100 # The maximum gap between subtitles to combine
noMorePossibleCombines = False
# Convert dictionary to list of dictionaries of the values
entryList = []
for key, value in inputDict.items():
value['originalIndex'] = int(key)-1
entryList.append(value)
while not noMorePossibleCombines:
entryList, noMorePossibleCombines = combine_single_pass(entryList, charRateGoal, gapThreshold, maxCharacters)
# Convert the list back to a dictionary then return it
return dict(enumerate(entryList, start=1))
def combine_single_pass(entryListLocal, charRateGoal, gapThreshold, maxCharacters):
# Want to restart the loop if a change is made, so use this variable, otherwise break only if the end is reached
reachedEndOfList = False
noMorePossibleCombines = True # Will be set to False if a combination is made
# Use while loop because the list is being modified
while not reachedEndOfList:
# Need to update original index in here
for entry in entryListLocal:
entry['originalIndex'] = entryListLocal.index(entry)
# Will use later to check if an entry is the last one in the list, because the last entry will have originalIndex equal to the length of the list - 1
originalNumberOfEntries = len(entryListLocal)
# Need to calculate the char_rate for each entry, any time something changes, so put it at the top of this loop
entryListLocal = calc_list_speaking_rates(entryListLocal, charRateGoal)
# Sort the list by the difference in speaking speed from charRateGoal
priorityOrderedList = sorted(entryListLocal, key=itemgetter('char_rate_diff'), reverse=True)
# Iterates through the list in order of priority, and uses that index to operate on entryListLocal
# For loop is broken after a combination is made, so that the list can be re-sorted and re-iterated
for progress, data in enumerate(priorityOrderedList):
i = data['originalIndex']
# Check if last entry, and therefore will end loop when done with this iteration
if progress == len(priorityOrderedList) - 1:
reachedEndOfList = True
# Check if the current entry is outside the upper and lower bounds
if (data['char_rate'] > charRateGoal or data['char_rate'] < charRateGoal):
# Check if the entry is the first in entryListLocal, if so do not consider the previous entry
if data['originalIndex'] == 0:
considerPrev = False
else:
considerPrev = True
# Check if the entry is the last in entryListLocal, if so do not consider the next entry
if data['originalIndex'] == originalNumberOfEntries - 1:
considerNext = False
else:
considerNext = True
# Check if current entry is still in the list - if it has been combined with another entry, it will not be
# Get the char_rate of the next and previous entries, if they exist, and calculate the difference
# If the diff is positive, then it is lower than the current char_rate
try:
nextCharRate = entryListLocal[i+1]['char_rate']
nextDiff = data['char_rate'] - nextCharRate
except IndexError:
considerNext = False
nextCharRate = None
nextDiff = None
try:
prevCharRate = entryListLocal[i-1]['char_rate']
prevDiff = data['char_rate'] - prevCharRate
except IndexError:
considerPrev = False
prevCharRate = None
prevDiff = None
else:
continue
# Define functions for combining with previous or next entries - Generated with copilot, it's possible this isn't perfect
def combine_with_next():
entryListLocal[i]['text'] = entryListLocal[i]['text'] + ' ' + entryListLocal[i+1]['text']
entryListLocal[i]['translated_text'] = entryListLocal[i]['translated_text'] + ' ' + entryListLocal[i+1]['translated_text']
entryListLocal[i]['end_ms'] = entryListLocal[i+1]['end_ms']
entryListLocal[i]['end_ms_buffered'] = entryListLocal[i+1]['end_ms_buffered']
entryListLocal[i]['duration_ms'] = int(entryListLocal[i+1]['end_ms']) - int(entryListLocal[i]['start_ms'])
entryListLocal[i]['duration_ms_buffered'] = int(entryListLocal[i+1]['end_ms_buffered']) - int(entryListLocal[i]['start_ms_buffered'])
entryListLocal[i]['srt_timestamps_line'] = entryListLocal[i]['srt_timestamps_line'].split(' --> ')[0] + ' --> ' + entryListLocal[i+1]['srt_timestamps_line'].split(' --> ')[1]
del entryListLocal[i+1]
def combine_with_prev():
entryListLocal[i-1]['text'] = entryListLocal[i-1]['text'] + ' ' + entryListLocal[i]['text']
entryListLocal[i-1]['translated_text'] = entryListLocal[i-1]['translated_text'] + ' ' + entryListLocal[i]['translated_text']
entryListLocal[i-1]['end_ms'] = entryListLocal[i]['end_ms']
entryListLocal[i-1]['end_ms_buffered'] = entryListLocal[i]['end_ms_buffered']
entryListLocal[i-1]['duration_ms'] = int(entryListLocal[i]['end_ms']) - int(entryListLocal[i-1]['start_ms'])
entryListLocal[i-1]['duration_ms_buffered'] = int(entryListLocal[i]['end_ms_buffered']) - int(entryListLocal[i-1]['start_ms_buffered'])
entryListLocal[i-1]['srt_timestamps_line'] = entryListLocal[i-1]['srt_timestamps_line'].split(' --> ')[0] + ' --> ' + entryListLocal[i]['srt_timestamps_line'].split(' --> ')[1]
del entryListLocal[i]
# Choose whether to consider next and previous entries, and if neither then continue to next loop
if data['char_rate'] > charRateGoal:
# Check to ensure next/previous rates are lower than current rate, and the combined entry is not too long, and the gap between entries is not too large
# Need to add check for considerNext and considerPrev first, because if run other checks when there is no next/prev value to check, it will throw an error
if considerNext == False or nextDiff or nextDiff < 0 or (entryListLocal[i]['break_until_next'] >= gapThreshold) or (len(entryListLocal[i]['translated_text']) + len(entryListLocal[i+1]['translated_text']) > maxCharacters):
considerNext = False
try:
if considerPrev == False or not prevDiff or prevDiff < 0 or (entryListLocal[i-1]['break_until_next'] >= gapThreshold) or (len(entryListLocal[i-1]['translated_text']) + len(entryListLocal[i]['translated_text']) > maxCharacters):
considerPrev = False
except TypeError:
considerPrev = False
elif data['char_rate'] < charRateGoal:
# Check to ensure next/previous rates are higher than current rate
if considerNext == False or not nextDiff or nextDiff > 0 or (entryListLocal[i]['break_until_next'] >= gapThreshold) or (len(entryListLocal[i]['translated_text']) + len(entryListLocal[i+1]['translated_text']) > maxCharacters):
considerNext = False
try:
if considerPrev == False or not prevDiff or prevDiff > 0 or (entryListLocal[i-1]['break_until_next'] >= gapThreshold) or (len(entryListLocal[i-1]['translated_text']) + len(entryListLocal[i]['translated_text']) > maxCharacters):
considerPrev = False
except TypeError:
considerPrev = False
else:
continue
# Continue to next loop if neither are considered
if not considerNext and not considerPrev:
continue
# Should only reach this point if two entries are to be combined
if data['char_rate'] > charRateGoal:
# If both are to be considered, then choose the one with the lower char_rate
if considerNext and considerPrev:
if nextDiff < prevDiff:
combine_with_next()
noMorePossibleCombines = False
break
else:
combine_with_prev()
noMorePossibleCombines = False
break
# If only one is to be considered, then combine with that one
elif considerNext:
combine_with_next()
noMorePossibleCombines = False
break
elif considerPrev:
combine_with_prev()
noMorePossibleCombines = False
break
else:
print(f"Error U: Should not reach this point! Current entry = {i}")
print(f"Current Entry Text = {data['text']}")
continue
elif data['char_rate'] < charRateGoal:
# If both are to be considered, then choose the one with the higher char_rate
if considerNext and considerPrev:
if nextDiff > prevDiff:
combine_with_next()
noMorePossibleCombines = False
break
else:
combine_with_prev()
noMorePossibleCombines = False
break
# If only one is to be considered, then combine with that one
elif considerNext:
combine_with_next()
noMorePossibleCombines = False
break
elif considerPrev:
combine_with_prev()
noMorePossibleCombines = False
break
else:
print(f"Error L: Should not reach this point! Index = {i}")
print(f"Current Entry Text = {data['text']}")
continue
return entryListLocal, noMorePossibleCombines
#-- End of combine_single_pass --
#----------------------------------------------------------------------
# Calculate the number of characters per second for each subtitle entry
def calc_dict_speaking_rates(inputDict, dictKey='translated_text'):
tempDict = copy.deepcopy(inputDict)
for key, value in tempDict.items():
tempDict[key]['char_rate'] = round(len(value[dictKey]) / (int(value['duration_ms']) / 1000), 2)
return tempDict
def calc_list_speaking_rates(inputList, charRateGoal, dictKey='translated_text'):
tempList = copy.deepcopy(inputList)
for i in range(len(tempList)):
# Calculate the number of characters per second based on the duration of the entry
tempList[i]['char_rate'] = round(len(tempList[i][dictKey]) / (int(tempList[i]['duration_ms']) / 1000), 2)
# Calculate the difference between the current char_rate and the goal char_rate - Absolute Value
tempList[i]['char_rate_diff'] = abs(round(tempList[i]['char_rate'] - charRateGoal, 2))
return tempList