-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bash_functions.AV
1757 lines (1687 loc) · 54.5 KB
/
.bash_functions.AV
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# vim: ft=bash noet:
set +x
! declare 2>&1 | grep -wq ^colors= && [ $BASH_VERSINFO -ge 4 ] && source $initDir/.colors
test "$debug" -gt 0 && echo "=> Running $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal ..."
test "$debug" -gt 0 && Echo "\n=> \${BASH_SOURCE[*]} = ${BASH_SOURCE[*]}\n"
type -P ffmpeg >/dev/null 2>&1 && ffmpeg="command ffmpeg -hide_banner" && ffmpegOptions="-probesize 400M -analyzeduration 400M"
type -P ffplay >/dev/null 2>&1 && ffplay="command ffplay -hide_banner" && ffplayOptions="$ffmpegOptions"
type -P ffprobe >/dev/null 2>&1 && ffprobe="command ffprobe -hide_banner" && ffprobeOptions="$ffmpegOptions"
#youtube_dl="eval LANG=C.UTF-8 command youtube-dl" # i.e https://unix.stackexchange.com/questions/505733/ # eval A EVITER
remuxOptions="-map 0 -c copy"
mp4Options="-movflags +frag_keyframe"
mp4RemuxOptions="-map 0 -c copy -c:s mov_text"
aac2m4aOptions="-bsf:a aac_adtstoasc"
cdr_device=/dev/dvd
[ $BASH_VERSINFO -ge 4 ] && declare -Ag audioExtension videoExtension audioDecode # i.e https://unix.stackexchange.com/a/535721/135038
audioExtension=( [libspeex]=spx [speex]=spx [opus]=opus [vorbis]=ogg [aac]=m4a [mp3]=mp3 [mp2]=mp2 [ac3]=ac3 [wmav2]=wma [pcm_dvd]=wav [pcm_s16le]=wav )
videoExtension=( [h264]=m4v [mpeg1video]=mpg [mpeg2video]=vob [mpeg4]=avi [flv1]=flv [wmv3]=wmv )
audioDecode=( [libspeex]=speexdec [speex]=speexdec [opus]=opusdec [vorbis]=oggdec )
#function aacDir2mp3 { inp=aac; shopt -s globstar && time for f in **/*.$inp; do time avconv -i "$f" "${f/.$inp/.mp3}" && rm -v "$f"; done; }
function _16kwav16k2opus {
local inputFile="$1"
if [ -s "$inputFile" ]
then
# title=$(mp3info -p %t "$inputFile")
# echo "==> title = <$title>"
# album=$(mp3info -p %l "$inputFile")
# echo "==> album = <$album>"
# artist=$(mp3info -p %a "$inputFile")
outputFile="${inputFile%.*}.opus"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
# time opusenc --title "$title" --album ALBUM="$album" --vbr "$inputFile" "$outputFile"
time opusenc --vbr "$inputFile" "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
return 1
fi
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
}
function aac2MP44YouTube {
test $# -ne 2 && {
echo "=> Usage: $FUNCNAME aacFile pictureFile" >&2
return 1
}
local audio=$1
local picture=$2
time $ffmpeg -i "$audio" -loop 1 -framerate 4 -i "$picture" -shortest -speed max -c:a copy -preset medium -tune stillimage -crf 18 -pix_fmt yuv420p -movflags +frag_keyframe "${audio%.*}.mp4" #cf. https://trac.ffmpeg.org/wiki/Encode/YouTube
sync
}
function aac2m4a {
# file="$1"
# shift
# time $ffmpeg -i "$file" -acodec copy $aac2m4aOptions $@ "${file/.aac/.m4a}"
for file
do
time $ffmpeg -i "$file" -acodec copy $aac2m4aOptions "${file/.aac/.m4a}"
touch -r "$file" "${file/.aac/.m4a}"
sync
done
}
function aacDir2mp3 { shopt -s globstar && for f in **/*.aac; do /bin/mv -vi "$f" "${f/.aac/.m4a}"; done; time pacpl -v --eopts "-v" -r -k -t mp3 .; }
function audioGet {
# ls "$@" >/dev/null || return
for file
do
extension=${file/*./}
audioCodec=$(audioCodecOfFile "$file" 2>/dev/null)
outputExtension=${audioExtension[$audioCodec]}
outputFileName=${file/.$extension/_AUDIO.$outputExtension}
outputFileName=$(echo $outputFileName | cut -d/ -f3-) # Remove the two leading directories from the path
minimumAudioStreamSize=$(mediainfo --Output="Audio;%StreamSize%" "$file") || return
if test -s "$outputFileName" && [ $(stat -c '%s' "$outputFileName") -ge $minimumAudioStreamSize ]
then
echo "=> INFO: The file $file has already been converted to $outputFileName." >&2
continue
fi
\mkdir -pv $(dirname $outputFileName)
if [ $outputExtension = aac ];then
time $ffmpeg -i "$file" -map 0 -map -0:v -codec copy "$outputFileName" -y
else
time $ffmpeg -i "$file" -map 0 -map -0:v:0 -codec copy "$outputFileName" -y
fi
touch -r "$file" "$outputFileName"
sync
echo
test -s "$outputFileName" && echo "=> Output file is: <$outputFileName>."
done
}
function addChaptersToM4A {
for file
do
MP4Box -chap "${file/.m4a/.chapters}" -add "$file" -new OUTPUT.m4a
\mv -v OUTPUT.m4a OUTPUT.m4b
done
sync
}
function addChaptersToMP4 {
for file
do
MP4Box -chap "${file/.mp4/.chapters}" -add "$file" -new OUTPUT.mp4
done
sync
}
function addSubtitles2media {
local inputVideo=$1
test $# -le 1 && {
echo "=> Usage: $FUNCNAME inputVideo subFile1 subFile2 subFile3 ..." >&2
return 1
}
local extension=${inputVideo/*./}
case $extension in
mp4|m4a|m4b|mov) subTitleCodec=mov_text;;
# webm|mkv|mka) subTitleCodec=srt;;
webm|mkv|mka) subTitleCodec=webvtt;;
*) subTitleCodec=not_supported;;
esac
local outputVideo=${inputVideo/.$extension/_NEW.$extension}
shift
local numberOfSubtitles=$#
(printf "$ffmpeg -i $inputVideo ";printf -- "-i %s " "$@";printf -- "-map 0:a? -map 0:v? ";printf -- "-map %d " $(seq $numberOfSubtitles);printf -- "-c copy -c:s $subTitleCodec $outputVideo\n") | sh -x
local retCode=$?
sync
sleep 1
touch -r "$inputVideo" "$outputVideo"
[ $retCode = 0 ] && echo && \mv -vf "$outputVideo" "$inputVideo" && \rm "$@"
}
function addRespectiveThumbnails4mp4Files {
test $# = 0 && {
echo "=> Usage: $FUNCNAME mp4File1 mp4File2 mp4File3 ..." >&2
return 1
}
local timestampFileRef=null baseName=null artworkFile=null
for mp4File
do
baseName="${mp4File/.*/}"
artworkFile="$(\ls -1 "$baseName".* | \grep -v ".$extension$")"
test ! -s "$artworkFile" && echo "=> [$FUNCNAME][WARNING] There is no artwork for <$mp4File>, skipping ..." >&2 && continue
timestampFileRef=$(mktemp) && touch -r "$mp4File" $timestampFileRef
addThumbnail2mp4_AtomicParsley "$artworkFile" "$mp4File" && rm "$artworkFile"
touch -r $timestampFileRef "$mp4File" && \rm $timestampFileRef
done
}
function addThumbnail2mp4_ffmpeg {
test $# = 0 && {
echo "=> Usage: $FUNCNAME artworkFile videoFileName" >&2
return 1
}
local artworkFile="$1"
shift
local videoFileName="$1"
local timestampFileRef=$(mktemp) && touch -r "$videoFileName" $timestampFileRef
local extension=${videoFileName/*./}
local ffprobeJSON_Info=$($ffprobe -v error -show_format -show_streams -print_format json "$videoFileName")
local embeddedArtworkCodecName=$(echo $ffprobeJSON_Info | jq -r '[ .streams[] | select(.codec_type=="video") ][-1].codec_name')
local major_brand=$(echo $ffprobeJSON_Info | jq -r '.format.tags.major_brand')
local disposition_stream_specifier=null
[ $major_brand = M4A ] && disposition_stream_specifier=v:0 || disposition_stream_specifier=v:1
echo "[ffmpeg] Adding thumbnail to '$videoFileName'"
$ffmpeg -loglevel repeat+error -i "$videoFileName" -i "$artworkFile" -map 0 -map 1 -c copy -disposition:$disposition_stream_specifier attached_pic "${videoFileName/.$extension/_NEW.$extension}"
[ $? = 0 ] && set +x && mv -f "${videoFileName/.$extension/_NEW.$extension}" "$videoFileName" || set +x
touch -r $timestampFileRef "$videoFileName" && \rm $timestampFileRef
sync
}
function addThumbnail2mp4_mp4art {
test $# = 0 && {
echo "=> Usage: $FUNCNAME artworkFile videoFile" >&2
return 1
}
local artworkFile="$1"
shift
local videoFileName="$1"
local timestampFileRef=$(mktemp) && touch -r "$videoFileName" $timestampFileRef
chmod u+w "$videoFileName"
mp4art --remove "$videoFileName"
mp4art --overwrite --add "$artworkFile" "$videoFileName"
touch -r $timestampFileRef "$videoFileName" && \rm $timestampFileRef
chmod -w "$videoFileName"
sync
}
function addThumbnail2mp4_AtomicParsley {
test $# = 0 && {
echo "=> Usage: $FUNCNAME artworkFile videoFile" >&2
return 1
}
local artworkFile="$1"
local artworkExtension="${artworkFile/*./}"
shift
if ! file -b "$artworkFile" | \grep -q JPEG.*JFIF; then
convert -verbose "$artworkFile" "${artworkFile/.*}_NEW.$artworkExtension" && mv -f "${artworkFile/.*}_NEW.$artworkExtension" "$artworkFile"
fi
local videoFileName="$1"
local timestampFileRef=$(mktemp) && touch -r "$videoFileName" $timestampFileRef
chmod u+w "$videoFileName"
AtomicParsley "$videoFileName" --artwork "$artworkFile" --overWrite
touch -r $timestampFileRef "$videoFileName" && \rm $timestampFileRef
chmod -w "$videoFileName"
sync
}
function addURL2mp4_m4a {
test $# = 0 && {
echo "=> Usage: $FUNCNAME url fileName" >&2
return 1
}
local url="$1"
local fileName="$2"
local extension="${fileName/*./}"
chmod u+w "$fileName"
timestampFileRef=$(mktemp) && touch -r "$fileName" $timestampFileRef
[ $extension = mp4 ] || [ $extension = m4a ] && mp4tags -m "$url" "$fileName" && touch -r $timestampFileRef "$fileName" && \rm $timestampFileRef
chmod -w "$fileName"
sync
}
function addURL2Audio_Video {
test $# = 0 && {
echo "=> Usage: $FUNCNAME url fileName" >&2
return 1
}
local url="$1"
local fileName="$2"
local extension="${fileName/*./}"
if $ffmpeg -i "$fileName" -map 0 -c copy -metadata description="$url" "${fileName%.*}-NEW.$extension";then
touch -r "$fileName" "${fileName%.*}-NEW.$extension"
mv -vf "${fileName%.*}-NEW.$extension" "$fileName"
fi
chmod -w "$fileName"
sync
}
function any216kHzopus {
# local inputFile="$1"
local outputFile=""
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile="${inputFile%.*}.opus"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
time $ffmpeg -i "$inputFile" -vn -sn -f wav - | sox -V -t wav - -t wav - channels 1 rate 16k norm | opusenc - "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function any2_3gp_amr_nb {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [-b bitrate in kbps|12.2k] audioFile" >&2
return 1
}
local bitrate=12.2k
local sampling=8k
local reSamplingOption=""
local outputFile=""
echo $1 | \grep -q -- "^-b" && bitrate=$2 && shift 2
echo $1 | \grep -q -- "^-s" && sampling=$2 && shift 2
bitrate=${bitrate/bps/}
test -n "$sampling" && sampling=${sampling/k/} && sampling=${sampling/Hz/}
for inputFile in "$@"
do
if [ -s "$inputFile" ]; then
test -n "$sampling" && outputFile="${inputFile%.*}-${bitrate}bps-${sampling}kHz_amr_nb.3gp" || outputFile="${inputFile%.*}-${bitrate}bps_amr_nb.3gp"
if [ ! -s "$outputFile" ]; then
test -n "$sampling" && { sampling=$(perl -e "{print $sampling*1000}"); reSamplingOption="-ar:a $sampling"; }
set -x
time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a -map 0:s? $reSamplingOption -c:s mov_text -c:a amr_nb -b:a $bitrate -ac:a 1 "$outputFile"
set +x
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." 1>&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." 1>&2
fi
done
}
function any2_3gp_amr_wb {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [-b bitrate in kbps|23.85k] audioFile" >&2
return 1
}
local bitrate=23.85k
local sampling=16k
local reSamplingOption=""
local outputFile=""
echo $1 | \grep -q -- "^-b" && bitrate=$2 && shift 2
echo $1 | \grep -q -- "^-s" && sampling=$2 && shift 2
bitrate=${bitrate/bps/}
test -n "$sampling" && sampling=${sampling/k/} && sampling=${sampling/Hz/}
for inputFile in "$@"
do
if [ -s "$inputFile" ]; then
test -n "$sampling" && outputFile="${inputFile%.*}-${bitrate}bps-${sampling}kHz_amr_wb.3gp" || outputFile="${inputFile%.*}-${bitrate}bps_amr_wb.3gp"
if [ ! -s "$outputFile" ]; then
test -n "$sampling" && { sampling=$(perl -e "{print $sampling*1000}"); reSamplingOption="-ar:a $sampling"; }
set -x
time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a -map 0:s? $reSamplingOption -c:s mov_text -c:a amr_wb -b:a $bitrate -ac:a 1 "$outputFile"
set +x
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." 1>&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." 1>&2
fi
done
}
function any2aac {
# local inputFile="$1"
local outputFile=""
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile="${inputFile%.*}.aac"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
time $ffmpeg -i "$inputFile" -vn -sn -acodec aac -aprofile aac_low -q:a 1 "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function any2aac_HE_AAC_VBR {
# local inputFile="$1"
local outputFile=""
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile="${inputFile%.*}_HE-AAC.aac"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
if $ffprobe "$inputFile" 2>&1 | grep -q Audio:.*mono
then
time $ffmpeg -i "$inputFile" -vn -sn -acodec libfdk_aac -aprofile aac_he -vbr 1 "$outputFile"
else
time $ffmpeg -i "$inputFile" -vn -sn -acodec libfdk_aac -aprofile aac_he_v2 -vbr 1 "$outputFile"
fi
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function any2m4a {
# local inputFile="$1"
local outputFile=""
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile="${inputFile%.*}.m4a"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
time $ffmpeg -i "$inputFile" -vn -sn -acodec aac -aprofile aac_low -q:a 1 "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function any2m4a_HE_AAC_VBR {
# local inputFile="$1"
local outputFile=""
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile="${inputFile%.*}_HE-AAC.m4a"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
if $ffprobe "$inputFile" 2>&1 | grep -q Audio:.*mono
then
time $ffmpeg -i "$inputFile" -vn -sn -acodec libfdk_aac -aprofile aac_he -vbr 1 "$outputFile"
else
time $ffmpeg -i "$inputFile" -vn -sn -acodec libfdk_aac -aprofile aac_he_v2 -vbr 1 "$outputFile"
fi
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function any2amr_nb {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [-b bitrate in kbps|12.2k] audioFile" >&2
return 1
}
local bitrate=12.2k
local sampling=8k
local reSamplingOption=""
local outputFile=""
echo $1 | \grep -q -- "^-b" && bitrate=$2 && shift 2
echo $1 | \grep -q -- "^-s" && sampling=$2 && shift 2
bitrate=${bitrate/bps/}
test -n "$sampling" && sampling=${sampling/k/} && sampling=${sampling/Hz/}
for inputFile in "$@"
do
if [ -s "$inputFile" ]; then
test -n "$sampling" && outputFile="${inputFile%.*}-${bitrate}bps-${sampling}kHz_amr_nb.amr" || outputFile="${inputFile%.*}-${bitrate}bps_amr_nb.amr"
if [ ! -s "$outputFile" ]; then
test -n "$sampling" && { sampling=$(perl -e "{print $sampling*1000}"); reSamplingOption="-ar:a $sampling"; }
# time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a -map 0:s? $reSamplingOption -c:s mov_text -c:a amr_nb -b:a $bitrate -ac:a 1 "$outputFile" # removed subtitles because the resulting file produces many "[ffmpeg/audio] amrnb: Corrupt bitstream" errors
set -x
time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a $reSamplingOption -c:a amr_nb -b:a $bitrate -ac:a 1 "$outputFile"
set +x
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." 1>&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." 1>&2
fi
done
}
function any2amr_wb {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [-b bitrate in kbps|23.85k] audioFile" >&2
return 1
}
local bitrate=23.85k
local sampling=16k
local reSamplingOption=""
local outputFile=""
echo $1 | \grep -q -- "^-b" && bitrate=$2 && shift 2
echo $1 | \grep -q -- "^-s" && sampling=$2 && shift 2
bitrate=${bitrate/bps/}
test -n "$sampling" && sampling=${sampling/k/} && sampling=${sampling/Hz/}
for inputFile in "$@"
do
if [ -s "$inputFile" ]; then
test -n "$sampling" && outputFile="${inputFile%.*}-${bitrate}bps-${sampling}kHz_amr_wb.amr" || outputFile="${inputFile%.*}-${bitrate}bps_amr_wb.amr"
if [ ! -s "$outputFile" ]; then
test -n "$sampling" && { sampling=$(perl -e "{print $sampling*1000}"); reSamplingOption="-ar:a $sampling"; }
# time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a -map 0:s? $reSamplingOption -c:s mov_text -c:a amr_wb -b:a $bitrate -ac:a 1 "$outputFile" # removed subtitles because the resulting file produces many "[ffmpeg/audio] amrwb: Encountered a bad or corrupted frame" errors
set -x
time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a $reSamplingOption -c:a amr_wb -b:a $bitrate -ac:a 1 "$outputFile"
set +x
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." 1>&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." 1>&2
fi
done
}
function any2opus {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [-b bitrate in kbps|64k] [-s frequency in kHz|same as input by default] audioFile" >&2
return 1
}
local bitrate=64k
local sampling=""
local reSamplingOption=""
local outputFile=""
echo $1 | \grep -q -- "^-b" && bitrate=$2 && shift 2
echo $1 | \grep -q -- "^-s" && sampling=$2 && shift 2
bitrate=${bitrate/bps/}
test -n "$sampling" && sampling=${sampling/k/} && sampling=${sampling/Hz/}
for inputFile in "$@"
do
if [ -s "$inputFile" ]; then
test -n "$sampling" && outputFile="${inputFile%.*}-${bitrate}bps-${sampling}kHz_opus.oga" || outputFile="${inputFile%.*}-${bitrate}bps_opus.oga"
if [ ! -s "$outputFile" ]; then
test -n "$sampling" && { sampling=$(perl -e "{print $sampling*1000}"); reSamplingOption="-ar:a $sampling"; }
set -x
time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a $reSamplingOption -vn -c:a libopus -b:a $bitrate -vbr 1 "$outputFile"
set +x
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." 1>&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." 1>&2
fi
done
}
function any2opusVBR {
test $# = 0 && {
echo "=> Usage: $FUNCNAME [-b bitrate in kbps|64k] [-s frequency in kHz|same as input by default] audioFile" >&2
return 1
}
local bitrate=64k
local sampling=""
local reSamplingOption=""
local outputFile=""
echo $1 | \grep -q -- "^-b" && bitrate=$2 && shift 2
echo $1 | \grep -q -- "^-s" && sampling=$2 && shift 2
bitrate=${bitrate/bps/}
test -n "$sampling" && sampling=${sampling/k/} && sampling=${sampling/Hz/}
for inputFile in "$@"
do
if [ -s "$inputFile" ]; then
test -n "$sampling" && outputFile="${inputFile%.*}-${bitrate}bps-${sampling}kHz_opus-VBR.oga" || outputFile="${inputFile%.*}-${bitrate}bps_opus-VBR.oga"
if [ ! -s "$outputFile" ]; then
test -n "$sampling" && { sampling=$(perl -e "{print $sampling*1000}"); reSamplingOption="-ar:a $sampling"; }
set -x
time $ffmpeg $ffmpegOptions -i "$inputFile" -map 0:a $reSamplingOption -vn -sn -f wav - | opusenc - --vbr --bitrate $bitrate "$outputFile"
set +x
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." 1>&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." 1>&2
fi
done
}
function any2wav {
# local inputFile="$1"
local outputFile=""
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile="${inputFile%.*}.wav"
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
time $ffmpeg -i "$inputFile" -vn -sn -f wav "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function any2wavSTDOUT {
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>" >&2
time $ffmpeg -i "$inputFile" -vn -sn -f wav -
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function audio2Video4YouTube {
test $# -ne 2 && {
echo "=> Usage: $FUNCNAME aacFile pictureFile" >&2
return 1
}
local audio=$1
local picture=$2
local audioCodec=$(audioCodecOfFile "$audio" 2>/dev/null)
local video4YouTubeFile=""
case $audioCodec in
aac)video4YouTubeFile="${audio%.*}__4YouTube.mp4"
time $ffmpeg -i "$audio" -loop 1 -framerate 4 -i "$picture" -shortest -speed max -map 0:a -c:a copy -map 1:v -preset medium -tune stillimage -crf 18 -pix_fmt yuv420p -movflags +frag_keyframe "$video4YouTubeFile" #cf. https://trac.ffmpeg.org/wiki/Encode/YouTube
;;
opus|vorbis)video4YouTubeFile="${audio%.*}__4YouTube.webm"
time $ffmpeg -i "$audio" -loop 1 -framerate 1 -i "$picture" -shortest -speed max -map 0:a -c:a copy -map 1:v "$video4YouTubeFile"
;;
*) echo "=> ERROR : $FUNCNAME : $audioCodec is not supported yet." >&2;return 1 ;;
esac
sync
videoInfo "$video4YouTubeFile"
}
function audio2stout {
$local inputFile="$1"
if [ -s "$inputFile" ]
then
$ffmpeg -i "$inputFile" -f wav -
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
}
function audioChannelLayoutOfFile {
local audioChannelLayout
local ffprobeJSON_File_Info=null
for file
do
# audioChannelLayout=$($ffprobe "$file" -select_streams a:0? -show_entries stream=channel_layout -of default=noprint_wrappers=1:nokey=1 -v error)
ffprobeJSON_File_Info=$($ffprobe -v error -show_format -show_streams -print_format json "$file")
audioChannelLayout=$(echo $ffprobeJSON_File_Info | jq -r '[ .streams[] | select(.codec_type=="audio") ][-1].channel_layout')
printf "=> $file : audioChannelLayout = " >&2
echo $audioChannelLayout
done
}
function audioCodecOfFile {
local audioCodec
local ffprobeJSON_File_Info=null
for file
do
# audioCodec=$($ffprobe "$file" -select_streams a:0? -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 -v error)
ffprobeJSON_File_Info=$($ffprobe -v error -show_format -show_streams -print_format json "$file")
audioCodec=$(echo $ffprobeJSON_File_Info | jq -r '[ .streams[] | select(.codec_type=="audio") ][-1].codec_name')
printf "=> $file : audioCodec = " >&2
echo $audioCodec
done
}
function audioDelay {
test $# -ne 2 && {
echo "=> Usage: $FUNCNAME video delay" >&2
return 1
}
local video=$1
local delay=$2
local extension=${video/*./}
time $ffmpeg -i "$video" -itsoffset $delay -i "$video" -c copy -map 0:v -map 1:a "${video%.*}-FIXED.$extension" #cf. https://superuser.com/questions/982342/in-ffmpeg-how-to-delay-only-the-audio-of-a-mp4-video-without-converting-the-au/983153#983153
}
function audioShift {
if [ $# != 2 ] && [ $# != 3 ]; then
echo "=> Usage: $FUNCNAME <filename> hh:mm:ss[.xxx]"
return 1
fi
fileName="$1"
extension=${fileName/*./}
fileBaseName=${fileName%.???}
shift=$2
time $ffmpeg -itsoffset $shift -i "$fileName" $remuxOptions "$fileBaseName-SHIFTED.$extension"
sync
touch -r "$fileName" "$fileBaseName-SHIFTED.$extension"
}
function avprobe {
for arg
do
echo "$arg" | egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $avprobe $avprobeOptions "$(youtube-dl -gf best -- "$arg")" || command avprobe $avprobeOptions -probesize 400M -analyzeduration 400M "$arg"
# done 2>&1 | cut -c 1-$(tput cols)
done
}
function containerOfFile {
local container
local ffprobeJSON_File_Info=null
for file
do
# container=$($ffprobe "$file" -show_entries format=format_name -of default=noprint_wrappers=1:nokey=1 -v error)
ffprobeJSON_File_Info=$($ffprobe -v error -show_format -show_streams -print_format json "$file")
container=$(echo $ffprobeJSON_File_Info | jq -r .format.format_name | cut -d, -f1)
printf "=> $file : container = " >&2
echo $container
done
}
function cropdetect {
ffmpeg -i "$1" -t 1 -vf cropdetect -f null - 2>&1 | awk '/crop/{line=$NF}END{print line}'
}
function ffplay {
local args=("$@")
local lastArg="${@: -1}"
type -P ffprobe >/dev/null && {
echo "$lastArg" | \egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $ffplay -user_agent "$userAgent" $ffplayOptions "$(youtube-dl -gf best -- "${args[@]}")" || $ffplay $ffplayOptions "${args[@]}"
echo
}
}
function ffprobe {
type -P ffprobe >/dev/null && {
for arg
do
echo "$arg" | \egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $ffprobe $ffprobeOptions -user_agent "$userAgent" "$(youtube-dl -gf best -- "$arg")" || $ffprobe $ffprobeOptions "$arg"
echo
done 2>&1
}
}
function ffprobe2Flat {
local ffprobeOptions="-v error -show_format -show_streams -of flat"
for arg
do
echo "$arg" | \egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $ffprobe $ffprobeOptions "$(youtube-dl -gf best -- "$arg")" || $ffprobe $ffprobeOptions "$arg"
done 2>&1 | cut -c 1-$(tput cols)
}
function ffprobe2Ini {
local ffprobeOptions="-v error -show_format -show_streams -of ini"
for arg
do
echo "$arg" | \egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $ffprobe $ffprobeOptions "$(youtube-dl -gf best -- "$arg")" || $ffprobe $ffprobeOptions "$arg"
done 2>&1 | cut -c 1-$(tput cols)
}
function ffprobe2JSON {
local ffprobeUserAgentOption="-user_agent \"$userAgent\""
local ffprobeOptions="-v error -show_format -show_streams -of json"
for arg
do
echo "$arg" | \egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $ffprobe $ffprobeUserAgentOption $ffprobeOptions "$(youtube-dl -gf best -- "$arg")" || $ffprobe $ffprobeOptions "$arg"
done 2>&1 | cut -c 1-$(tput cols)
}
function ffprobe2XML {
local ffprobeOptions="-v error -show_format -show_streams -of xml"
for arg
do
echo "$arg" | \egrep -q "(https?|s?ftps?|ssh|rtmp|rtsp|mms)://" && $ffprobe $ffprobeOptions "$(youtube-dl -gf best -- "$arg")" || $ffprobe $ffprobeOptions "$arg"
done 2>&1 | cut -c 1-$(tput cols)
}
function flac2spx {
$local inputFile="$1"
if [ -s "$inputFile" ]
then
outputFile=${inputFile/.flac/_speex.oga}
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
namedPipe=$(mktemp -u).wav
mkfifo $namedPipe
# time sox -V "$inputFile" -r 16k $namedPipe norm &
# time sox -V "$inputFile" $namedPipe rate 16k norm &
time $ffmpeg -i "$inputFile" -ar 16k $namedPipe -y &
# time speexenc --title "$title" --comment ALBUM="$album" -V --quality 10 --vbr $namedPipe "$outputFile"
time speexenc -V --quality 10 --vbr $namedPipe "$outputFile"
\rm $namedPipe
sync
touch -r "$inputFile" "$outputFile"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
return 1
fi
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
}
function frameCount {
for video
do
echo "Video : $video"
printf "Frames : "
mediainfo --Inform="Video;%FrameCount%" "$video"
echo
done
}
function m2ts2mp4 {
local inputFile="$1"
if [ $# != 2 ]
then
echo "=> Usage: $FUNCNAME inputFileName outputFileName [ffmpegCLIParameters]" >&2
return 1
fi
shift
outputFile="$1"
outputExtension=${outputFile/*./}
local options
case $outputExtension in
vob) options="-f mpeg" ;;
*) options="" ;;
esac
time $ffmpeg -i "$inputFile" -map 0 -c:v copy -c:a copy -c:s mov_text $options "$@"
sync
touch -r "$inputFile" "$outputFile"
}
function m3uConcat {
files="$@"
echo "#EXTM3U"
egrep -hv "EXTM3U|^# *$" $files | dos2unix | egrep -v "^$"
}
function m4a2aac {
# file="$1"
# shift
# time $ffmpeg -i "$file" -acodec copy $@ "${file/.m4a/.aac}"
for file
do
time $ffmpeg -i "$file" -acodec copy "${file/.m4a/.aac}"
touch -r "$file" "${file/.m4a/.aac}"
sync
done
}
function mediainfoSummary {
for media
do
mediainfo "$media"
echo
done | \egrep "^($|Complete name|Width|Height|Encoding settings|Format |Format version|Format profile|Duration|Video|Audio|Text|Menu|Language)| size|Kbps|[Bb]it ?rate|Frame rate|Sampling rate|Channel|Cover|Info"
}
function mkv2ogg {
# file="$1"
# shift
# time $ffmpeg -i "$file" -vn -acodec copy $@ "${file/.mkv/.ogg}"
for file
do
time $ffmpeg -i "$file" -vn -acodec copy "${file/.mkv/.ogg}"
touch -r "$file" "${file/.mkv/.ogg}"
sync
done
}
function mkvInfo {
# file="$1"
# shift
# time time mkvinfo -s "$file" | grep Track
for file
do
echo "=> file = $file" >&2
echo >&2
time mkvinfo -s "$file"
echo >&2
done | grep Track
}
function moov_atomFix {
for inputFile
do
echo "=> inputFile = <$inputFile>" >&2
outputFile="${inputFile/.mp4/_moov_atom_FIXED.mp4}"
qtfaststart "$inputFile" "$outputFile" && touch -r "$inputFile" "$outputFile" && echo "=> outputFile = <$outputFile>" >&2
sync
done
}
function mp32opus {
# file="$1"
# shift
# time $ffmpeg -i "$file" $@ "${file%.*}.opus"
for file
do
set -x
time $ffmpeg $ffmpegOptions -i "$file" -map 0:a -map 0:v:0? -c:a libopus -c:v libtheora -q:v 10 -r:v 1 "${file%.*}.ogg"
set +x
touch -r "$file" "${file%.*}.ogg"
sync
done
}
function mp32spx {
$local inputFile="$1"
if [ -s "$inputFile" ]
then
title=$(mp3info2 -p %t "$inputFile")
echo "==> title = <$title>"
album=$(mp3info -p %l "$inputFile")
echo "==> album = <$album>"
# artist=$(mp3info -p %a "$inputFile")
outputFile=${inputFile/.mp3/_speex.oga}
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
time sox -V "$inputFile" -t wav - channels 1 rate 16k norm | speexenc --title "$title" --comment ALBUM="$album" -V --vbr --quality 10 - "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
return 1
fi
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
}
function mp42mp4_HE_AAC_CBR {
# local inputFile="$1"
for inputFile
do
if [ -s "$inputFile" ]
then
echo "=> inputFile = <$inputFile>"
outputFile=${inputFile/.mp4/_HE-AAC.mp4}
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
if $ffprobe "$inputFile" 2>&1 | grep -q Audio:.*mono
then
time $ffmpeg -i "$inputFile" $remuxOptions -acodec libfdk_aac -aprofile aac_he -ab 64k $mp4Options "$outputFile"
else
time $ffmpeg -i "$inputFile" $remuxOptions -acodec libfdk_aac -aprofile aac_he_v2 -ab 64k $mp4Options "$outputFile"
fi
sync
touch -r "$inputFile" "$outputFile"
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
continue
fi
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
done
}
function mp4DashAudio2aac {
$local inputFile="$1"
if [ -s "$inputFile" ]
then
outputFile=${inputFile/.mp4/.aac}
echo "==> outputFile = $outputFile"
if [ ! -s "$outputFile" ]
then
time $ffmpeg -i "$inputFile" $remuxOptions "$outputFile"
sync
touch -r "$inputFile" "$outputFile"
else
echo "=> ERROR: The file <$outputFile> already exits." >&2
return 1
fi
echo "=> outputFile = <$outputFile>"
else
echo "=> ERROR: The file <$inputFile> does not exit or is empty." >&2
fi
}
function mp4DashAudio2m4a {
$local inputFile="$1"