Skip to content

Command Line Interface

translate_subtitles_cli()

Command Line Interface for Neural Subtitles Translation.

Parameters:

Name Type Description Default
-h --help

Show CLI help message

required
-i --input

Path to the input movie (if ends with .mp4) or srt file (if ends with .srt) containing subtitles to be translated

required
-o --output

Path to the output movie (if ends with .mp4 and input is a movie) or srt file (if ends with .srt) containing translated subtitles

required
-s --source

Code for the language of source subtitles (the source MP4 must only contain one track of subtitles)

required
-t --target

Code for the target language after translation.

required
Example

With the following command, subtitles from 'video.mp4' will be extracted and translated from 'french' to 'english'. The video containing translated subtitles will be saved as 'translated_video.mp4':

subtitles_translator -i video.mp4 -o translated_video.mp4 -s fr -t en
Source code in subtitles_translator/cli.py
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
def translate_subtitles_cli() -> None:
    """Command Line Interface for Neural Subtitles Translation.

    Parameters:
        -h (--help): Show CLI help message
        -i (--input): Path to the input movie (if ends with .mp4) or srt file (if ends with .srt) containing subtitles to be translated
        -o (--output): Path to the output movie (if ends with .mp4 and input is a movie) or srt file (if ends with .srt) containing translated subtitles
        -s (--source): Code for the language of source subtitles (the source MP4 must only contain one track of subtitles)
        -t (--target): Code for the target language after translation.

    Example:
        With the following command, subtitles from 'video.mp4' will be extracted and translated from 'french' to 'english'. The video containing translated subtitles will be saved as 'translated_video.mp4':

        ```shell
        subtitles_translator -i video.mp4 -o translated_video.mp4 -s fr -t en
        ```

    """

    parser = argparse.ArgumentParser(
        description="Translate the subtitles of the source movie and save a new movie file with translated subtitles."
    )
    parser.add_argument(
        "-i",
        "--input",
        type=str,
        required=True,
        help=(
            "path to the input movie (if ends with .mp4) or srt files (if ends with .srt) containing subtitles to be"
            " translated"
        ),
    )
    parser.add_argument(
        "-o",
        "--output",
        type=str,
        required=True,
        help=(
            "desired path to the output movie (if ends with .mp4 and input is a movie) or srt files (if ends with .srt)"
            " containing translated subtitles"
        ),
    )
    parser.add_argument(
        "-s", "--source", type=str, required=True, help="language of the input subtitles (source language)"
    )
    parser.add_argument("-t", "--target", type=str, required=True, help="target language")

    args = parser.parse_args()

    if args.input.endswith(".mp4"):
        source_srt = extract_srt(args.input).split("\n")
    elif args.input.endswith(".srt"):
        if args.output.endswith(".mp4"):
            raise ValueError("MP4 output file inconsistent with SRT file input.")
        with open(args.input, "r+") as srt_file:
            source_srt = srt_file.read().splitlines()
    else:
        raise ValueError("Input file format not supported.")

    source_language = AvailableLanguages(args.source)
    target_language = AvailableLanguages(args.target)

    translator = Translator(source_language=source_language, target_language=target_language)
    subtitles = Subtitles(source_srt)

    translator.translate_subtitles(subtitles)

    if args.output.endswith(".mp4"):
        subtitles.save_srt()
        insert_srt(args.input, args.output, "translated.srt")
    elif args.output.endswith(".srt"):
        subtitles.save_srt(args.output)
    else:
        raise ValueError("Input file format not supported.")