-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaduration
executable file
·132 lines (110 loc) · 9.42 KB
/
mediaduration
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
#!/bin/bash
# nb: the swp file that editscript relies on is provided by nano
# <konsolebox_> iconoclast_hero: 1) Yes that's one way to do it but bash doesn't support decimals so checking if the result is > 0 would be more complicated. 2) You can use printf and pipe it back to Perl.
# <konsolebox_> iconoclast_hero: You can also just do everything in Perl: ... | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t += $1); END { if ($t > 0) { printf "%02d:%02d:%02d\n", $t/3600, $t/60%60, $t%60; }; }'
finddur(){
IFS=':'; tdurs=( $(find -type f -iname "*.$1" -print0 2>/dev/null |
xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null |
perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' |
tail -n 1)
)
[[ "${tdurs[0]}" ]] && tdur=$(( 10#${tdurs[0]} + 10#${tdurs[1]} + 10#${tdurs[2]} )) || tdur=0
[[ "$tdur" -gt 0 ]] && [[ "$1" != opus ]] &&
printf 'Duration of .%s files : %s\n' "$1" "${tdurs[*]}" ||
printf 'Duration of .%s files: %s\n' "$1" "${tdurs[*]}"
}
editscript(){
local scriptname script path swp; scriptname=$(realpath "$0" 2>/dev/null); script="${scriptname##*/}"; path="${scriptname%/*}"; swp="$path/.$script.swp"
[[ ! -e "$swp" ]] && printf "\n\n%s\n\n" "$swp" && (/usr/bin/nano "$scriptname") && exit
printf "\n%s is already being edited.\n%s exists; try fg or look in another window.\n" "$scriptname" "$swp"; exit ;}
pause(){ read -p "$*" ; }
[[ "$1" == @(edit|e|nano|-e|-E) ]] && editscript
readarray exts < <(find . -type f 2>/dev/null | sed -rn 's|.*/[^/]+\.([^/.]+)$|\1|p' | sort -u)
if [ -n "$1" ];
then
find -type f -name "*.$1" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
else
#mp3
[[ "${exts[@]}" =~ mp3 ]] && finddur mp3
#m4a
[[ "${exts[@]}" =~ m4a ]] && finddur m4a
#m4b
[[ "${exts[@]}" =~ m4b ]] && finddur m4b
#flac
[[ "${exts[@]}" =~ flac ]] && finddur flac
#ogg
[[ "${exts[@]}" =~ ogg ]] && finddur ogg
#mp4
[[ "${exts[@]}" =~ mp4 ]] && finddur mp4 #printf "Duration of .mp4 files: " && find -type f -iname "*.mp4" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
#avi
[[ "${exts[@]}" =~ avi ]] && finddur avi #printf "Duration of .avi files: " && find -type f -iname "*.avi" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
#shn -- ffmpeg doesn't seem to work with this...
[[ "${exts[@]}" =~ shn ]] && printf "Duration of .shn files: " && find -type f -iname "*.shn" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
# [[ "${exts[@]}" =~ shn ]] && printf "Duration of .shn files: ffmpeg" && find -type f -iname "*.shn" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
#wav -- ffmpeg usually has trouble with this
[[ "${exts[@]}" =~ wav ]] && finddur wav #printf "Duration of .wav files: " && find -type f -iname "*.wav" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
[[ "${exts[@]}" =~ wav ]] && printf "Duration of .wav files: " && find -type f -iname "*.wav" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
[[ "${exts[@]}" =~ wav ]] && printf "Duration of .wav files: " && find -type f -iname "*.wav" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
#opus
[[ "${exts[@]}" =~ opus ]] && finddur opus
fi
exit
#cleaned up above and left the before below commented out:
#----------------------
#
#
#
#
##mp3
# [[ "${exts[@]}" =~ mp3 ]] && finddur mp3 #printf "Duration of .mp3 files: " && find -type f -iname "*.mp3" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' |tail -n 1
##mp3 too
## [[ "${exts[@]}" =~ mp3 ]] && printf "Duration of .mp3 files: " && find -type f -iname "*.mp3" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
## [[ "${exts[@]}" =~ mp3 ]] && printf "Duration of .mp3 files: " && find -type f -iname "*.mp3" -print0 -exec sh -c 'while IFS= read -r -d '' {}; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {} 2>/dev/null; done' | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
##m4a
# [[ "${exts[@]}" =~ m4a ]] && finddur m4a # printf "Duration of .m4a files: " && find -type f -iname "*.m4a" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
##m4b
# [[ "${exts[@]}" =~ m4b ]] && finddur m4b
#
## [[ "${exts[@]}" =~ m4b ]] &&
## printf "Duration of .m4b files: " &&
## find -type f -iname "*.m4b" -print0 2>/dev/null |
## xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null |
## perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' |
## tail -n 1
#
##flac
#[[ "${exts[@]}" =~ flac ]] && finddur flac
#
##if [[ "${exts[@]}" =~ flac ]]
## then
## tdur=$(find -type f -iname "*.flac" -print0 2>/dev/null |
## xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null |
## perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' |
## tail -n 1)
## [[ "${tdur##*:}" -gt 0 ]] && printf 'Duration of .flac files: %s' "$tdur"
## fi
#
## [[ "${exts[@]}" =~ flac ]] && printf "Duration of .flac (ffmpeg) files: " && find -type f -iname "*.flac" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
#
##ogg
#[[ "${exts[@]}" =~ ogg ]] && finddur ogg
#
##mp4
# [[ "${exts[@]}" =~ mp4 ]] && finddur mp4 #printf "Duration of .mp4 files: " && find -type f -iname "*.mp4" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
#
##avi
# [[ "${exts[@]}" =~ avi ]] && finddur avi #printf "Duration of .avi files: " && find -type f -iname "*.avi" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
#
##shn -- ffmpeg doesn't seem to work with this...
## [[ "${exts[@]}" =~ shn ]] && printf "Duration of .shn files: " && find -type f -iname "*.shn" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
## [[ "${exts[@]}" =~ shn ]] && printf "Duration of .shn files: ffmpeg" && find -type f -iname "*.shn" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
#
##wav -- ffmpeg usually has trouble with this
# [[ "${exts[@]}" =~ wav ]] && finddur wav #printf "Duration of .wav files: " && find -type f -iname "*.wav" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
## [[ "${exts[@]}" =~ wav ]] && printf "Duration of .wav files: " && find -type f -iname "*.wav" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
##opus
# [[ "${exts[@]}" =~ opus ]] && finddur opus #printf "Duration of .opus files: " && find -type f -iname "*.opus" -print0 2>/dev/null | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1
## [[ "${exts[@]}" =~ opus ]] && printf "Duration of .opus files: " && find -type f -iname "*.opus" -print0 | while IFS= read -r -d '' file; do ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null; done | awk '{s+=$1} END {printf "%02d:%02d:%02d\n",s/3600,s%3600/60,s%60}'
#fi
#
#