srt_file_translator

SRT File translator

PyPI version License: MIT

An SRT translator for Python using Google Translate. This package breaks down given SRT formatted text into individual statments that are translated individually. Multi statement lines are broken down into single statement lines and statements that span multiple lines are aggregated into a single line. This allows google translate to translate each statement as a single unit and allows reasonable similarities to the original timing of the SRT file.

Setup

Make sure you have Python 3.10.x (or higher) installed on your system. You can download it here.

Installation

pip install srt_file_translator

Getting Started

srt_file_translator contains a basic Translator wrapper class that allows for easy translations of srt files. Technical Docs Here.

You need a Google Cloud API Key to use this library. You can create a new json key here. Additonal information on how to get started using google cloud translate can be found here.

Usage

Translator Usage:

# Import the Translator class
from srt_file_translator import Translator

# Initialize a translator object
translator = Translator(key_path="bq_key.json")

# Translate an srt file
translator.srt_file_translator(
    # The path to the srt file you want to translate
    source_file="test/myscript-en.srt",
    # The path to the output file
    target_file="test/myscript-es.srt",
    # The source language of the srt file
    source_language="en",
    # The target language of the output srt file
    target_language="es"
    # Optionally: Specify end of statement delimiter characters
    # for aggregating multi line statements into a single line
    statement_delimiters=['.', '?', '!']
    # Defaults to ['.', '?', '!']
)

Show Available Languages:

from srt_file_translator import Translator
translator = Translator(key_path="bq_key.json")
translator.show_languages()

Actual Example:

Consider the following srt file:

0
00:00:00,000 --> 00:00:00,500


1
00:00:00,500 --> 00:00:04,620
Welcome to an introduction on how to use

2
00:00:04,620 --> 00:00:06,600
the Translate SRT package.

3
00:00:06,600 --> 00:00:08,540
This is only an example SRT

4
00:00:08,540 --> 00:00:12,130
file for the purpose of this demonstration.

Using the following code:

from srt_file_translator import Translator
translator = Translator(key_path="bq_key.json")
translator.srt_file_translator(
    source_file="example_data/welcome_example-en.srt",
    target_file="example_data/welcome_example-es.srt",
    source_language="en",
    target_language="es"
)

Produces:

0
00:00:00,500 --> 00:00:06,600
Bienvenido a una introducción sobre cómo utilizar el paquete Translate SRT.

1
00:00:06,600 --> 00:00:12,130
Este es sólo un ejemplo de SRT.

Notice how the statements are aggregated into a single line before being translated. This allows google translate to translate the entire statement as a single unit. This is important because google translate will often translate individual words incorrectly if they are not in the context of an entire statement.

Additional Examples:

Translating all files in a folder to multiple languages:

# Import the Translator class
from srt_file_translator import Translator
# Import os for file system operations
import os

# Initialize a translator object
translator = Translator(key_path="bq_key.json")

# Specify the input and output folders
input_folder = "example_data"
output_folder = "example_output"

# Create the output folder if it doesn't exist
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# Specify the source language and target languages
source_language = "en"
target_languates = ["es", "fr", "de", "it", "pt", "ru", "zh", "ja", "ko"]

# Loop through all files in the input folder
for file in os.listdir(input_folder):
    # Loop through all target languages
    for target_language in target_languates:
        # Translate each input file to each target language
        translator.srt_file_translator(
            source_file=os.path.join(input_folder, file),
            target_file=os.path.join(output_folder, f"{os.path.splitext(file)[0]}-{target_language}.srt"),
            source_language=source_language,
            target_language=target_language
        )

License

Copyright 2024 Connor Makowski

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  1"""
  2
  3# SRT File translator
  4[![PyPI version](https://badge.fury.io/py/srt_file_translator.svg)](https://badge.fury.io/py/srt_file_translator)
  5[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
  6
  7An SRT translator for Python using Google Translate. This package breaks down given SRT formatted text into individual statments that are translated individually. Multi statement lines are broken down into single statement lines and statements that span multiple lines are aggregated into a single line. This allows google translate to translate each statement as a single unit and allows reasonable similarities to the original timing of the SRT file.
  8
  9# Setup
 10
 11Make sure you have Python 3.10.x (or higher) installed on your system. You can download it [here](https://www.python.org/downloads/).
 12
 13### Installation
 14
 15```
 16pip install srt_file_translator
 17```
 18
 19# Getting Started
 20
 21`srt_file_translator` contains a basic `Translator` wrapper class that allows for easy translations of srt files. [Technical Docs Here](https://connor-makowski.github.io/srt_file_translator/srt_file_translator.html).
 22
 23You need a Google Cloud API Key to use this library. You can create a new json key [here](https://console.cloud.google.com/apis/credentials/serviceaccountkey). Additonal information on how to get started using google cloud translate can be found [here](https://cloud.google.com/translate/docs/setup).
 24
 25# Usage
 26
 27## Translator Usage:
 28```py
 29# Import the Translator class
 30from srt_file_translator import Translator
 31
 32# Initialize a translator object
 33translator = Translator(key_path="bq_key.json")
 34
 35# Translate an srt file
 36translator.srt_file_translator(
 37    # The path to the srt file you want to translate
 38    source_file="test/myscript-en.srt",
 39    # The path to the output file
 40    target_file="test/myscript-es.srt",
 41    # The source language of the srt file
 42    source_language="en",
 43    # The target language of the output srt file
 44    target_language="es"
 45    # Optionally: Specify end of statement delimiter characters
 46    # for aggregating multi line statements into a single line
 47    statement_delimiters=['.', '?', '!']
 48    # Defaults to ['.', '?', '!']
 49)
 50```
 51
 52## Show Available Languages:
 53```py
 54from srt_file_translator import Translator
 55translator = Translator(key_path="bq_key.json")
 56translator.show_languages()
 57```
 58
 59## Actual Example:
 60
 61Consider the following srt file:
 62
 63```srt
 640
 6500:00:00,000 --> 00:00:00,500
 66
 67
 681
 6900:00:00,500 --> 00:00:04,620
 70Welcome to an introduction on how to use
 71
 722
 7300:00:04,620 --> 00:00:06,600
 74the Translate SRT package.
 75
 763
 7700:00:06,600 --> 00:00:08,540
 78This is only an example SRT
 79
 804
 8100:00:08,540 --> 00:00:12,130
 82file for the purpose of this demonstration.
 83
 84```
 85
 86Using the following code:
 87  
 88```py
 89from srt_file_translator import Translator
 90translator = Translator(key_path="bq_key.json")
 91translator.srt_file_translator(
 92    source_file="example_data/welcome_example-en.srt",
 93    target_file="example_data/welcome_example-es.srt",
 94    source_language="en",
 95    target_language="es"
 96)
 97```
 98
 99Produces:
100
101```srt
1020
10300:00:00,500 --> 00:00:06,600
104Bienvenido a una introducción sobre cómo utilizar el paquete Translate SRT.
105
1061
10700:00:06,600 --> 00:00:12,130
108Este es sólo un ejemplo de SRT.
109```
110
111Notice how the statements are aggregated into a single line before being translated. This allows google translate to translate the entire statement as a single unit. This is important because google translate will often translate individual words incorrectly if they are not in the context of an entire statement.
112
113## Additional Examples:
114
115### Translating all files in a folder to multiple languages:
116
117```py
118# Import the Translator class
119from srt_file_translator import Translator
120# Import os for file system operations
121import os
122
123# Initialize a translator object
124translator = Translator(key_path="bq_key.json")
125
126# Specify the input and output folders
127input_folder = "example_data"
128output_folder = "example_output"
129
130# Create the output folder if it doesn't exist
131if not os.path.exists(output_folder):
132    os.makedirs(output_folder)
133
134# Specify the source language and target languages
135source_language = "en"
136target_languates = ["es", "fr", "de", "it", "pt", "ru", "zh", "ja", "ko"]
137
138# Loop through all files in the input folder
139for file in os.listdir(input_folder):
140    # Loop through all target languages
141    for target_language in target_languates:
142        # Translate each input file to each target language
143        translator.srt_file_translator(
144            source_file=os.path.join(input_folder, file),
145            target_file=os.path.join(output_folder, f"{os.path.splitext(file)[0]}-{target_language}.srt"),
146            source_language=source_language,
147            target_language=target_language
148        )
149```
150
151
152# License
153
154Copyright 2024 Connor Makowski
155
156Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
157
158The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
159
160THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
161"""
162from .translator import Translator, SRT_Utils