-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpvloop
497 lines (458 loc) · 14.3 KB
/
mpvloop
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
#!/bin/bash
# MPVLOOP - run MPV with loop options set according to a pattern.
# SPDX-License-Identifier: Unlicense
#
# This small work is dedicated to all those helping to protect endangered species.
# 2024 Looper Guará
# Regular expression to match a loop 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]+"
# Scale to convert loop pattern numbers
# deciseconds: 0.1
# seconds: 1
sc=0.1
# 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")"
swreq() {
type awk >/dev/null 2>&1 || { echo >&2 "${scp}: WARNING: AWK not found - aborting."; exit 1; }
type file >/dev/null 2>&1 || { echo >&2 "${scp}: WARNING: FILE (command) not found - aborting."; exit 1; }
type grep >/dev/null 2>&1 || { echo >&2 "${scp}: WARNING: GREP not found - aborting."; exit 1; }
type mpv >/dev/null 2>&1 || { echo >&2 "${scp}: WARNING: MPV not found - aborting."; exit 1; }
}
lpat_tt() {
h=$(awk "BEGIN { print $(cut -d ':' -f 1 <<< "$1") * 3600 }")
m=$(awk "BEGIN { print $(cut -d ':' -f 2 <<< "$1") * 60 }")
s=$(cut -d ':' -f 3 <<< "$1")
awk 'BEGIN { printf ("%.0f", ( '"$h"' + '"$m"' + '"$s"' ) / '"$sc"' ) }'
}
lpat_aa() {
tr -d '{a,A}' <<< "$ab" | tr '{b,B}' '-' | cut -d '-' -f 1
}
lpat_bb() {
tr -d '{a,A}' <<< "$ab" | tr '{b,B}' '-' | cut -d '-' -f 2
}
lpat_a() {
awk "BEGIN { print $aa * $sc }"
}
lpat_b() {
awk "BEGIN { print $bb * $sc }"
}
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}: $((iter - 1)) iterations completed - exiting..."
exit;;
'SKIP file' )
return 1;;
'Cancel' )
break;;
esac
done 2>&1
fi
echo
}
core() {
if [ "$aa" -eq 0 ] && [ "$bb" -eq 0 ] ; then
echo "${scp}: Loop pattern: $(grep -oE "$lpat" <<< "$ab") (file)"
echo "${scp}: Options: --loop-file=inf" "$@"
echo "${scp}: Starting mpv..."
mpv --loop-file=inf "$@" "$f1"
return
elif [ "$aa" -eq 0 ] ; then
b=$(lpat_b)
echo "${scp}: Loop pattern and duration: $(grep -oE "$lpat" <<< "$ab") (${b}s)"
echo "${scp}: Options: --ab-loop-a=no --ab-loop-b=$b" "$@"
echo "${scp}: Starting mpv..."
mpv --ab-loop-a=no --ab-loop-b="$b" "$@" "$f1"
return
elif [ "$bb" -eq 0 ] ; then
a=$(lpat_a)
echo "${scp}: Loop pattern: $(grep -oE "$lpat" <<< "$ab")"
echo "${scp}: Options: --start=$a --ab-loop-a=$a --ab-loop-b=no" "$@"
echo "${scp}: Starting mpv..."
mpv --start="$a" --ab-loop-a="$a" --ab-loop-b=no "$@" "$f1"
return
fi
a=$(lpat_a)
b=$(lpat_b)
echo "${scp}: Loop pattern and duration: $(grep -oE "$lpat" <<< "$ab") ($(awk "BEGIN { print ( $bb * $sc ) - ( $aa * $sc ) }")s)"
echo "${scp}: Options: --start=$a --ab-loop-a=$a --ab-loop-b=$b" "$@"
echo "${scp}: Starting mpv..."
mpv --start="$a" --ab-loop-a="$a" --ab-loop-b="$b" "$@" "$f1"
}
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 ab in $(cut -s -d "$d" -f 1 --complement <<< "$line" | tr "$d" '\t') ; do
if grep -qE ^"${lpat}" <<< "$ab" ; then
aa=$(lpat_aa)
bb=$(lpat_bb | grep -oE "[0-9]+")
iter=$((iter + 1))
if ((iter > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iter=$((iter - 1))
break
fi
fi
core "$@"
else
echo >&2 "${scp}: WARNING: Invalid loop pattern - skipping: $ab"
continue
fi
done
done
}
if [ -e "$1" ] ; then
swreq
for f2 in "$@" ; do
if [ -d "$f2" ] ; then
shopt -qs globstar
f1a="$(realpath -q "$f2")"
for f1b in "${f1a}"/** ; do
if [ -f "$f1b" ] ; then
iall1+=("$f1b")
else
continue
fi
done
elif [ -f "$f2" ] ; then
iall1+=("$f2")
else
echo >&2 "${scp}: WARNING: No such file or directory - skipping: $f2"
fi
done
for f1 in "${iall1[@]}" ; do
if basename "$f1" | grep -oqE "${lpat}" ; then
for ab in $(basename "$f1" | grep -oE "${lpat}") ; do
aa=$(lpat_aa)
bb=$(lpat_bb)
iter=$((iter + 1))
if ((iter > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iter=$((iter - 1))
break
fi
fi
core
done
else
echo >&2 "${scp}: WARNING: No loop pattern - skipping: $(basename "$f1")"
fi
done
echo "${scp}: ${iter:-0} iterations completed - exiting..."
elif [ '--help' = "$1" ] ; then
cat << '_EOF_'
MPVLOOP - run MPV with loop options set according to a pattern.
SYNOPSIS
mpvloop {[<mpv profile,...>] [<loop pattern>... | -l] [-s] [path/]filename |
--lpat <timestamp range>... | --help | --version}
DESCRIPTION
MPVLOOP is a bash script that executes MPV with loop playback options set
according to a pattern in file names, a playlist or the command line.
MPV is run with any combination of the options: --start, --ab-loop-a,
--ab-loop-b, --loop-file.
MPV profile names are parsed and MPV is run according to: --profile.
MPV is only invoked to play a file with a loop pattern and is executed once for
each loop pattern; otherwise the files are skipped. Input directories are
expanded recursively. The line is read from left to right. If a key is pressed
within one second before MPV runs again, the script may exit or continue after
confirmation. Status messages report loop patterns and MPV options used, loop
duration, invalid inputs such as a file name without a pattern, and the number
of successful iterations.
LOOP PATTERN
Recognized patterns are formed with an initial letter A followed by numbers next
to an intermediate letter B followed by numbers. Letters are matched regardless
of the case. Numbers are positive integers or zero and represent time in
deciseconds, that is, tenths of a second. Zero disables each time point.
Patterns are converted to seconds with one decimal place and translated into
time settings for loop playback from points A to B.
Examples:
a1b2 (from 00:00:00.1 to 00:00:00.2) (from 0.1 seconds to 0.2 seconds)
a10b20 (from 00:00:01 to 00:00:02) (from 1 second to 2 seconds)
a0b123 (from 00:00:00 to 00:00:12.3) (from start of file to 12.3 seconds)
a123b0 (from 00:00:12.3) (from 12.3 seconds to end of file)
a0b0 (from 00:00:00) (from start to end of file)
For command line:
mpvloop foo-a1b2-a10b20.mp4
How MPV is run:
mpv --start=0.1 --ab-loop-a=0.1 --ab-loop-b=0.2 foo-a1b2-a10b20.mp4 ; \
mpv --start=1 --ab-loop-a=1 --ab-loop-b=2 foo-a1b2-a10b20.mp4
OPTIONS
MPV profile names and any number of loop patterns without flags can be entered
on the command line. Usage is printed if no arguments are given.
<mpv profile,...>
Name of the MPV profile to use.
For command line:
mpvloop gpu-hq foo-a1b2.mp4
How MPV is run:
mpv --start=0.1 --ab-loop-a=0.1 --ab-loop-b=0.2 --profile=gpu-hq foo-a1b2.mp4
<loop pattern>...
Play files according to this optional pattern. Other patterns in filenames, if
any, are ignored.
For command line:
mpvloop a10b20 foo-a1b2.mp4
How MPV is run:
mpv --start=1 --ab-loop-a=1 --ab-loop-b=2 foo-a1b2.mp4
-l file
Plaintext playlist with commas to separate a single file followed by any number
of associated patterns on each line. Patterns in filenames, if any, are ignored.
A line which does not satisfy all of the conditions below is also ignored:
- There is comma.
- There is loop pattern.
- The first field is a regular file.
For playlist file foo.txt which contains the following two lines:
path/to/foo1.mp4,a1b2,a123b0
foo2.mp4,a0b123
How MPV is run:
mpv --start=0.1 --ab-loop-a=0.1 --ab-loop-b=0.2 path/to/foo1.mp4 ; \
mpv --start=12.3 --ab-loop-a=12.3 --ab-loop-b=no path/to/foo1.mp4 ; \
mpv --ab-loop-a=no --ab-loop-b=12.3 foo2.mp4
-s
Shuffle the file list. With -l, options must be combined in any order, for
example: -ls.
--lpat <timestamp range>...
Print loop patterns equivalent to the timestamp ranges entered in any of the
following three formats. Decisecond place is optional. A single missing
timestamp translates to zero in the loop pattern:
1. HH:MM:SS[.n]-HH:MM:SS[.n]
2. HH:MM:SS[.n]-
3. -HH:MM:SS[.n]
Example:
mpvloop --lpat '00:02:03.4-00:09:27.8' '-00:00:12.3' '00:00:12.3-'
a1234b5678 (00:02:03.4-00:09:27.8)
a0b123 (-00:00:12.3)
a123b0 (00:00:12.3-)
--version
Display version information.
--help
Print this help summary.
DEPENDENCIES
Awk
file (command)
GNU Bourne-Again SHell (Bash)
GNU Coreutils
GNU Grep
mpv
COPYRIGHT
Unlicense <http://unlicense.org/>
This is free and unencumbered software released into the public domain and
provided without warranty of any kind.
SEE ALSO
input.conf (MPV)
The last line will configure MPV to print the current playing time in seconds to
the terminal screen (stdout) in addition to setting or clearing loop points as
in the default configuration when the L key is pressed. If "{time-pos}" instead
of "{=time-pos}" is used, the playing time will be printed as a timestamp
instead of seconds.
l ab-loop ; print-text "ab-loop=${=time-pos}"
_EOF_
elif [ '--version' = "$1" ] ; then
echo "mpvloop 1.1"
elif [ '--lpat' = "$1" ] ; then
swreq
if [ -n "$2" ] ; then
shift
for tt in "$@" ; do
if grep -qP '^[0-9][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9])?-[0-9][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9])?$' <<< "$tt" ; then
ta=$(cut -d '-' -f 1 <<< "$tt")
tb=$(cut -d '-' -f 2 <<< "$tt")
lpat_ab="a$(lpat_tt "$ta")b$(lpat_tt "$tb")"
if [ ${#lpat_ab} -lt 8 ] ; then
printf "%s\\t\\t%s\\n" "$lpat_ab" "(${tt})"
else
printf "%s\\t%s\\n" "$lpat_ab" "(${tt})"
fi
elif grep -qP '^[0-9][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9])?-$' <<< "$tt" ; then
ta=$(cut -d '-' -f 1 <<< "$tt")
lpat_ab="a$(lpat_tt "$ta")b0"
if [ ${#lpat_ab} -lt 8 ] ; then
printf "%s\\t\\t%s\\n" "$lpat_ab" "(${tt})"
else
printf "%s\\t%s\\n" "$lpat_ab" "(${tt})"
fi
elif grep -qP '^-[0-9][0-9]:[0-5][0-9]:[0-5][0-9](?:\.[0-9])?$' <<< "$tt" ; then
tb=$(cut -d '-' -f 2 <<< "$tt")
lpat_ab="a0b$(lpat_tt "$tb")"
if [ ${#lpat_ab} -lt 8 ] ; then
printf "%s\\t\\t%s\\n" "$lpat_ab" "(${tt})"
else
printf "%s\\t%s\\n" "$lpat_ab" "(${tt})"
fi
else
echo >&2 "${scp}: WARNING: Invalid timestamp format - skipping: $tt"
fi
done
else
echo "${scp}: Usage: ${scp} --lpat <HH:MM:SS[.n]-HH:MM:SS[.n] | HH:MM:SS[.n]- | -HH:MM:SS[.n]>..."
echo "${scp}: Help summary: ${scp} --help"
fi
elif [ '-l' = "$1" ] || [ '-ls' = "$1" ] || [ '-sl' = "$1" ] ; then
swreq
if [ "$#" -eq 2 ] && [ -r "$2" ] ; then
if file -b --mime-type "$2" | grep -q 'text/plain' ; then
if grep -q '[^[:space:]]' < "$2" ; then
if [ '-ls' = "$1" ] || [ '-sl' = "$1" ] ; then
shuffle=y
fi
loopfile="$2"
core_ll
else
echo >&2 "${scp}: WARNING: Empty file: $2"
echo "${scp}: Help summary: ${scp} --help"
fi
else
echo >&2 "${scp}: WARNING: A single plaintext file is expected for playlist: $2"
echo "${scp}: Help summary: ${scp} --help"
fi
else
shift
echo >&2 "${scp}: WARNING: A single plaintext file is expected for playlist:" "$@"
echo "${scp}: Help summary: ${scp} --help"
fi
echo "${scp}: ${iter:-0} iterations completed - exiting..."
elif [ '-l' = "$2" ] || [ '-ls' = "$2" ] || [ '-sl' = "$2" ] ; then
swreq
if [ "$#" -eq 3 ] && [ -r "$3" ] ; then
if file -b --mime-type "$3" | grep -q 'text/plain' ; then
if grep -q '[^[:space:]]' < "$3" ; then
if [ '-ls' = "$2" ] || [ '-sl' = "$2" ] ; then
shuffle=y
fi
loopfile="$3"
core_ll --profile="$1"
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
echo "${scp}: ${iter:-0} iterations completed - exiting..."
elif [ "$#" -eq 0 ] ; then
echo "${scp}: Usage: ${scp} {[<mpv profile,...>] [<loop pattern>... | -l] [-s] [path/]filename | --lpat <timestamp range>... | --help | --version}"
else
swreq
for arg in "$@" ; do
iterin=$((iterin + 1))
if grep -oqE ^"${lpat}"\$ <<< "$arg" ; then
aball+=("$arg")
elif [ -e "$arg" ] ; then
shift $((iterin - 1))
iall3+=("$@")
break
elif [ '-s' = "$arg" ] ; then
shuffle=y
else
if [ ! -v prf ] ; then
prf="$arg"
else
echo >&2 "${scp}: WARNING: MPV profile already set to ${prf@Q} - skipping: $arg"
fi
fi
done
unset iterin
if [ -v iall3 ] ; then
for f2 in "${iall3[@]}" ; do
if [ -d "$f2" ] ; then
shopt -qs globstar
f1a="$(realpath -q "$f2")"
for f1b in "${f1a}"/** ; do
if [ -f "$f1b" ] ; then
iall2+=("$f1b")
else
continue
fi
done
elif [ -f "$f2" ] ; then
iall2+=("$f2")
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
for f1 in "${iall1[@]}" ; do
if [ -v aball ] ; then
for ab in "${aball[@]}" ; do
aa=$(lpat_aa)
bb=$(lpat_bb)
iter=$((iter + 1))
if ((iter > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iter=$((iter - 1))
break
fi
fi
if [ -v prf ] ; then
core --profile="$prf"
else
core
fi
done
else
if basename "$f1" | grep -oqE "${lpat}" ; then
for ab in $(basename "$f1" | grep -oE "${lpat}") ; do
aa=$(lpat_aa)
bb=$(lpat_bb)
iter=$((iter + 1))
if ((iter > 1)) ; then
uquit
if [ "$?" -eq 1 ] ; then
iter=$((iter - 1))
break
fi
fi
if [ -v prf ] ; then
core --profile="$prf"
else
core
fi
done
else
echo >&2 "${scp}: WARNING: No loop pattern - skipping: $(basename "$f1")"
fi
fi
done
else
echo >&2 "${scp}: WARNING: Invalid options - aborting."
echo "${scp}: Help summary: ${scp} --help"
fi
echo "${scp}: ${iter:-0} iterations completed - exiting..."
fi