Skip to content

Commit 733a4d8

Browse files
committed
improving robustness (removing echo, adding --)
1 parent 346a12f commit 733a4d8

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

ymph

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,42 @@ function show_help() {
6868
}
6969

7070
function download_sound() {
71-
printf "$DEFAULT_COLOR"
71+
printf -- "$DEFAULT_COLOR"
7272
printf "Preparing music for video $ID_COLOR$1\n"
7373

7474
if [[ -f "$DOWNLOAD_DIR/$1" ]]; then
75-
printf "$DEFAULT_COLOR\tUsing cached $ID_COLOR$1$DEFAULT_COLOR\n"
75+
printf -- "$DEFAULT_COLOR\tUsing cached $ID_COLOR$1$DEFAULT_COLOR\n"
76+
if [[ -f "$DOWNLOAD_DIR/$1.invalid" ]]; then
77+
rm -- "$DOWNLOAD_DIR/$1.invalid"
78+
fi
7679
return 0
7780
elif [[ -f "$DOWNLOAD_DIR/$1.invalid" ]]; then
78-
printf "$ERROR_COLOR\tError: $ID_COLOR$1$DEFAULT_COLOR has been marked as invalid.\n" >&2
81+
printf -- "$ERROR_COLOR\tError: $ID_COLOR$1$DEFAULT_COLOR has been marked as invalid.\n" >&2
7982
printf "\tTo try to redownload, delete the file $ID_COLOR$DOWNLOAD_DIR/$1.invalid$DEFAULT_COLOR\n" >&2
8083
return 1
8184
fi
8285
printf "\t$ID_COLOR$1$DEFAULT_COLOR is not present in cache. Downloading it.\n"
8386

8487
local audio_formats=$(youtube-dl --no-warnings -F -- "$1" | grep "audio only" 2>/dev/null)
85-
local format_count=$(echo "$audio_formats" | wc -l)
88+
local format_count=$(printf -- "$audio_formats" | wc -l)
8689

8790
if [[ $format_count -gt 1 ]]; then
88-
local audio_formats_2=$(echo "$audio_formats" | grep -v "worst")
89-
local format_count=$(echo "$audio_formats" | wc -l)
91+
local audio_formats_2=$(printf -- "$audio_formats" | grep -v "worst")
92+
local format_count=$(printf -- "$audio_formats" | wc -l)
9093

9194
if [[ $format_count -ge 1 ]]; then
9295
local audio_formats="$audio_formats_2"
93-
local audio_formats_2=$(echo "$audio_formats" | grep "m4a")
94-
local format_count=$(echo "$audio_formats" | wc -l)
96+
local audio_formats_2=$(printf -- "$audio_formats" | grep "m4a")
97+
local format_count=$(printf -- "$audio_formats" | wc -l)
9598

9699
if [[ $format_count -ge 1 ]]; then
97100
local audio_formats="$audio_formats_2"
98101
fi
99102
fi
100103
fi
101104

102-
local format=$(echo "$audio_formats" | head -n1 | cut -d ' ' -f 1)
103-
local extension=$(echo "$audio_formats" | head -n1 | sed 's/[0-9]\+ \+//' | cut -d ' ' -f 1)
105+
local format=$(printf -- "$audio_formats" | head -n1 | cut -d ' ' -f 1)
106+
local extension=$(printf -- "$audio_formats" | head -n1 | sed 's/[0-9]\+ \+//' | cut -d ' ' -f 1)
104107

105108
youtube-dl --no-warnings -q -f $format --add-metadata -o "$DOWNLOAD_DIR/$1.$extension" -- "$1" >/dev/null 2>/dev/null
106109

@@ -110,24 +113,24 @@ function download_sound() {
110113
if [[ $NORMALIZE == "true" ]]; then
111114
printf "\t$ID_COLOR$1$DEFAULT_COLOR: normalizing.\n"
112115
local file_info=$(ffmpeg -y -i "$DOWNLOAD_DIR/$1.$extension" -pass 1 -filter:a loudnorm=print_format=json -f null - 2>&1)
113-
local lra=$(echo $file_info | grep "input_lra" | sed 's/.*"input_lra" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
114-
local tp=$(echo $file_info | grep "input_tp" | sed 's/.*"input_tp" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
115-
local thresh=$(echo $file_info | grep "input_thresh" | sed 's/.*"input_thresh" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
116-
local i=$(echo $file_info | grep "input_i" | sed 's/.*"input_i" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
116+
local lra=$(printf -- "$file_info" | grep "input_lra" | sed 's/.*"input_lra" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
117+
local tp=$(printf -- "$file_info" | grep "input_tp" | sed 's/.*"input_tp" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
118+
local thresh=$(printf -- "$file_info" | grep "input_thresh" | sed 's/.*"input_thresh" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
119+
local i=$(printf -- "$file_info" | grep "input_i" | sed 's/.*"input_i" : "\(-\?[0-9]*\.[0-9]*\)".*/\1/')
117120

118121
yes | ffmpeg -loglevel panic -i "$DOWNLOAD_DIR/$1.$extension" -pass 2 -filter:a dynaudnorm=f=1000,loudnorm=linear=true:measured_I=$i:measured_LRA=$lra:measured_tp=$tp:measured_thresh=$thresh "$DOWNLOAD_DIR/$1.norm.$extension"
119122

120-
rm "$DOWNLOAD_DIR/$1.$extension" 2>/dev/null
121-
mv "$DOWNLOAD_DIR/$1.norm.$extension" "$DOWNLOAD_DIR/$1" 2>/dev/null
123+
rm -- "$DOWNLOAD_DIR/$1.$extension" 2>/dev/null
124+
mv -- "$DOWNLOAD_DIR/$1.norm.$extension" "$DOWNLOAD_DIR/$1" 2>/dev/null
122125
else
123-
mv "$DOWNLOAD_DIR/$1.$extension" "$DOWNLOAD_DIR/$1"
126+
mv -- "$DOWNLOAD_DIR/$1.$extension" "$DOWNLOAD_DIR/$1"
124127
fi
125-
printf "$ID_COLOR$1$DEFAULT_COLOR: ready.\n"
128+
printf -- "$ID_COLOR$1$DEFAULT_COLOR: ready.\n"
126129

127130
return 0
128131
else
129132

130-
printf "$ERROR_COLOR\tError$DEFAULT_COLOR downloading $ID_COLOR$1$DEFAULT_COLOR.\n" >&2
133+
printf -- "$ERROR_COLOR\tError$DEFAULT_COLOR downloading $ID_COLOR$1$DEFAULT_COLOR.\n" >&2
131134
touch "$DOWNLOAD_DIR/$1.invalid"
132135
return 1
133136
fi
@@ -149,7 +152,7 @@ function readable_size() {
149152
let "size = size / 1024"
150153
suffix="Gio"
151154
fi
152-
printf "$size $suffix"
155+
printf -- "$size $suffix"
153156
}
154157

155158
function readable_time() {
@@ -164,71 +167,71 @@ function readable_time() {
164167
let "hours = hours - 24 * days"
165168

166169
if [[ $days -gt 0 ]]; then
167-
printf "$days"
170+
printf -- "$days"
168171
printf "d "
169172
fi
170173

171174
if [[ $hours -lt 10 ]]; then
172175
printf "0$hours:"
173176
else
174-
printf "$hours:"
177+
printf -- "$hours:"
175178
fi
176179

177180
if [[ $minutes -lt 10 ]]; then
178181
printf "0$minutes'"
179182
else
180-
printf "$minutes'"
183+
printf -- "$minutes'"
181184
fi
182185

183186
if [[ $seconds -eq 0 ]]; then
184187
printf "00"
185188
elif [[ $seconds -lt 10 ]]; then
186189
printf "0$seconds"
187190
else
188-
printf "$seconds"
191+
printf -- "$seconds"
189192
fi
190193
}
191194

192195
function skip_sig() {
193-
printf "$REQUEST_COLOR"
196+
printf -- "$REQUEST_COLOR"
194197
printf "Received: next. ASAP Master\n"
195-
printf "$DEFAULT_COLOR"
198+
printf -- "$DEFAULT_COLOR"
196199
SKIP="true"
197200
PREV="false"
198201
}
199202

200203
function prev_sig() {
201-
printf "$REQUEST_COLOR"
204+
printf -- "$REQUEST_COLOR"
202205
printf "Received: prev. ASAP Master\n"
203-
printf "$DEFAULT_COLOR"
206+
printf -- "$DEFAULT_COLOR"
204207
PREV="true"
205208
SKIP="false"
206209
}
207210

208211
function bye_sig() {
209-
printf "$REQUEST_COLOR"
212+
printf -- "$REQUEST_COLOR"
210213
printf "Received: quit. Good bye\n"
211-
printf "$DEFAULT_COLOR"
214+
printf -- "$DEFAULT_COLOR"
212215

213-
rm "$PID_FILE"
216+
rm -- "$PID_FILE"
214217
if [[ -f "$TEMPORARY_FILE" ]]; then
215-
rm "$TEMPORARY_FILE"
218+
rm -- "$TEMPORARY_FILE"
216219
fi
217220

218221
if [[ $CACHING == "false" ]]; then
219-
rm "$DOWNLOAD_DIR/$id" 2>/dev/null
220-
rm "$DOWNLOAD_DIR/$current_id" 2>/dev/null
222+
rm -- "$DOWNLOAD_DIR/$id" 2>/dev/null
223+
rm -- "$DOWNLOAD_DIR/$current_id" 2>/dev/null
221224
fi
222225

223-
rm "$DOWNLOAD_DIR/"*.* 2>/dev/null
226+
rm -- "$DOWNLOAD_DIR/"*.* 2>/dev/null
224227
stop_player
225228
exit
226229
}
227230

228231
function play_sig() {
229-
printf "$REQUEST_COLOR"
232+
printf -- "$REQUEST_COLOR"
230233
printf "Received: play\n"
231-
printf "$DEFAULT_COLOR"
234+
printf -- "$DEFAULT_COLOR"
232235

233236
if [[ $STATE == "pause" ]]; then
234237
STATE_CHANGED="true"
@@ -253,9 +256,9 @@ function toggle_sig() {
253256
}
254257

255258
function pause_sig() {
256-
printf "$REQUEST_COLOR"
259+
printf -- "$REQUEST_COLOR"
257260
printf "Received: pause\n"
258-
printf "$DEFAULT_COLOR"
261+
printf -- "$DEFAULT_COLOR"
259262

260263
if [[ $STATE == "play" ]]; then
261264
STATE_CHANGED="true"
@@ -283,16 +286,16 @@ function show_cache() {
283286
printf "Cache folder: $DOWNLOAD_DIR\n"
284287
printf "File list:\n"
285288
for i in $(ls "$DOWNLOAD_DIR"); do
286-
if [[ $(echo $i | cut -d '.' -f 2) == "invalid" ]]; then
287-
printf "$ID_COLOR$(echo $i | cut -d '.' -f 1)$ERROR_COLOR\tVideo is unavailable\n"
289+
if [[ $(printf -- "$i" | cut -d '.' -f 2) == "invalid" ]]; then
290+
printf -- "$ID_COLOR$(printf -- "$i" | cut -d '.' -f 1)$ERROR_COLOR\tVideo is unavailable\n"
288291
let "invalid_count++"
289292

290-
elif [[ $(echo $i | cut -d '.' -f 2) == "$i" ]]; then
293+
elif [[ $(printf -- "$i" | cut -d '.' -f 2) == "$i" ]]; then
291294
local dur_title=$(ffprobe -i $DOWNLOAD_DIR/$i -v quiet -print_format json -show_format | jq -r '.format.duration,.format.tags.title')
292-
local title=$(echo $dur_title | cut -d ' ' -f 2-)
293-
local this_duration=$(echo $dur_title | cut -d '.' -f 1)
295+
local title=$(printf -- "$dur_title" | tail -n 1)
296+
local this_duration=$(printf -- "$dur_title" | head -n 1 | cut -d '.' -f 1)
294297

295-
printf "$ID_COLOR$i\t$TITLE_COLOR$title\n"
298+
printf -- "$ID_COLOR$i\t$TITLE_COLOR$title\n"
296299

297300
duration=$(($duration + $this_duration))
298301
if [[ $this_duration -gt $longest ]]; then
@@ -310,19 +313,19 @@ function show_cache() {
310313
let "valid_count++"
311314
fi
312315
done
313-
printf "$DEFAULT_COLOR"
316+
printf -- "$DEFAULT_COLOR"
314317
printf "\nValid tracks: $valid_count"
315318
printf "\nBad tracks: $invalid_count"
316319
printf "\nTotal cache size: "
317320
readable_size $size
318321
printf "\nTotal duration: "
319322
readable_time $duration
320-
printf "\nHeaviest file: $ID_COLOR $heaviest_id$DEFAULT_COLOR - $TITLE_COLOR$(ffprobe -i $DOWNLOAD_DIR/$heaviest_id -show_format -v quiet | grep TAG:title | cut -d '=' -f 2)"
321-
printf "$DEFAULT_COLOR ["
323+
printf "\nHeaviest file: $ID_COLOR $heaviest_id$DEFAULT_COLOR - $TITLE_COLOR$(ffprobe -i $DOWNLOAD_DIR/$heaviest_id -show_format -v quiet -print_format json | jq -r '.format.tags.title')"
324+
printf -- "$DEFAULT_COLOR ["
322325
readable_size $heaviest
323326
printf "]"
324-
printf "\nLongest music: $ID_COLOR $longest_id$DEFAULT_COLOR - $TITLE_COLOR$(ffprobe -i $DOWNLOAD_DIR/$heaviest_id -show_format -v quiet | grep TAG:title | cut -d '=' -f 2)"
325-
printf "$DEFAULT_COLOR ["
327+
printf "\nLongest music: $ID_COLOR $longest_id$DEFAULT_COLOR - $TITLE_COLOR$(ffprobe -i $DOWNLOAD_DIR/$longest_id -show_format -v quiet -print_format json | jq -r '.format.tags.title')"
328+
printf -- "$DEFAULT_COLOR ["
326329
readable_time $longest
327330
printf "]\n"
328331
}
@@ -333,7 +336,7 @@ function search_cache() {
333336
local matching=0
334337
local total=0
335338
for i in $(ls "$DOWNLOAD_DIR"); do
336-
if [[ $(echo $i | cut -d '.' -f 2) == "$i" ]]; then
339+
if [[ $(printf -- "$i" | cut -d '.' -f 2) == "$i" ]]; then
337340
let "total++"
338341
local title=$(ffprobe -i $DOWNLOAD_DIR/$i -v quiet -print_format json -show_format | jq -r '.format.tags.title')
339342
if [[ $title =~ $1 ]]; then
@@ -350,7 +353,7 @@ function search_cache() {
350353
}
351354

352355
function init() {
353-
COMMAND=$(echo $0 | sed 's/.*\/\(.*\)/\1/')
356+
COMMAND=$(printf -- "$0" | sed 's/.*\/\(.*\)/\1/')
354357

355358
ID_COLOR="\e[38;5;14m"
356359
ERROR_COLOR="\e[38;5;9m"
@@ -553,7 +556,7 @@ function parse_args() {
553556
exit 0
554557
;;
555558
*)
556-
printf "$COMMAND: $1: Unknow field.\nTry $COMMAND --help.\n" >&2
559+
printf -- "$COMMAND: $1: Unknow field.\nTry $COMMAND --help.\n" >&2
557560
exit 1
558561
esac
559562

@@ -566,7 +569,7 @@ function start_up() {
566569

567570
if [[ -f "$PID_FILE" ]]; then
568571
if [[ $(ps -ef | grep $(cat $PID_FILE) | grep ymph | wc -l) -eq 0 && $(ps -ef | grep $(cat $PID_FILE) | grep $COMMAND | wc -l) -eq 0 ]]; then
569-
printf "$ERROR_COLOR" >&2
572+
printf -- "$ERROR_COLOR" >&2
570573
printf "Error:$DEFAULT_COLOR ymph seems to have died, but didn't clean up after him.\n" >&2
571574
printf "If that is the case, delete the file $PID_FILE\n"
572575
exit 3
@@ -601,12 +604,12 @@ function start_up() {
601604
fi
602605

603606
if [[ ! -f "$PLAYLIST_FILE" ]]; then
604-
printf "$ERROR_COLOR" >&2
607+
printf -- "$ERROR_COLOR" >&2
605608
printf "Error:$DEFAULT_COLOR Failed to open playlist file.\n" >&2
606609
exit 3
607610
fi
608611
if [[ $(wc -l "$PLAYLIST_FILE" | cut -d ' ' -f 1) -eq 0 ]]; then
609-
printf "$ERROR_COLOR" >&2
612+
printf -- "$ERROR_COLOR" >&2
610613
printf "Error:$DEFAULT_COLOR the playlist is empty!\n" >&2
611614
exit 3
612615
fi
@@ -659,7 +662,7 @@ function try_sleep() {
659662

660663
function main_loop() {
661664
start_player
662-
echo "$$" > "$PID_FILE"
665+
printf "$$" > "$PID_FILE"
663666

664667
COUNT=0
665668
AHEAD_COUNT=$(($AHEAD_LOADING - 1))
@@ -694,7 +697,7 @@ function main_loop() {
694697
fi
695698
current_id="$ID"
696699

697-
printf "$ID_COLOR"
700+
printf -- "$ID_COLOR"
698701
printf "Now playing $TITLE_COLOR$(ffprobe -i $DOWNLOAD_DIR/$current_id -show_format -v quiet | grep TAG:title | cut -d '=' -f 2)$DEFAULT_COLOR [$ID_COLOR$current_id$DEFAULT_COLOR]\n"
699702

700703
while [[ $STATE == "pause" && $SKIP == "false" ]]; do
@@ -801,7 +804,7 @@ function main_loop() {
801804

802805
function check_dependency() {
803806
printf "\t\t$1:"
804-
for i in $(seq $(echo $1 | wc -c) 10); do
807+
for i in $(seq $(printf -- "$1" | wc -c) 10); do
805808
printf " "
806809
done
807810

@@ -818,7 +821,6 @@ function check_dependencies() {
818821
check_dependency cat
819822
check_dependency cut
820823
check_dependency date
821-
check_dependency echo
822824
check_dependency head
823825
check_dependency mkdir
824826
check_dependency mv

0 commit comments

Comments
 (0)