-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.sh-functions-media
498 lines (442 loc) · 15.4 KB
/
.sh-functions-media
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
# -*- shell-script -*-
[[ ! -z $DEBUG ]] && echo "sourcing .sh-functions-media"
videodl () {
# REQUIREMENTS:
# rtmpdump: apt or git://git.ffmpeg.org/rtmpdump
# livestreamer: /~https://github.com/chrippa/livestreamer
# livedumper: pip or /~https://github.com/m45t3r/livedumper
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
videodl URL [QUALITY]
Download video URL using livedumper, with QUALITY defaulting to 'best'.
EOF
return 0; fi;
livedumper "$2" "${1:-best}";
}
video-extract () {
(
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
video-extract INPUT_VIDEO OUTPUT_CLIP_NAME [ START_TIME ] END_TIME
Extract a clip from START_TIME (or 00:00:00) to END_TIME from the video file
INPUT_VIDEO and save as OUTPUT_CLIP_NAME.
Time Format: hh:mm:ss[.s...]
Options:
-h --help display this help
-n --no-act display command but don't execute
EOF
shift;
return 0;
elif [[ "$1" =~ ^-n$\|^--no-act(ion)?$ ]]; then
noaction="t";
shift;
fi;
# end options processing
file="$1";
out="$2";
if [[ $# -gt 3 ]]; then
start="-ss $3";
s_start=`timetoseconds $3`;
if [[ $4 == "end" ]]; then
duration="";
else
s_end=`timetoseconds $4`;
duration="-t $((s_end - s_start))";
fi;
else
start="-ss 00:00:00";
duration="-t $3";
fi;
# syntax:
# avconv $startpos -i "$file" $duration -c:a copy -c:v copy "$out"
# alternative: (mencoder doesn't allow fractional seconds in times)
# mencoder -ss $startpos -endpos $endpos -oac pcm -oav copy "$file" "$out"
cmd="avconv $start -i \"$file\" $duration -codec copy -metadata title=\"$out\" \"$out\"";
if [[ -n $noaction ]]; then
echo "$cmd";
else
eval "$cmd";
fi;
)
}
video-extract-duration () {
(
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
video-extract-duration INPUT OUTCLIP STARTTIME DURATION
Extract a clip of DURATION seconds from INPUT beginning at STARTTIME and
save as OUTCLIP.
Time Format: hh:mm:ss[.s...]
Options:
-h --help display this help
-n --no-act display command but don't execute
EOF
shift;
return 0;
elif [[ "$1" =~ ^-n$\|^--no-act(ion)?$ ]]; then
noaction="t";
shift;
fi;
# end options processing
file="$1";
out="$2";
start="-ss $3";
duration="-t $4";
# syntax:
# avconv $startpos -i "$file" $duration -c:a copy -c:v copy "$out"
# alternative: (mencoder doesn't allow fractional seconds in times)
# mencoder -ss $startpos -endpos $endpos -oac pcm -oav copy "$file" "$out"
cmd="avconv $start -i \"$file\" $duration -codec copy -metadata title=\"$out\" \"$out\"";
if [[ -n $noaction ]]; then
echo "$cmd";
else
eval "$cmd";
fi;
)
}
video-concat () {
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
video-concat INPUT_CLIP_1 INPUT_CLIP_2 [OUTPUT_CLIP_NAME]
Concatenate INPUT_CLIP_1 and INPUT_CLIP_2 with transitions.
Save as OUTPUT_CLIP (default: out.mp3)
EOF
return 0;
fi;
melt "$1" "$2" -mix 19 -mixer luma -consumer avformat:"${3:-out.mp4}" vcodec=libxvid b=5000k;
}
animelyrics () {
# Takes either an URL (http://www.animelyrics.com/section/group/song.htm)
# or a JIS-encoded text file (song.jis.txt).
# If an URL is given, the kanji print view version (song.jis.txt) of the
# file is downloaded.
# The JIS-encoded file is then converted to UTF8, except for the ASCII art
# heading, which is left unchanged.
# Finally, line endings are converted to unix format.
# The result is saved as a song.utf8.txt file.
# Requires: iconv (from glibc-common), dos2unix.
if [[ "$1" =~ ^http://www.animelyrics.com/ ]]; then
url="${1/%.htm/.jis.txt}";
fname="${url##*/}";
curl "$url" > "$fname";
else
fname="$1";
fi;
out="${fname/%.jis.txt/.utf8.txt}";
head -n8 "$fname" > "$out";
tail -n+8 "$fname" | iconv -f SHIFT-JIS -t UTF-8 >> "$out";
# ^M characters from line endings
dos2unix "$out";
}
img-rot () {
# requires imagemagick/graphicsmagick
infile="$1";
cw_angle="$2";
outfile="${3:-${infile}_rot_$2}";
if [ -f "$outfile" ]; then
echo "File '$outfile' exists.";
confirm "Overwrite?" || return 1;
fi;
convert "$infile" -rotate "$cw_angle" "$outfile";
}
img-resize () {
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
img-resize [-h | -v] INFILE WxH [OUTFILE]
Resize an image.
The new size is given in the format WIDTHxHEIGHT (as given by 'identify').
If the OUTFILE name is not specified, it is the same as the input file name,
but with '_resized' appended (before the extension).
For example: The command
img-resize myfile.jpg 50x100
will resize myfile.jpg to 50 pixels by 100 pixels and save the result as
myfile_resized.jpg.
Options:
-h --help display this help
-v --verbose verbose
EOF
shift;
return 0;
fi;
if [[ "$1" =~ ^-v$\|^--verbose$ ]]; then
verbose="true";
shift;
fi;
infile="$1";
size="$2";
default_out_name="${infile%.*}_resized.${infile##*.}";
outfile="${3:-$default_out_name}";
if [[ -n $verbose ]]; then
echo "convert \"$infile\" -resize \"$size\" \"$outfile\"";
fi;
convert "$infile" -resize "$size" "$outfile";
}
img-max-dimension() {
{
convert test.png -print "%w %h\n" /dev/null | read w h;
echo $(( w > h ? w : h ));
}
}
# img-crop-NxM WARNING: result is broken for some values of n. Consider using img-crop-NxM-excess.
img-crop-NxM() {
file="$1";
ext="${file##*.}";
crop="$2";
n="${crop%x*}";
m="${crop#*x}";
cropspec="${n}x${m}@";
outfile="${3:-out.$ext}"
grid_colour="${4:-black}";
border="${5:-0}";
borderspec="${border}x${border}";
convert "$file" -bordercolor $grid_colour -background $grid_colour -crop $cropspec -chop 1x1 -splice 1x1 -flatten -border $borderspec $outfile
}
img-crop-WxH() {
file="$1";
ext="${file##*.}";
cell="$2";
w="${cell%x*}";
h="${cell#*x}";
cropspec="${w}x${h}";
outfile="${3:-out.$ext}"
grid_colour="${4:-black}";
border="${5:-0}";
borderspec="${border}x${border}";
convert "$file" -bordercolor $grid_colour -background $grid_colour -crop $cropspec -chop 1x1 -splice 1x1 -flatten -chop 0x1 -border $borderspec $outfile
}
img-crop-NxM-excess() {
file="$1";
ext="${file##*.}";
W=`convert "$file" -print "%w" /dev/null`;
H=`convert "$file" -print "%h" /dev/null`;
crop="$2";
n="${crop%x*}";
m="${crop#*x}";
w="$((W/n))";
h="$((H/m))";
cropspec="${w}x${h}";
outfile="${3:-out.$ext}"
grid_colour="${4:-black}";
border="${5:-0}";
borderspec="${border}x${border}";
convert "$file" -bordercolor $grid_colour -background $grid_colour -crop $cropspec -chop 1x1 -splice 1x1 -flatten -chop 0x1 -border $borderspec $outfile
}
img-copy-at-18%() {
for file in "$@"; do
name="${file%.*}";
ext="${file##*.}";
convert "$file" -resize 18% "$name.18%.$ext";
done;
}
img-white-to-trans() {
[[ "$1" =~ ^-h$\|^--help$ ]] && echo "img-white-to-trans FILE [FUZZ%]" && return 0;
f="$1"; fuzz="$2";
convert -fuzz $fuzz% -transparent white "$f" "${f}_trans.png";
}
vlc-last-screenshot() {
(
n="${1:-1}";
cd ~/Pictures/vlc-snapshot;
display $(\ls -t | tail -n+$n | head -n1 | xargs realpath);
)
}
figurosity-download-pose-00() {
tempf=`mktemp`;
wget -O $tempf "https://figurosity.com/figure-drawing-poses/$1";
src=`<$tempf pup 'meta[property=og:image] attr{content}'`;
wget -O "$1-pose-00" "$src";
}
mkvtracks () {
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
mkvtracks [-h | -v] file
Print a list of tracks in an MKV file.
Options:
-h --help display this help
-v --verbose verbose listing
EOF
shift;
return 0;
fi;
if [[ "$1" =~ ^-v$\|^--verbose$ ]]; then
shift;
mkvinfo "$1" | grep -ie '\(track\|codec id\)';
return 0;
fi;
mkvmerge -i "$1";
}
mkvsubtrack () {
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
mkvsubtrack FILE
Return the track number and format (file extension) of the subtitle track.
EOF
return 0; fi;
mkvmerge -i "$1" |
grep -F 'subtitles' |
perl -pe 's/Track ID (\d+): subtitles \(([^)]+\/)?([^)]+)\)/$1 $3/; tr/A-Z/a-z/;' |
perl -pe 's/vobsub/sub/; s/substationalpha/ssa/';
}
mkvsubextract () {
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
mkvsubextract FILE
Extract subtitle track from mkv file and save in current directory.
EOF
return 0; fi;
read trackID ext <<< `mkvsubtrack "$1"`;
mkvextract tracks "$1" "$trackID":"${1%.*}.$ext";
}
mkvextract-ass () {
file="$1";
trackID="$2";
mkvextract tracks "$file" "$trackID":"${file%.*}.ass";
}
mkvdeletetitle() {
for f in "$@"; do
mkvpropedit "$f" -e info -d title;
done;
}
mkvsettitlebyfilename() {
for f in "$@"; do
mkvpropedit "$f" -e info -s title="${f%.mkv}";
done;
}
mp4deletetitle() {
for f in "$@"; do
AtomicParsley "$f" --title "";
done;
}
mp4settitlebyfilename() {
for f in "$@"; do
AtomicParsley "$f" --title "${f%.mp4}";
done;
}
ffmpegsettitlebyfilename() {
for f in "$@"; do
ffmpeg -i "$f" -vcodec copy -acodec copy -metadata title="${f%.*}" "${f%.*}.out.${f##*.}";
done;
}
ffmpegdeletetitle() {
for f in "$@"; do
ffmpeg -i -codec copy -metadata Title="" "${f%.*}.out.${f##*.}"
done;
}
mediainfo-browse () {
# usage: mediainfo-browse $FILE
# open mediainfo data on $FILE in HTML format in browser
tmp=$(mktemp);
mediainfo --Output=HTML "$1" > "$tmp";
${BROWSER:-sensible-browser} "$tmp";
}
subextract () {
if [[ "$1" =~ ^-h$\|^--help$ ]]; then
cat <<EOF
subextract FILE [FORMAT]
Extract subtitle and convert to FORMAT.
EOF
return 0; fi;
(
file="$1";
format="${2:-srt}";
ffmpeg -i "$name" -vn -an -codec:s "$format" "${name%.*}.$2"
)
}
videonumtracks() {
ffprobe -show_streams "$1" 2>/dev/null |
grep -oF "[STREAM]" |
wc -l;
}
audionumtracks() {
ffprobe -show_streams -select_streams a "$1" 2>/dev/null |
grep -oF "[STREAM]" |
wc -l;
}
audioformat() {
ffprobe -show_streams -select_streams a "$1" 2>/dev/null |
linesbetween '[STREAM]' '[/STREAM]' |
grep -oP '(?<=codec_long_name.)\w+' |
tr 'A-Z' 'a-z';
}
# TODO: allow multiple audio streams with audioformat and audioextract
audioextract() {
name="${1%.*}";
format=$(audioformat "$1");
out_name="${2:-$name.$format}";
[ -e $out_name ] && echo "file exists" && return 1;
ffmpeg -i "$1" -vn -acodec copy "${2:-$name.$format}";
}
ytdl_480p() {
format=$(youtube-dl -F "$1" | grep -s enUS | grep -s x480 | cut -d' ' -f1);
youtube-dl -ci --write-sub --sub-lang enUS -f "$format" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1";
}
ytdl_Np() {
file="$2";
format=$(youtube-dl -F "$file" | grep -s enUS | grep -s "x$1" | head -n1 | cut -d' ' -f1);
youtube-dl -ci --sub-lang enUS --write-sub -f "$format" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$file";
}
CRUNCHYCOOKIES="$HOME/.pentadactyl/cookies/crunchyroll/cookies.txt"
CRUNCHYUA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
CRUNCHYUA="Mozilla/5.0 (X11; Linux i686; rv:89.0) Gecko/20100101 Firefox/89.0"
CRUNCHYYTDL=youtube-dlc;
CRUNCHYYTDL=/opt/yt-dlp_linux/yt-dlp_linux;
CRUNCHYYANKFORMAT="";
crdl_720p() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
format=$($CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" | grep -s enUS | grep -s x720 | head -n1 | cut -d' ' -f1)
$CRUNCHYYTDL -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1"
}
crdl_480p() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
format=$($CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" | grep -s enUS | grep -s x480 | head -n1 | cut -d' ' -f1)
$CRUNCHYYTDL -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1"
}
crdl_360p() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
format=$($CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" | grep -s enUS | grep -s x360 | head -n1 | cut -d' ' -f1)
$CRUNCHYYTDL -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1"
}
crdl_Np() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
format=$($CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$2" | grep -s enUS | grep -s "x$1" | head -n1 | cut -d' ' -f1)
$CRUNCHYYTDL -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$2"
}
crdl_subsonly() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
$CRUNCHYYTDL -ci --all-subs --skip-download --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1"
}
crdl() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
format=$($CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" | iselect -ma | cut -d' ' -f1)
echo "FORMAT: $format";
if [[ -n $CRUNCHYYANKFORMAT ]]; then echo "$format" | xclip; fi;
$CRUNCHYYTDL -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1"
}
crdlc() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
format=$(youtube-dlc -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" | iselect -ma | cut -d' ' -f1)
youtube-dlc -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$1"
}
crdl_choose_format() {
$CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" |
iselect -ma |
cut -d' ' -f1;
}
crdl_print_formats() {
$CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1";
}
crdl_format() {
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
# crdl_format URL FORMAT
url="$1";
format="$2";
$CRUNCHYYTDL -ci --write-sub --sub-lang enUS -f "$format" --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" --xattrs -o "%(title)s-%(id)s.%(ext)s" "$url"
}
crdl_getformat480() {
$CRUNCHYYTDL -F --user-agent "$CRUNCHYUA" --cookies "$CRUNCHYCOOKIES" "$1" | grep -s enUS | grep -s x480 | head -n1 | cut -d' ' -f1;
}
crdl_nthline() {
# crdl_nthline N URLFILE
# "$CRUNCHYCOOKIES" file can be generated by a browser extension
crdl `nthline $1 <"$2"`;
}