-
Notifications
You must be signed in to change notification settings - Fork 0
/
any2ogg
executable file
·100 lines (87 loc) · 2.47 KB
/
any2ogg
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
#!/bin/bash
msg() {
echo ">> $*"
}
die() {
msg "$*"
exit 1
}
cat <<END >&2
any2ogg by AITap, 2011
Published under GPLv3 or later
END
if [ $# -eq 0 ]
then
cat <<END
Usage: $0 <file.flac|file.ape|file.wv> [file ...]
- splits selected files using CUE sheet (if detected)
- recodes them to OGG Vorbis
- recursively self-calls on directory contents
- is tag-friendly
END
exit 0
fi
for p in shntool ffmpeg cuetag enconv flac gawk
do
which $p &>/dev/null || msg "$p not found on your system!"
done
self="$(realpath "$0")"
for file in "$@"
do
msg "Doing: $file"
basename=$(basename "$file")
if [ -d "$file" ]
then
msg "'$file' is a directory"
file=$(readlink -e "$file")
mkdir -p "$basename" || die "mkdir failed"
cd "$basename"
"$self" "$file"/*
cd ..
continue
elif [ -r "$file" ] && echo "$file" | egrep -qi "\.(flac|wv|ape|m4a|wav)$"
then
for guess in "${file}.cue" "$(sed -r 's/\..{2,4}$/.cue/'<<<"$file")" # встроенным variable substitution - не вышло
do
if [ -r "$guess" -a "$guess" != "$file" ]
then
msg "Detected CUE: $guess"
cue=$(date +/tmp/%s.cue)
enconv < "$guess" > "$cue" || cp "$guess" "$cue"
break
fi
done
if [ -z "$cue" ] && ffprobe -show_format "$file" 2>/dev/null | grep -q 'TAG:Cuesheet'
then
msg "Detected embedded CUE"
cue=$(date +/tmp/%s.cue)
ffprobe -show_format "$file" | \
sed -n 'H;${g;s/.*TAG:Cuesheet=//;s/\nTAG:.*//;p}' > "$cue"
enconv "$cue"
fi
if [ -n "$cue" ]
then
file=$(readlink -e "$file")
dir=$(gawk '($2=="DATE"){Y=$3};($1=="TITLE"){$1="";T=gensub(/^ |["\r]/,"","g")};/TRACK/{printf "%4d - %s",Y,gensub(/[\/:]/,"_","g",T);exit}' "$cue")
[ -z "$dir" ] && dir="$basename"
mkdir -pv "$dir" && cd "$dir" || die "mkdir failed"
msg "Splitting file"
if echo "$file" | grep -qi '\.ape$' && ! command -v mac > /dev/null; then # kludge for debian - there is no monkey's audio package
export ST_APE_DEC='ffmpeg -loglevel fatal -i %f -f wav -'
fi
shnsplit -o 'cust ext=ogg ffmpeg -i - -loglevel fatal -c:a libvorbis -q:a 7 %f' -m '?_/-:_' -t "%n. %t" -f "$cue" "$file" || die "shnsplit failed"
rm -vf "00. pregap.ogg"
msg "Tagging files"
cuetag "$cue" *.ogg
msg "Cleaning up"
rm -vf "$cue"
cd ..
continue
else
ffmpeg -loglevel warning -i "$file" -vn -aq 5 -acodec libvorbis "$(sed -r 's/\..{2,4}$/.ogg/'<<<${basename})" || die "ffmpeg failed"
fi
else
msg "'$file' doesn't look like a suitable file, skipping"
continue
fi
done