Overview of SRTranslator
master.srt and .ass formats.repository·master·Indexed 42 days ago
https://github.com/sinedie/srtranslatorA tool for translating .srt and .ass subtitle files using translation engines such as DeepL, TranslatePy, and PyDeepLX. It provides a Command Line Interface (CLI), a Python library for programmatic translation, and a GUI powered by flet. Features include support for custom translator classes, line wrapping, and batch processing of subtitle directories.
.srt and .ass formats.To implement a custom translator, create a class that inherits from stranslator.translators.base.Translator. You must implement a translate method with the following signature:
translate(self, text: str, source_language: str, destination_language: str)
Optional features:
__init__ method to handle setup (e.g., API keys).max_char attribute: You can define a max_char: int attribute to specify the maximum character limit for a single translation request. This is useful for APIs with strict length constraints.Once defined, your custom translator can be used interchangeably with the built-in translators provided by the library.
from stranslator.translators.base import Translator
class CustomTranslator(Translator):
# Optional: define character limit for the translator
max_char: int = 5000
def translate(self, text: str, source_language: str, destination_language: str):
# Implement your translation logic here
# e.g., calling an external API
return "translated text"The GUI is powered by flet. To package your own executable, navigate to the GUI folder and run the following commands:
flet pack to build the application.assets folder to the dist directory to ensure the UI renders correctly.cd ./GUI
pip install -r requirements.txt
pip install pyinstaller
flet pack main.py
cp -r ./assets ./dist/assetsYou can programmatically translate .srt or .ass files by importing the appropriate file class and a translator implementation.
Supported translator imports include:
DeeplApi (requires API key)DeeplTranslator (scraping based)TranslatePyPyDeepLXWorkflow:
SrtFile or AssFile..translate(translator, src_lang, dest_lang)..wrap_lines() to improve subtitle formatting..save(new_filepath) to write the result.translator.quit() to clean up resources.import os
from srtranslator import SrtFile, AssFile
from srtranslator.translators.deepl_scrap import DeeplTranslator
filepath = "./filepath/to/srt"
# Load
sub = SrtFile(filepath)
# Translate
translator = DeeplTranslator()
sub.translate(translator, "en", "es")
# Formatting and Saving
sub.wrap_lines()
sub.save(f"{os.path.splitext(filepath)[0]}_translated.srt")
# Cleanup
translator.quit()Install the srtranslator package from PyPI using pip.
pip install srtranslatorWhen a translation task is initiated, the application creates a TranslationProgress instance for each file. This object manages the lifecycle of a single file's translation:
filepath, translator_id, src_lang, dest_lang, and wrap_limit.show_progress(total, progress) method to update a visual progress bar.self.cancelled = True and prevents further processing for that file.The GUI allows users to select either specific files or an entire directory.
.srt or .ass files..srt and .ass files within that directory tree using glob.handle_translation function iterates through all identified files, creating a TranslationProgress object for each and executing the translation sequentially.wrap_limit setting controls how lines are wrapped in the resulting subtitle file. In the GUI, this is managed via a counter component that allows users to increment or decrement the value. This value is passed to the sub.wrap_lines(self.wrap_limit) method during the translation process.TranslatePy translator, be aware that excessive usage may result in being banned by the service provider.The DeeplTranslator class allows you to translate text by scraping DeepL. If you do not provide a driver argument, the class will automatically create a new Firefox driver using geckodriver (installing it and adding it to the PATH if necessary) and set up free proxies to prevent IP bans.
Use the .translate(text, source_language, destination_language) method to perform translations and ensure you call .quit() to clean up the driver instance.
from srtranslator.translators.deepl_scrap import DeeplTranslator
translator = DeeplTranslator(driver=driver)
translator.translate(text, source_language, destination_language)
translator.quit()