Created
March 20, 2023 23:51
-
-
Save markshust/fb78c1166791fe530d0d60bc00461ef3 to your computer and use it in GitHub Desktop.
Bash script to translate SRT files in the current directory 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 | |
languageName='Spanish' | |
language='es' | |
for file in *.srt; do | |
if [[ $file == *.$language.srt ]]; then | |
# Skip processing of already translated files | |
continue | |
fi | |
if [ ! -f "${file%.*}.$language.srt" ]; then | |
# Read input file and translate if necessary | |
echo "Translating $file to $languageName..." | |
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" | |
fi | |
done | |
echo "Translation complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment