-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha2t
114 lines (88 loc) · 5.25 KB
/
a2t
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
# Small but handy functions. For Personal use only.
# Use it at your own risk!
# Author: Gabriele Risso License: Apache 2.0
#lang=${lang:-$(echo ${LANG:0:2})};
session=${session:-$(echo "$(wget -qO - http://frightanic.com/goodies_content/docker-names.php)-$(date '+%Y-%m-%d-%H')" )};
textw="/dev/shm/textw_$session.txt"
audiow="/dev/shm/audiow_$session.mp3"
if ! type a2t 2>/dev/null 1>&2;
then
a2t ()
{
if ! command -v whisper-ctranslate2 2>/dev/null 1>&2;
then
echo "whisper-ctranslate2 is not installed on your system. Install it: pip install -U whisper-ctranslate2" >&2;
echo "if it's your first time whisper is going to install a medium sized model (2.85 GB)"
echo "a2t is disabled" >&2;
elif ! command -v gum --version 2>/dev/null 1>&2;
then
echo " gum is not installed on your system. Install it: https://github.com/charmbracelet/gum#installation and re-try" >&2;
else
if ! type a2t-whisp 2>/dev/null 1>&2;
then
a2t-whisp ()
{
if [ -z ${OPENAI_API_KEY+x} ]; then echo '{{ Bold "[!] OPENAI_API_KEY not set! type: export OPENAI_API_KEY=\"your-key-here\" " }}' | gum format -t template >&2; return 1; fi
echo '{{ Bold "[*] session: '$session' " }}' | gum format -t template >&2;
echo '{{ Bold "[!] whisper subtitles started" }}' | gum format -t template >&2;
local start=$SECONDS;
local formatsub=${formatsub:-$(echo "txt")};
local model=${model:-$(echo "medium")};
#if input text exis then set it as input, if not select it
local input="$1";
#select the lastly created file.mp3 (1 minute old) located in /dev/shm/ OR select a local .mp3 file
local newest_file="";
newest_file=$(find /dev/shm/ -type f -cmin -1 -size +0 -regextype sed -regex ".*\(a\|v\)*.mp3" -printf "%T@ %p\n" 2>/dev/null | sort -n | tail -1 | awk '{print $2}');
if [ -n "$newest_file" ]; then
echo '{{ Bold "[!] most recent file --> '$newest_file'" }}' | gum format -t template >&2;
input=${input:-$(echo $newest_file)};
else
echo '{{ Bold "[+] Select a mp3 file. Use keyboard arrows < > to traverse directories, up or down arrows to move in the directory." }}' | gum format -t template >&2;
input=$(gum file $PWD);
fi
cp $input $audiow;
#output definition
local output="$2";
local output=${output:-$(echo $textw)};
#local tmpsrt=$(basename $subw);
#local tmptxt=$(basename $output);
gum style "INPUT audio located at: $input" "INput audio language: auto" "OUTPUT text saved at $output" "OUTput language: auto" "SESSION: $session" --foreground 15 --border-foreground 50 --border rounded --width="$(($COLUMNS -4))" --italic --margin "1 1" --padding "0 1";
gum spin -s points --show-output --spinner.foreground="50" --title.bold --spinner.margin="0 1" --title "using Whisper for subtitles creation... this can take a short while" --\
whisper-ctranslate2 $input --output_format $formatsub --task transcribe --compute_type int8 --output_dir /dev/shm/ ;
#choose best model
echo '{{ Bold "[t] completed in '$((SECONDS-start))' seconds" }}' | gum format -t template >&2;
#rename subtitle file to the output name
local namesrt="";
namesrt=$(basename $input);
mv "/dev/shm/${namesrt%.*}.$formatsub" "$textw";
# subtitle to text: https://github.com/yumstar/srt2text/blob/main/srt2text.py
#context
gum confirm --negative="no" --affirmative="yes, let's read!" "open pager to read text?" --timeout=15s --selected.foreground="0" --unselected.foreground="44" --selected.italic --unselected.italic --selected.background="50" --prompt.italic --prompt.margin="0 0 0 $(($COLUMNS -29))" \
&& gum pager --soft-wrap --border-foreground="86" --help.foreground="200" --show-line-numbers < $output;
echo '{{ Bold "[+] subtitles OK" }}' | gum format -t template >&2;
echo '{{ Bold "[?] What now? press ESC to exit." }}' | gum format -t template >&2;
echo ' ' >&2;
while read -r -n1 key
do
# if input == ESC key
if [[ $key == $'\e' ]];
then
break;
fi
CHOICE=$(gum choose --item.foreground 250 "edit function sourcecode" "edit output text in glow" "play input audio" "help" "contribute to aiyu");
[[ "$CHOICE" == "edit function sourcecode" ]] && $EDITOR $(gum filter --value="a2t" --height=9);
[[ "$CHOICE" == "edit output text in glow" ]] && cp $textw "${textw::-4}.md" && glow "/dev/shm/";
[[ "$CHOICE" == "play input audio" ]] && ffplay -nodisp -autoexit -loglevel fatal $input;
[[ "$CHOICE" == "help" ]] && glow -p README.md;
[[ "$CHOICE" == "contribute to aiyu" ]] && echo "Great! I thought so, thank you for making $(gum style --bold "aiyu") is the best! leave a $(gum style --bold "star") if you like this. Go to: https://github.com/GabrieleRisso/aiyu";
done;
}
else
echo "a2t is set on the system. To use it: a2t \"input.mp3\" \"output.mp3\" " >&2;
fi
fi
a2t-whisp "$1" "$2";
}
else
echo '{{ Bold "a2t is set on the system" }}' | gum format -t template >&2;
fi