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

Bilingual sub #57

Closed
ghost opened this issue Jul 25, 2023 · 3 comments
Closed

Bilingual sub #57

ghost opened this issue Jul 25, 2023 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 25, 2023

it would be good for language learners.

@sinedie
Copy link
Owner

sinedie commented Aug 11, 2023

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
Copy link
Author

ghost commented Aug 12, 2023

@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 sinedie pinned this issue Oct 15, 2023
@sinedie
Copy link
Owner

sinedie commented Oct 15, 2023

Seems to work, pinned in case someone needs it

@sinedie sinedie closed this as completed Oct 15, 2023
@sinedie sinedie changed the title feature req creating bilingual sub Bilingual sub Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant