A python translator for srt files.
Project description
SRT File translator
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.7.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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file srt_file_translator-1.1.0.tar.gz
.
File metadata
- Download URL: srt_file_translator-1.1.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8adba754143952d6687104ae2274c7822ed64e4e29311ecdc03a4f62939dae2a |
|
MD5 | 5f6d7f18ec08a1d41d9d80ceedc85fc4 |
|
BLAKE2b-256 | 28fa50ac46081047d05068f897a3dfbbd4589771a4b2c527d16140899d33119c |
File details
Details for the file srt_file_translator-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: srt_file_translator-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb55e736e879fe9c378224c89b3f0116a3f97f1f2dd974a6809f6498a2b3729d |
|
MD5 | bc19482946882ad1764d3827e3722f0e |
|
BLAKE2b-256 | 48e7c711633569d176d8950065e6908633211c49acd41b7848a1ff3bf4749a1d |