-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslateYouTubeTranscript.sh
executable file
·126 lines (110 loc) · 4.46 KB
/
TranslateYouTubeTranscript.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/zsh
#================================================================================
# AUTHOR
# Clint Box
# https://www.youtube.com/bearcatjamboree
#
# FUNCTION
# Translate a YouTube video transcript to another language
#
# DETAILS
# This script will download a translated transcript from a video on YouTube,
# as long as subtitles are enable for the video. The transcript and an SRT
# version will both be produced.
#
# USAGE
# ${SCRIPT_NAME} "<language code>" "<video URL or YouTube ID>" "<output folder>"
#================================================================================
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo ${machine}
lang_code="$1"
video_provided="$2"
output_folder="$3"
declare -A languages
row=1
while IFS="|" read -r head lang code tail
do
if [[ "$row" -gt "2" ]]; then
lang=$(echo $lang | sed 's/^[ \t]*//;s/[ \t]*$//')
code=$(echo $code | sed 's/^[ \t]*//;s/[ \t]*$//')
languages[$lang]="$code"
fi
row=$row+1
done < "LanguageCodes.md"
# Sort languages to alphabetical order
joined=$(printf ", \"%s\"" $(echo "${(@k)languages}" | tr " " "\n" | sort | tr "\n" " "))
#echo "${joined:2}"
##############################################################################
# Check for voice to use
###############################################################################
if [[ "$lang_code" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
language=$(osascript -e 'return choose from list { '${joined:2}' }')
elif [[ "$machine" == "Linux" ]]; then
language=$(dialog --title "Choose a file" --stdout --title "Please choose a language to process" --fselect ~/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
language=$(dialog --title "Choose a file" --stdout --title "Please choose a language to process" --fselect ~/ 14 48)
else
echo "Usage: $0 language [video URL or ID] output_folder"
exit 1
fi
lang_code="${languages[$language]}"
fi
##############################################################################
# Prompt for video_provided
###############################################################################
if [[ "$video_provided" == "" ]]; then
if [[ "$machine" == "Mac" ]]; then
video_provided=$(osascript -e 'set T to text returned of (display dialog "Enter YouTube video url or ID:" buttons {"Cancel", "OK"} default button "OK" default answer "")')
elif [[ "$machine" == "Linux" ]]; then
video_provided=$(dialog --title "Enter YouTube video url or ID:" --inputbox "video_provided:" 8 60)
elif [[ "$machine" == "Cygwin" ]]; then
video_provided=$(dialog --title "Enter YouTube video url or ID:" --inputbox "video_provided:" 8 60)
else
echo "Usage: $0 language [video URL or ID] output_folder"
exit 1
fi
fi
if [[ "$video_provided" == "" ]]; then
echo "Usage: $0 output_folder [video URL or ID]"
exit 1
fi
video_provided="${video_provided##*v=}"
video_provided="${video_provided##*/}"
video_id="${video_provided%&*}"
echo "video_id = $video_id"
##############################################################################
# Check for file was passed. Show open file dialog if no argument and on Mac
###############################################################################
if ! [ -d "$output_folder" ]; then
if [[ "$machine" == "Mac" ]]; then
output_folder=$(osascript -e 'tell application (path to frontmost application as text)
set output_folder to choose folder with prompt "Please choose an _output folder"
POSIX path of output_folder
end')
elif [[ "$machine" == "Linux" ]]; then
output_folder=$(dialog --title "Choose a folder" --stdout --title "Please select output folder:" --fselect ~/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
output_folder=$(dialog --title "Choose a folder" --stdout --title "Please select output folder:" --fselect ~/ 14 48)
else
echo "Usage: $0 language [video URL or ID] output_folder"
exit 1
fi
fi
echo "$output_folder"
if ! [ -d "$output_folder" ]; then
echo "Usage: $0 output_folder [video URL or ID] "
exit 1
fi
new_output=$output_folder/$lang_code
if ! [ -d "$new_output" ]; then
mkdir $new_output
fi
python TranslateYouTubeTranscript.py --video_id="$video_id" --output_folder "$new_output" --language "$lang_code"