### Package SRTranslator GUI from source Source: https://github.com/sinedie/srtranslator/blob/master/README.md Provides instructions for packaging the SRTranslator GUI application from its source code. This involves navigating to the GUI directory, installing dependencies, and using `flet pack` to create executables. ```bash cd ./GUI pip install -r requirements.txt pip install pyinstaller flet pack main.py cp -r ./assets ./dist/assets ``` -------------------------------- ### Install SRTranslator using pip Source: https://github.com/sinedie/srtranslator/blob/master/README.md Installs the SRTranslator package from PyPI using pip. This is the standard method for adding the library to your Python environment. ```bash pip install srtranslator ``` -------------------------------- ### Implement a Custom Translator Source: https://context7.com/sinedie/srtranslator/llms.txt Demonstrates how to create a custom translator by extending the base `Translator` class. The `translate` method must be implemented to integrate with any translation service. This example includes a placeholder for custom API calls and resource cleanup in the `quit` method. ```python from srtranslator import SrtFile from srtranslator.translators.base import Translator class MyCustomTranslator(Translator): # Maximum characters per translation request max_char = 5000 def __init__(self, api_key=None): self.api_key = api_key def translate(self, text: str, source_language: str, destination_language: str) -> str: # Implement your translation logic here # For example, call a custom API: # response = requests.post("https://my-api.com/translate", json={ # "text": text, # "source": source_language, # "target": destination_language # }) # return response.json()["translated_text"] return text # Placeholder def quit(self): # Clean up resources if needed pass # Use custom translator translator = MyCustomTranslator(api_key="your-key") sub = SrtFile("./subtitles.srt") sub.translate(translator, "en", "es") sub.save("./subtitles_translated.srt") translator.quit() ``` -------------------------------- ### SRTranslator CLI: Basic Translation Source: https://context7.com/sinedie/srtranslator/llms.txt Provides examples of using the SRTranslator command-line interface for basic subtitle translation. It shows how to translate an SRT file from English to Spanish using the default DeepL scraper, and how to translate an ASS file with automatic source language detection. ```bash # Basic SRT translation (English to Spanish using DeepL scraper) python -m srtranslator ./movie.srt -i en -o es # Translate ASS file with auto-detect source language python -m srtranslator ./anime.ass -o ja ``` -------------------------------- ### Translate subtitles using TranslatePy (Multi-Service) Source: https://context7.com/sinedie/srtranslator/llms.txt Employs the TranslatePy class for translations via Google Translate and other services without needing an API key. This example shows batch translation of SRT files from a directory. ```python import os import glob from srtranslator import SrtFile from srtranslator.translators.translatepy import TranslatePy # Initialize - no API key needed translator = TranslatePy() folder = "srt_test/" for filepath in glob.glob(os.path.join(folder, "**/*.srt"), recursive=True): srt = SrtFile(filepath) srt.translate(translator, "en", "es") srt.wrap_lines() srt.save(f"{os.path.splitext(filepath)[0]}_translated.srt") translator.quit() ``` -------------------------------- ### Implement Custom Translator in Python Source: https://github.com/sinedie/srtranslator/blob/master/docs/create_translator.md This Python code snippet demonstrates how to create a custom translator by inheriting from the `Translator` base class. It includes an example of the mandatory `translate` method and an optional `max_char` attribute. The `translate` method is where the core translation logic should be implemented. ```python from stranslator.translators.base import Translator class CustomTranslator(Translator): # This is a limitation cause not all translators can translate more than a few characters max_char: int = 5000 def translate(self, text: str, source_language: str, destination_language: str): print("Do your magic here. Call an API, piglatin it, whatever, do WTF you want") ``` -------------------------------- ### Initialize PyDeepLX with Proxy Support Source: https://context7.com/sinedie/srtranslator/llms.txt Initializes the PyDeepLX translator with proxy support enabled. This is useful for rotating IP addresses to avoid rate limiting or access geo-restricted services. It then loads an SRT file, translates it from English to French, wraps lines, and saves the translated file. ```python from srtranslator import PyDeepLX, SrtFile # Initialize with proxy support enabled translator = PyDeepLX(proxies=True) sub = SrtFile("./movie.srt") sub.translate(translator, "en", "fr") # English to French sub.wrap_lines() sub.save("./movie_french.srt") ``` -------------------------------- ### Initialize a translator instance Source: https://github.com/sinedie/srtranslator/blob/master/README.md Demonstrates how to initialize a translator object. SRTranslator supports multiple translation backends, including DeeplTranslator, TranslatePy, DeeplApi (requiring an API key), and PyDeepLX. ```python translator = DeeplTranslator() # or TranslatePy() or DeeplApi(api_key) or DeepLX() ``` -------------------------------- ### SRTranslator CLI: Advanced Translator and Options Source: https://context7.com/sinedie/srtranslator/llms.txt Illustrates advanced usage of the SRTranslator CLI, including selecting different translators (TranslatePy, DeepL API, PyDeepLX), using authentication keys, enabling proxy support, and configuring verbose or debug output. It also shows how to set a custom line wrap limit. ```bash # Use TranslatePy (Google Translate) with verbose output python -m srtranslator ./subtitles.srt -i en -o fr -t translatepy -v # Use DeepL API with authentication python -m srtranslator ./video.srt -i en -o de -t deepl-api --auth YOUR_API_KEY # Use PyDeepLX with proxy support python -m srtranslator ./film.srt -i en -o it -t pydeeplx --proxies # Custom line wrap limit and debug output python -m srtranslator ./subtitles.srt -i en -o pt -w 60 -vv # Show browser window during translation (for debugging) python -m srtranslator ./movie.srt -i en -o es -s ``` -------------------------------- ### SRTranslator command line advanced usage help Source: https://github.com/sinedie/srtranslator/blob/master/README.md Displays the help message for the SRTranslator command-line interface, outlining all available arguments and options for translation, including language selection, verbosity, line wrapping, translator choice, and authentication. ```bash usage: __main__.py [-h] [-i SRC_LANG] [-o DEST_LANG] [-v] [-vv] [-s] [-w WRAP_LIMIT] [-t {deepl-scrap,translatepy,deepl-api,pydeeplx}] [--auth AUTH] path Translate an .STR and .ASS file positional arguments: path File to translate options: -h, --help show this help message and exit -i SRC_LANG, --src-lang SRC_LANG Source language. Default: auto -o DEST_LANG, --dest-lang DEST_LANG Destination language. Default: es (spanish) -v, --verbose Increase output verbosity -vv, --debug Increase output verbosity for debugging -s, --show-browser Show browser window -w WRAP_LIMIT, --wrap-limit WRAP_LIMIT Number of characters -including spaces- to wrap a line of text. Default: 50 -t {deepl-scrap,translatepy,deepl-api}, --translator {deepl-scrap,translatepy,deepl-api,pydeeplx} Built-in translator to use --auth AUTH Api key if needed on translator --proxies Use proxy by default for pydeeplx ``` -------------------------------- ### Translate subtitle files using command line Source: https://github.com/sinedie/srtranslator/blob/master/README.md Demonstrates how to use SRTranslator directly from the command line to translate SRT or ASS files. It specifies the input file, source language, and destination language. ```bash # SRT file python -m srtranslator ./filepath/to/srt -i SRC_LANG -o DEST_LANG # ASS file python -m srtranslator ./filepath/to/ass -i SRC_LANG -o DEST_LANG ``` -------------------------------- ### TranslatePy Python API Usage Source: https://github.com/sinedie/srtranslator/blob/master/docs/translatepy.md Demonstrates how to instantiate and use the TranslatePy translator in Python. It covers initializing the translator, performing translations, and cleaning up resources. No external dependencies are explicitly mentioned beyond the srtranslator library itself. ```python from srtranslator.translators.translatepy import TranslatePy translator = TranslatePy() translator.translate(text, source_language, destination_language) translator.quit() ``` -------------------------------- ### Translate subtitles using PyDeepLX (DeepL Alternative) Source: https://context7.com/sinedie/srtranslator/llms.txt Utilizes the PyDeepLX class for DeepL translations without official API access. It incorporates automatic proxy rotation and retry mechanisms for robust performance. ```python from srtranslator import SrtFile from srtranslator.translators.pydeeplx import PyDeepLX ``` -------------------------------- ### Handle ASS files with AssFile class Source: https://context7.com/sinedie/srtranslator/llms.txt Shows how to process Advanced SubStation Alpha (.ass) subtitle files, preserving styling tags during translation using the AssFile class and TranslatePy. It handles line breaks and saves the translated file. ```python import os from srtranslator.ass_file import AssFile from srtranslator.translators.translatepy import TranslatePy # Initialize translator (TranslatePy uses Google Translate) translator = TranslatePy() # Load ASS file filepath = "./anime_subtitles.ass" sub = AssFile(filepath) # Translate from Japanese to English sub.translate(translator, "ja", "en") # Process line breaks sub.wrap_lines() # Save translated file sub.save(f"{os.path.splitext(filepath)[0]}_translated.ass") translator.quit() ``` -------------------------------- ### Handle SRT files with SrtFile class Source: https://context7.com/sinedie/srtranslator/llms.txt Demonstrates loading, translating, line wrapping, and saving SRT subtitle files using the SrtFile class and DeeplTranslator. It manages backups and can resume interrupted translations. ```python import os from srtranslator import SrtFile from srtranslator.translators.deepl_scrap import DeeplTranslator # Initialize translator translator = DeeplTranslator() # Load SRT file filepath = "./movie_subtitles.srt" sub = SrtFile(filepath) # Translate from English to Spanish sub.translate(translator, "en", "es") # Wrap lines at 50 characters for better display sub.wrap_lines(50) # Save translated file sub.save(f"{os.path.splitext(filepath)[0]}_translated.srt") # Clean up translator resources translator.quit() ``` -------------------------------- ### Load, translate, and save subtitle files Source: https://github.com/sinedie/srtranslator/blob/master/README.md Shows the core workflow for translating subtitle files using SRTranslator. It covers loading an SRT or ASS file, performing the translation to a specified language, optionally improving line wrapping, and saving the translated file. ```python filepath = "./filepath/to/srt" # SRT File sub = SrtFile(filepath) # ASS File sub = AssFile(filepath) # Translate sub.translate(translator, "en", "es") # Making the result subtitles prettier sub.wrap_lines() sub.save(f"{os.path.splitext(filepath)[0]}_translated.srt") ``` -------------------------------- ### Translate Text with DeepL Translator (Python) Source: https://github.com/sinedie/srtranslator/blob/master/docs/deepl_scrap.md This snippet demonstrates how to initialize and use the DeeplTranslator class from the srtranslator library in Python. It shows how to create a translator instance, perform translations, and properly close the driver. The driver is optional; if not provided, a new Firefox driver is created and configured with free proxies. ```python from srtranslator.translators.deepl_scrap import DeeplTranslator driver = None # Or provide your own webdriver instance translator = DeeplTranslator(driver=driver) text_to_translate = "Hello, world!" source_lang = "en" dest_lang = "fr" translated_text = translator.translate(text_to_translate, source_lang, dest_lang) print(f"Translated text: {translated_text}") translator.quit() ``` -------------------------------- ### Translate Text with DeepL API (Python) Source: https://github.com/sinedie/srtranslator/blob/master/docs/deepl_api.md Demonstrates how to initialize and use the DeeplApi class for text translation. It requires an API key and optionally accepts source and destination languages. The driver is managed automatically if not provided. ```python from srtranslator.translators.deepl_api import DeeplApi translator = DeeplApi(api_key='your_api_key') # As a recomendation, put in in a .env file and load it with python-dotenv translator.translate(text, source_language, destination_language) translator.quit() # Totally optional ``` -------------------------------- ### Translate SRT Files using DeepL API via CLI Source: https://github.com/sinedie/srtranslator/blob/master/docs/deepl_api.md Shows how to use the SRTranslator command-line interface to translate SRT files using the DeepL API. Requires specifying the translator, authentication key, source and target languages, and the path to the SRT file. ```bash python -m srtranslator --t deepl-api --auth YOUR_API_KEY -i src_lang -o target_lang /path/to/srt ``` ```bash python -m srtranslator --translator deepl-api --auth YOUR_API_KEY -i src_lang -o target_lang /path/to/srt ``` -------------------------------- ### Configure Custom Proxies for DeepL Scraping Source: https://context7.com/sinedie/srtranslator/llms.txt Shows how to configure custom proxies for the `DeeplTranslator` to use specific country endpoints (e.g., US or GB) for web scraping. This can help in avoiding rate limiting or accessing different regional versions of the DeepL website. The code iterates through SRT files in a folder, translates them, and saves the output. ```python import os import glob from srtranslator import SrtFile from srtranslator.translators.deepl_scrap import DeeplTranslator from srtranslator.translators.selenium_utils import create_proxy, create_driver folder = "srt_test/" for filepath in glob.glob(os.path.join(folder, "**/*.srt"), recursive=True): # Create proxy from specific countries (US or GB) # Country IDs from https://www.sslproxies.org/ proxy = create_proxy(country_id=["US", "GB"]) driver = create_driver(proxy) translator = DeeplTranslator(driver) srt = SrtFile(filepath) srt.translate(translator, "en", "es") srt.wrap_lines() srt.save(f"{os.path.splitext(filepath)[0]}_translated.srt") translator.quit() ``` -------------------------------- ### TranslatePy CLI Usage Source: https://github.com/sinedie/srtranslator/blob/master/docs/translatepy.md Shows how to invoke the TranslatePy translator from the command line interface. It specifies the required arguments for translator selection, source and target languages, and the input SRT file path. This functionality is part of the srtranslator module. ```bash python -m srtranslator --t translatepy -i src_lang -o target_lang /path/to/srt ``` ```bash python -m srtranslator --translator translatepy -i src_lang -o target_lang /path/to/srt ``` -------------------------------- ### Translate subtitles using DeeplApi (Official API) Source: https://context7.com/sinedie/srtranslator/llms.txt Utilizes the official DeepL API for high-quality translations. Requires a DeepL API key and demonstrates batch translation of multiple SRT files in a folder. ```python import os import glob from srtranslator import SrtFile from srtranslator.translators.deepl_api import DeeplApi # Initialize with your DeepL API key translator = DeeplApi(api_key="your-deepl-api-key") # Batch translate multiple files folder = "srt_files/" for filepath in glob.glob(os.path.join(folder, "**/*.srt"), recursive=True): srt = SrtFile(filepath) srt.translate(translator, "en", "de") # English to German srt.wrap_lines() srt.save(f"{os.path.splitext(filepath)[0]}_german.srt") ``` -------------------------------- ### Translate SRT File using DeepL Translator (CLI) Source: https://github.com/sinedie/srtranslator/blob/master/docs/deepl_scrap.md This snippet shows how to use the SRTranslator from the command line to translate an SRT file using the DeepL translator. It specifies source and target languages and the path to the SRT file. The `--translator deepl-scrap` flag explicitly selects the DeepL translator. ```bash python -m srtranslator -i src_lang -o target_lang /path/to/srt ``` ```bash python -m srtranslator --translator deepl-scrap -i src_lang -o target_lang /path/to/srt ``` -------------------------------- ### Import SRTranslator modules for script usage Source: https://github.com/sinedie/srtranslator/blob/master/README.md Imports necessary classes and translator modules from the SRTranslator library for use in Python scripts. It includes classes for SRT and ASS file handling, as well as various translation service integrations. ```python import os # SRT File from srtranslator import SrtFile # ASS File from srtranslator import AssFile from srtranslator.translators.deepl_api import DeeplApi from srtranslator.translators.deepl_scrap import DeeplTranslator from srtranslator.translators.translatepy import TranslatePy from srtranslator.translators.pydeeplx import PyDeepLX ``` -------------------------------- ### Integrate Tor for Anonymous DeepL Translations Source: https://context7.com/sinedie/srtranslator/llms.txt Explains how to use Tor for anonymous translations via the DeepL web interface. It configures Firefox to use the Tor SOCKS proxy (default port 9050) and then uses the `DeeplTranslator` to translate SRT files. The translations are saved with a suffix indicating they are translated via Tor. ```python import os import glob from selenium import webdriver from srtranslator import SrtFile from srtranslator.translators.deepl_scrap import DeeplTranslator # Configure Firefox to use Tor SOCKS proxy profile = webdriver.FirefoxProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.socks", "127.0.0.1") profile.set_preference("network.proxy.socks_port", 9050) profile.set_preference("network.proxy.socks_version", 5) profile.update_preferences() driver = webdriver.Firefox(firefox_profile=profile) translator = DeeplTranslator(driver=driver) folder = "srt_test/" for filepath in glob.glob(os.path.join(folder, "**/*.srt"), recursive=True): srt = SrtFile(filepath) srt.translate(translator, "en", "es") srt.wrap_lines(50) srt.save(f"{os.path.splitext(filepath)[0]}_translated.srt") translator.quit() ``` -------------------------------- ### Translate subtitles using DeeplTranslator (Web Scraper) Source: https://context7.com/sinedie/srtranslator/llms.txt Basic usage of the DeeplTranslator class for free DeepL translation via web scraping with Selenium. It includes automatic proxy rotation and handles SRT file translation and saving. ```python from srtranslator import SrtFile from srtranslator.translators.deepl_scrap import DeeplTranslator # Basic usage - creates internal browser with automatic proxy translator = DeeplTranslator() sub = SrtFile("./subtitles.srt") sub.translate(translator, "auto", "es") # Auto-detect source language sub.wrap_lines() sub.save("./subtitles_spanish.srt") translator.quit() ``` -------------------------------- ### Quit the translator instance Source: https://github.com/sinedie/srtranslator/blob/master/README.md Ensures that the translator instance is properly closed or quit after use. This is important for releasing resources, especially for translators that might manage browser instances or network connections. ```python translator.quit() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.