Created
March 20, 2023 20:30
-
-
Save markshust/21f954828a4bcbc7106d5de2f3128857 to your computer and use it in GitHub Desktop.
Translate SRT file into another language using Google Translate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires translate-shell to be installed. | |
# Get it on GitHub at: https://github.com/soimort/translate-shell | |
# Parse command-line arguments | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
--file|-f ) file="$2"; shift ;; | |
--language|-l ) language="$2"; shift ;; | |
* ) echo "Unknown option: $1"; exit 1 ;; | |
esac | |
shift | |
done | |
# Check that required arguments are provided | |
if [[ -z "$file" ]]; then | |
echo "Error: Input file not specified." | |
echo "Usage: $0 --file <input_file> --language <language_code>" | |
exit 1 | |
fi | |
if [[ -z "$language" ]]; then | |
echo "Error: Language code not specified." | |
echo "Usage: $0 --file <input_file> --language <language_code>" | |
exit 1 | |
fi | |
if [[ ! -f "$file" ]]; then | |
echo "Error: Input file '$file' does not exist or is not a regular file." | |
exit 1 | |
fi | |
if ! [[ $language =~ ^[a-z]{2}$ ]]; then | |
echo "Error: Language code '$language' is not valid. Please use a two-letter language code (ISO 639-1)." | |
exit 1 | |
fi | |
# Read input file and translate if necessary | |
while read -r line; do | |
if [[ "$line" =~ [a-z] ]] ; then | |
translated=$(echo "$line" | trans -brief ":$language") | |
if [[ $? -ne 0 ]]; then | |
echo "Error: Translation failed for '$line'. Skipping." | |
else | |
echo "$translated" >> "${file%.*}.$language.srt" | |
fi | |
else | |
echo "$line" >> "${file%.*}.$language.srt" | |
fi | |
done < "$file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment