-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.sh
More file actions
executable file
·257 lines (206 loc) · 5.3 KB
/
audio.sh
File metadata and controls
executable file
·257 lines (206 loc) · 5.3 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/zsh
. ./messages.sh
. ./logging.sh
. ./vars.sh
. ./utils.sh
prepare_raw() {
if [ "$REWRITE" = true ]; then
rm "$RAW"
fi
if [ -f "$RAW" ]; then
print_message "RAW file already exists"
return 0
fi
if [ -n "$FILE_SOURCE" ]; then
cp "$FILE_SOURCE" "$RAW"
elif [ -n "$YOUTUBE_URL" ]; then
youtube_download
else
print_error "Script requires either youtube source URL or local file path (-y/-l)"
exit 1
fi
}
check_ffmpeg() {
if ! command -v ffmpeg > "$LOG" 2>&1
then
check-brew
print_message "You miss FFMPEG. Installing it..."
brew install ffmpeg
fi
}
check-brew() {
if ! command -v brew > "$LOG" 2>&1; then
print_error "You miss Homebrew. Please install it before contining"
exit 1
fi
}
youtube_download() {
if ! command -v yt-dlp > "$LOG" 2>&1
then
check-brew
print_error "You miss Youtube-dl. Please install all the dependencies from requirements.txt"
exit 1
fi
print_message "Downloading audio from the video: "$YOUTUBE_URL""
yt-dlp -x --audio-format mp3 $YOUTUBE_URL -o "$RAW" > "$LOG" 2>&1
}
crop() {
if [ "$TESTRUN" = true ]; then
print_message "No cropping on test run"
CROPPED="$RAW"
return 0
fi
if [ "$REWRITE" = true ]; then
rm "$CROPPED"
fi
if [ -f "$CROPPED" ]; then
print_message "CROPPED file already exists"
return 0
fi
check_ffmpeg
print_message "Cutting down intro from the file"
ffmpeg -i "$RAW" -ss "$CUT" "$CROPPED" > "$LOG" 2>&1
}
add_compression() {
if [ "$REWRITE" = true ]; then
rm "$COMPRESSED"
fi
if [ -f "$COMPRESSED" ]; then
print_message "COMPRESSED file already exists"
return 0
fi
check_ffmpeg
print_message "Adding some compression effect."
ffmpeg -i "$CROPPED" -af "acompressor" "$COMPRESSED" > "$LOG" 2>&1
}
add_metadata() {
if [ "$REWRITE" = true ]; then
rm "$FINAL"
fi
if [ -f "$FINAL" ]; then
print_message "FINAL file already exists"
return 0
fi
check_ffmpeg
print_message "Adding metadata to the mp3 file"
TITLE="Live $EPISODE_NUMBER"
ARTIST="Cowsay Show"
YEAR=$(date +\"%Y\")
GENRE="Podcast"
ffmpeg -i "$COMPRESSED" -i "$COVER" -map 0 -map 1 -metadata title="$TITLE" -metadata artist="$ARTIST" -metadata genre="$GENRE" -metadata year="$YEAR" -c copy $FINAL > "$LOG" 2>&1
}
clean() {
if [ "$TESTRUN" = true ]; then
print_message "No cleaning on test run"
return 0
fi
print_message "Cleaning up the environment"
rm "$CROPPED" > "$LOG" 2>&1
rm "$COMPRESSED" > "$LOG" 2>&1
}
azure_upload() {
print_message "Uploading the file to the Azure"
if ! command -v azcopy > "$LOG" 2>&1
then
check-brew
print_message "You miss Azcopy. Installing it..."
brew install azcopy
fi
LOGIN="$(azcopy login status)"
if [[ "$LOGIN" == *"not logged in"* ]]; then
print_error "You are not logged in to azcopy..."
exit 1
fi
URL=""$AZURE_DIR""$FINAL""
azcopy copy "$FINAL" "$URL" > "$LOG" 2>&1
}
##################################################################################
print_message "Preparing to execute the script"
set -euo pipefail
# set -x
# Prepare the logs
prepare_logging "audio"
# Check on OS
OS="$(uname -s)"
if [ $OS != "Darwin" ]; then
print_error "This script is meant to be running on macOS"
exit 1
fi
# Init and check arguments
# Default values
EPISODE_NUMBER=""
FILE_SOURCE=""
YOUTUBE_URL=""
UPLOAD=false
TESTRUN=false
REWRITE=false
# Read Arguments
while getopts ":e:y:l:utr" opt; do
case ${opt} in
e )
EPISODE_NUMBER=$OPTARG
;;
y )
YOUTUBE_URL=$OPTARG
;;
l )
FILE_SOURCE=$OPTARG
;;
u )
UPLOAD=true
;;
t )
TESTRUN=true
;;
r )
REWRITE=true
;;
\? )
echo "Invalid option: -$OPTARG (-e -y/-l [-u])" >&2
exit 1
;;
: )
echo "Option -$OPTARG requires an argument (-e -y/-l [-u])" >&2
exit 1
;;
esac
done
if [ -z "$EPISODE_NUMBER" ]; then
print_error "This script needs episode number in order to work"
exit 1
fi
# Files
RAW="raw$EPISODE_NUMBER.mp3"
CROPPED="cropped$EPISODE_NUMBER.mp3"
COMPRESSED="compressed$EPISODE_NUMBER.mp3"
FINAL="cow$EPISODE_NUMBER.mp3"
prepare_dir "$EPISODE_NUMBER"
CUT=$(cat "cut-$EPISODE_NUMBER.txt")
prepare_raw
crop
# Check if the crop is correct
qlmanage -p "$CROPPED"
print_message "Do you want to proceed with this variant? y/[n]"
read ANSWER
if [ -n "$ANSWER" ] && [ $ANSWER = "y" ]; then
print_message "Continuing..."
else
print_error "Not using this variant. Please run the script again"
clean
exit 0
fi
if [ "$TESTRUN" = false ]; then
add_compression
else
print_message "No compression on test run"
COMPRESSED="$CROPPED"
fi
add_metadata
if [ "$UPLOAD" = "false" ]; then
print_success "The script has finished successfuly without file upload 🐮."
clean
exit 0
fi
azure_upload
print_success "Audio file is successfully prepared and uploaded 🐮."
clean