Machine translation for ASS subtitles via MS Translator on Azure
This python script uses the Microsoft Translator API (part of Azure Cognitive Services) to translate dialogue lines in ASS (Advanced Substation Alpha) subtitle files, from and to any of the supported languages. Some formatting tags and forced newlines may be sacrificed in the process.
Before using the script, you need to grab a key for the Translator service. You'll need an Azure account and set up the Translator service, then get one of the keys provided. The service allows translating some 2 million character per month for free. Should be more than enough for personal use of this script.
- Python 3.6 or newer
requests
python module, ex. installed via pip- Translator API key
For ease of use, it is recommended to set the API key as environment variable TRANSLATOR_KEY read by the script. Alternatively, you can provide it on each run as the --key
argument.
The general CLI pattern is
autosub [-h] -l LANGUAGES [-s] [-v] [--key KEY] input [output]
Use -h
to get help.
Required arguments:
- Input and output are paths to your files. Point input to the source .ass file and provide a path to write the output. Output is stdout by default.
- -l or --languages selects source and target language to configure the translator. Provided as
source:target
using laguage codes (ex.en:de
) that can also be listed via the Translator API.
Optional arguments:
- -s or --stream is a flag. Also, you probably shouln't use it. It tells the script to translate line by line instead of buffering more lines and sending them all at once. Maybe it's less memory intensive, maybe it's useless, but it's definitely slow.
- -v or --verbose is a flag. This enables printing status messages onto stdout reporting what's happening. Obviously, when file output is stdout, this doesn't do anything. You don't want statuses in your output.
- --key lets you provide the API key as an argument instead of setting it in environment.
No, of course not. Even if it doesn't screw up formatting or accidentaly skip lines, it's still just machine translation. Have fun.
- ...use the Microsoft translator? Because I found it maybe does a better job here. Also, getting the API to work wasn't a nightmare.
- ...make this? Because I'm a lazy retired fansubber. This is surprisingly useful for translating. It does a bulk of the work for you, then you just make it make sense.
If you can make sense of the code, feel free to make any improvements, fixes, complete rewrites, whatever. I'll be here.