sinedie / SRTranslator

SRT files translator
Do What The F*ck You Want To Public License
219 stars 27 forks source link

Bilingual sub #57

Closed ghost closed 1 year ago

ghost commented 1 year ago

it would be good for language learners.

sinedie commented 1 year ago

Ok... yeah, that can be nice... A workaround could be to translate the file an then merge... but the wrap and line character limits then should be disabled as one sentence on two languages is not gonna be the same length.

We can find a woraround, sure

ghost commented 1 year ago

@sinedie I have already a script that does it here

def create_bilingual_subs_and_move(srt_file, untranslated_path, sub_index):
    original_srt_file = srt_file.with_suffix('.srt')
    translated_srt_file = srt_file.with_stem(srt_file.stem + '_tr').with_suffix('.srt')
    bilingual_srt_file = srt_file.with_stem(srt_file.stem + '_tr_bilingual').with_suffix('.srt')

    original_subs = pysubs2.load(str(original_srt_file), encoding="utf-8")
    translated_subs = pysubs2.load(str(translated_srt_file), encoding="utf-8")

    for t_line in translated_subs:
        nearest_cue = find_nearest_cue(t_line.start, original_subs)
        if nearest_cue:
            t_line.text = t_line.text + "\\N" + nearest_cue.text
    translated_subs.save(str(bilingual_srt_file), encoding="utf-8")

    new_bilingual_srt_file = srt_file.with_stem(srt_file.stem.rstrip('_')).with_suffix('.srt')
    bilingual_srt_file.rename(new_bilingual_srt_file)

    original_location = find_original_location(srt_file, sub_index)
    shutil.move(str(new_bilingual_srt_file), str(original_location))

    os.remove(str(untranslated_path / (srt_file.stem + '_tr.srt')))
    if translated_srt_file.exists():
        os.remove(str(translated_srt_file))

def find_nearest_cue(timestamp, original_subs):
    nearest_cue = None
    min_time_diff = float('inf')

    for line in original_subs:
        time_diff = abs(line.start - timestamp)
        if time_diff < min_time_diff:
            min_time_diff = time_diff
            nearest_cue = line

    return nearest_cue
sinedie commented 1 year ago

Seems to work, pinned in case someone needs it