-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpvclipf
552 lines (523 loc) · 15 KB
/
mpvclipf
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/bin/bash
# MPVCLIPF - run MPVCLIP to play tagged clips.
# SPDX-License-Identifier: Unlicense
#
# This small work is dedicated to all those helping to protect endangered species.
# 2024 Looper Guará
# How the shell should call MPVCLIP
_mpvclip=mpvclip
# Regular expression to match a clip pattern
# lowercase letters only: a[0-9]+b[0-9]+
# uppercase letters only: A[0-9]+B[0-9]+
# case insensitive: [aA][0-9]+[bB][0-9]+
lpat="[aA][0-9]+[bB][0-9]+"
# Time in seconds to press any key and exit the script
# half a second: 0.5
q=1
# Field delimiter in a playlist
d=,
scp="$(basename "$0")"
if [ "$#" -eq 0 ] ; then
echo "${scp}: Usage: ${scp} {[<mpv profile,...>] <tag> [-p <pattern> | -l] [-s] [path/]filename | --help | --version}"
exit
elif [ '--help' = "$1" ] ; then
cat << '_EOF_'
MPVCLIPF - run MPVCLIP to play tagged clips
SYNOPSIS
mpvclipf {[<mpv profile,...>] <tag> [-p <pattern> | -l] [-s] [path/]filename |
--help | --version}
DESCRIPTION
Once for each tagged clip pattern associated with a file and optionally a path
pattern and MPV profile:
MPVCLIPF executes MPVCLIP; the file plays according to the tagged pattern.
Entered directories are searched recursively. The line is read from left to
right. If a key is pressed within one second before MPVCLIP is executed again,
the script may exit or continue after confirmation.
Status messages report the specified tag, invalid inputs, and the number of
successful iterations.
TAGGED CLIP PATTERN
A tag is defined if a match to a regular expression specified on the command
line is found at the end of a clip pattern in file names or a playlist.
OPTIONS
MPV profile names and a single tag without flags can be entered on the command
line. Usage is printed if no arguments are given.
<tag>
Search input directories, file names, or a playlist for a match to tag at the
end of a clip pattern. Tag, as specified on the command line, is a regular
expression. Found clips are played sequentially.
For command line:
mpvclipf Earth path/to
How MPVCLIP is run:
mpvclip a12b34 path/to/foo-a12b34Earth-a23b45Mars.mp4
-p <pattern>
Compares a shell pattern with the concatenation of a directory name and the base
name of the file. The test applies to the full path.
For command line:
mpvclipf Earth -p Saturn path/to
How MPVCLIP is run:
mpvclip a12b34 path/to/Saturn/foo-a12b34Earth-a23b45Mars.mp4 ; \
mpvclip a12b34 path/to/foo-a12b34Earth-a23b45Mars-a34b56Saturn.mp4
<mpv profile,...>
Name of the MPV profile to use.
For command line:
mpvclipf gpu-hq Pluto path/to
How MPVCLIP is run:
mpvclip a1b2 --profile=gpu-hq path/to/foo-a1b2Pluto.mp4
-l file
Plaintext playlist with commas to separate a single file followed by any number
of associated patterns on each line. A tagged clip plays if it matches the
specified tag. Patterns in file names, if any, are ignored.
A line which does not satisfy all of the conditions below is also ignored:
- There is comma.
- There is clip pattern.
- The first field is a regular file.
For playlist file foo.txt which contains the following two lines:
path/to/foo1.mp4,a1b2Neptune,a123b0
foo2.mp4,a0b123Neptune
How MPVCLIP is run:
mpvclip a1b2 path/to/foo1.mp4 ; mpvclip a0b123 foo2.mp4
-s file
Shuffle the file list. With -l, options must be combined in any order, for
example: -ls.
--version
Display version information.
--help
Print help summary.
DEPENDENCIES
file (command)
GNU Bourne-Again SHell (Bash)
GNU Coreutils
GNU Findutils (find)
GNU Grep
mpv
mpvclip (in a $PATH location)
COPYRIGHT
Unlicense <http://unlicense.org/>
This is free and unencumbered software released into the public domain and
provided without warranty of any kind.
_EOF_
exit
elif [ '--version' = "$1" ] ; then
echo "mpvclipf 1.1"
exit
fi
swreq() {
type "$_mpvclip" >/dev/null 2>&1 || { echo >&2 "${0}: WARNING: MPVCLIP not found - aborting."; exit 1; }
type find >/dev/null 2>&1 || { echo >&2 "${0}: WARNING: FIND not found - aborting."; exit 1; }
type grep >/dev/null 2>&1 || { echo >&2 "${0}: WARNING: GREP not found - aborting."; exit 1; }
type file >/dev/null 2>&1 || { echo >&2 "${0}: WARNING: FILE (command) not found - aborting."; exit 1; }
type mpv >/dev/null 2>&1 || { echo >&2 "${0}: WARNING: MPV not found - aborting."; exit 1; }
}
uquit() {
echo "${scp}: INFO: Press any key to exit the script or skip the file."
if read -rst "$q" -N 1 ; then
echo "${scp}: File: $f1"
echo "${scp}: Please confirm:"
select ans in "EXIT the script" "SKIP file" "Cancel" ; do
case "$ans" in
'EXIT the script' )
echo "${scp}: $((iterout - 1)) iterations completed - exiting..."
exit;;
'SKIP file' )
return 1;;
'Cancel' )
break;;
esac
done 2>&1
fi
echo
}
core_ll() {
if [ -v shuffle ] ; then
readarray -t iall1 < <(shuf "$loopfile")
else
readarray -t iall1 < <(cat "$loopfile")
fi
for line in "${iall1[@]}" ; do
f1="$(cut -d "$d" -f 1 <<< "$line")"
if [ ! -f "$f1" ] || grep -qv "$d" <<< "$line" ; then
if [ ${#line} -gt 90 ] ; then
echo >&2 "${scp}: WARNING: No field separator or file - skipping: ${line:0:45}...${line: -45}"
continue
else
echo >&2 "${scp}: WARNING: No field separator or file - skipping: $line"
continue
fi
fi
for ab2 in $(cut -s -d "$d" -f 1 --complement <<< "$line" | tr "$d" '\t') ; do
if grep -qoE ^"${lpat}${ltag}"\$ <<< "$ab2" ; then
ab=$(grep -oE "$lpat" <<< "$ab2")
iterout=$((iterout + 1))
if ((iterout > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iterout=$((iterout - 1))
continue 2
fi
fi
echo "${scp}: Clip tag: $ltag"
echo "${scp}: Starting ${_mpvclip}..."
"$_mpvclip" "$@" "$ab" "$f1"
else
continue
fi
done
done
}
if [ ! -e "$1" ] ; then
swreq
if [ '-l' = "$2" ] || [ '-ls' = "$2" ] || [ '-sl' = "$2" ] ; then
if [ "$#" -eq 3 ] && [ -r "$3" ] ; then
if file -b --mime-type "$3" | grep -q 'text/plain' ; then
if grep -q '[^[:space:]]' < "$3" ; then
ltag="$1"
loopfile="$3"
if [ '-ls' = "$2" ] || [ '-sl' = "$2" ] ; then
shuffle=y
fi
core_ll
else
echo >&2 "${scp}: WARNING: Empty file: $3"
echo "${scp}: Help summary: ${scp} --help"
fi
else
echo >&2 "${scp}: WARNING: A single plaintext file is expected for playlist: $3"
echo "${scp}: Help summary: ${scp} --help"
fi
else
shift 2
echo >&2 "${scp}: WARNING: A single plaintext file is expected for playlist:" "$@"
echo "${scp}: Help summary: ${scp} --help"
fi
if [ -v iterout ] ; then
echo "${scp}: $iterout iterations completed - exiting..."
else
echo >&2 "${scp}: WARNING: Tag or pattern not found - exiting..."
fi
elif [ '-p' = "$2" ] && [ -e "$4" ] || [ '-s' = "$4" ] ; then
ltag="$1"
ppat="$3"
if [ '-s' = "$4" ] ; then
shuffle=y
shift 4
else
shift 3
fi
iall3+=("$@")
if [ -v iall3 ] ; then
for f2 in "${iall3[@]}" ; do
if [ -d "$f2" ] ; then
readarray -t f3 < <(find "$f2" -path "*${ppat}*" -regex '.*'"${lpat}${ltag}"'.*' -type f)
if [ -n "${f3[0]}" ] ; then
iall2+=("${f3[@]}")
else
continue
fi
elif [ -f "$f2" ] ; then
if basename "$f2" | grep -qE "${lpat}${ltag}" && realpath -q "$f2" | grep -qF "$ppat" ; then
iall2+=("$f2")
else
continue
fi
else
echo >&2 "${scp}: WARNING: No such file or directory - skipping: $f2"
fi
done
if [ -v shuffle ] ; then
readarray -t iall1 < <(shuf -e "${iall2[@]}")
else
iall1+=("${iall2[@]}")
fi
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
for f1 in "${iall1[@]}" ; do
for ab in $(basename "$f1" | grep -oE "${lpat}${ltag}" | grep -oE "$lpat") ; do
iterout=$((iterout + 1))
if ((iterout > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iterout=$((iterout - 1))
continue 2
fi
fi
echo "${scp}: Clip tag: $ltag"
echo "${scp}: Starting ${_mpvclip}..."
"$_mpvclip" "$ab" "$f1"
done
done
if [ -v iterout ] ; then
echo "${scp}: $iterout iterations completed - exiting..."
else
echo >&2 "${scp}: WARNING: Tag or pattern not found - exiting..."
fi
elif [ '-l' = "$3" ] || [ '-ls' = "$3" ] || [ '-sl' = "$3" ] ; then
if [ "$#" -eq 4 ] && [ -r "$4" ] ; then
if file -b --mime-type "$4" | grep -q 'text/plain' ; then
if grep -q '[^[:space:]]' < "$4" ; then
for arg in "$1" "$2" ; do
iterin=$((iterin + 1))
if [ -n "$prf" ] ; then
ltag="$arg"
elif mpv --show-profile="$(cut -d ',' -f 1 <<< "$arg")" | grep -qi 'unknown' ; then
if [ "$iterin" -eq 1 ] ; then
ltag="$arg"
else
if [ -n "$ltag" ] ; then
echo >&2 "${scp}: WARNING: Cannot define MPV profile - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
fi
else
if [ -z "$prf" ] ; then
prf="$arg"
fi
fi
done
loopfile="$4"
if [ '-ls' = "$3" ] || [ '-sl' = "$3" ] ; then
shuffle=y
fi
core_ll "$prf"
else
echo >&2 "${scp}: WARNING: Empty file: $4"
echo "${scp}: Help summary: ${scp} --help"
fi
else
echo >&2 "${scp}: WARNING: A single plaintext file is expected for playlist: $4"
echo "${scp}: Help summary: ${scp} --help"
fi
else
shift 3
echo >&2 "${scp}: WARNING: A single plaintext file is expected for playlist:" "$@"
echo "${scp}: Help summary: ${scp} --help"
fi
if [ -v iterout ] ; then
echo "${scp}: $iterout iterations completed - exiting..."
else
echo >&2 "${scp}: WARNING: Tag or pattern not found - exiting..."
fi
elif [ '-p' = "$3" ] && [ -e "$5" ] || [ '-s' = "$5" ] ; then
ppat="$4"
for arg in "$1" "$2" ; do
iterin=$((iterin + 1))
if [ -n "$prf" ] ; then
ltag="$arg"
elif mpv --show-profile="$(cut -d ',' -f 1 <<< "$arg")" | grep -qi 'unknown' ; then
if [ "$iterin" -eq 1 ] ; then
ltag="$arg"
else
if [ -n "$ltag" ] ; then
echo >&2 "${scp}: WARNING: Cannot define MPV profile - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
fi
else
if [ -z "$prf" ] ; then
prf="$arg"
fi
fi
done
if [ '-s' = "$5" ] ; then
shuffle=y
shift 5
else
shift 4
fi
iall3+=("$@")
if [ -v iall3 ] ; then
for f2 in "${iall3[@]}" ; do
if [ -d "$f2" ] ; then
readarray -t f3 < <(find "$f2" -path "*${ppat}*" -regex '.*'"${lpat}${ltag}"'.*' -type f)
if [ -n "${f3[0]}" ] ; then
iall2+=("${f3[@]}")
else
continue
fi
elif [ -f "$f2" ] ; then
if basename "$f2" | grep -qE "${lpat}${ltag}" && realpath -q "$f2" | grep -qF "$ppat" ; then
iall2+=("$f2")
else
continue
fi
else
echo >&2 "${scp}: WARNING: No such file or directory - skipping: $f2"
fi
done
if [ -v shuffle ] ; then
readarray -t iall1 < <(shuf -e "${iall2[@]}")
else
iall1+=("${iall2[@]}")
fi
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
for f1 in "${iall1[@]}" ; do
for ab in $(basename "$f1" | grep -oE "${lpat}${ltag}" | grep -oE "$lpat") ; do
iterout=$((iterout + 1))
if ((iterout > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iterout=$((iterout - 1))
continue 2
fi
fi
echo "${scp}: Clip tag: $ltag"
echo "${scp}: Starting ${_mpvclip}..."
"$_mpvclip" "$prf" "$ab" "$f1"
done
done
if [ -v iterout ] ; then
echo "${scp}: $iterout iterations completed - exiting..."
else
echo >&2 "${scp}: WARNING: Tag or pattern not found - exiting..."
fi
elif [ -e "$2" ] || [ '-s' = "$2" ] ; then
ltag="$1"
if [ '-s' = "$2" ] ; then
shuffle=y
shift 2
else
shift
fi
iall3+=("$@")
if [ -v iall3 ] ; then
for f2 in "${iall3[@]}" ; do
if [ -d "$f2" ] ; then
readarray -t f3 < <(find "$f2" -regex '.*'"${lpat}${ltag}"'.*' -type f)
if [ -n "${f3[0]}" ] ; then
iall2+=("${f3[@]}")
else
continue
fi
elif [ -f "$f2" ] ; then
if basename "$f2" | grep -qE "${lpat}${ltag}" ; then
iall2+=("$f2")
else
continue
fi
else
echo >&2 "${scp}: WARNING: No such file or directory - skipping: $f2"
fi
done
if [ -v shuffle ] ; then
readarray -t iall1 < <(shuf -e "${iall2[@]}")
else
iall1+=("${iall2[@]}")
fi
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
for f1 in "${iall1[@]}" ; do
for ab in $(basename "$f1" | grep -oE "${lpat}${ltag}" | grep -oE "$lpat") ; do
iterout=$((iterout + 1))
if ((iterout > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iterout=$((iterout - 1))
continue 2
fi
fi
echo "${scp}: Clip tag: $ltag"
echo "${scp}: Starting ${_mpvclip}..."
"$_mpvclip" "$ab" "$f1"
done
done
if [ -v iterout ] ; then
echo "${scp}: $iterout iterations completed - exiting..."
else
echo >&2 "${scp}: WARNING: Tag or pattern not found - exiting..."
fi
elif [ ! -e "$2" ] && [ -e "$3" ] || [ '-s' = "$3" ] ; then
for arg in "$1" "$2" ; do
iterin=$((iterin + 1))
if [ -n "$prf" ] ; then
ltag="$arg"
elif mpv --show-profile="$(cut -d ',' -f 1 <<< "$arg")" | grep -qi 'unknown' ; then
if [ "$iterin" -eq 1 ] ; then
ltag="$arg"
else
if [ -n "$ltag" ] ; then
echo >&2 "${scp}: WARNING: Cannot define MPV profile - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
fi
else
if [ -z "$prf" ] ; then
prf="$arg"
fi
fi
done
if [ '-s' = "$3" ] ; then
shuffle=y
shift 3
else
shift 2
fi
iall3+=("$@")
if [ -v iall3 ] ; then
for f2 in "${iall3[@]}" ; do
if [ -d "$f2" ] ; then
readarray -t f3 < <(find "$f2" -regex '.*'"${lpat}${ltag}"'.*' -type f)
if [ -n "${f3[0]}" ] ; then
iall2+=("${f3[@]}")
else
continue
fi
elif [ -f "$f2" ] ; then
if basename "$f2" | grep -qE "${lpat}${ltag}" ; then
iall2+=("$f2")
else
continue
fi
else
echo >&2 "${scp}: WARNING: No such file or directory - skipping: $f2"
fi
done
if [ -v shuffle ] ; then
readarray -t iall1 < <(shuf -e "${iall2[@]}")
else
iall1+=("${iall2[@]}")
fi
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
exit 1
fi
for f1 in "${iall1[@]}" ; do
for ab in $(basename "$f1" | grep -oE "${lpat}${ltag}" | grep -oE "$lpat") ; do
iterout=$((iterout + 1))
if ((iterout > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iterout=$((iterout - 1))
continue 2
fi
fi
echo "${scp}: Clip tag: $ltag"
echo "${scp}: Starting ${_mpvclip}..."
"$_mpvclip" "$prf" "$ab" "$f1"
done
done
if [ -v iterout ] ; then
echo "${scp}: $iterout iterations completed - exiting..."
else
echo >&2 "${scp}: WARNING: Tag or pattern not found - exiting..."
fi
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
fi
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
fi