-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone-audio-folder-compressed.sh
executable file
·120 lines (97 loc) · 4.1 KB
/
clone-audio-folder-compressed.sh
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
#!/bin/bash
# cfr. https://stackoverflow.com/a/3232082
confirm() {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure?} [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
# only return lines that are EXCLUSIVELY found in the first file
onlyleft() {
# cfr. https://stackoverflow.com/a/53426391
# cat includes.txt excludes.txt excludes.txt | sort | uniq --unique
cat $1 $2 $2 | sort | uniq -u
}
# maybe source folders can also be selected interactively with a program called dialog:
# https://www.bytebang.at/Blog/Select+files+with+a+dialog+witin+a+BASH+script
# or maybe I should use whiptail? (should be like a drop-in replacement)
# TODO: check argument validity and show a help message when they are incorrect
SRCDIR="$1"
DSTDIR="$2"
# aac or ogg (or maybe some others like wma later on)
TYPE="$3"
TMP_SRCFILES=/tmp/srcfiles
TMP_DSTFILES=/tmp/dstfiles
TMP_SRCFILES_NOEXT=/tmp/srcfilesnoext
TMP_DSTFILES_NOEXT=/tmp/dstfilesnoext
TMP_ONLYINSRCFILES=/tmp/onlyinsrcfiles
echo "Will try to make a 'clone' of all the audio files under $SRCDIR into $DSTDIR"
confirm "Do you want to continue?" || exit 0
# confirm "[WATCH OUT] Do you want to remove all the files in $DSTDIR first to start with a clean slate???" && rm -r "${DSTDIR}/*"
( cd "${SRCDIR}" && find . -type f -regex '.*\(flac\|wma\|ape\|wav\)' ) | sort > "${TMP_SRCFILES}"
head "${TMP_SRCFILES}"
( cd "${DSTDIR}" && find . -type f -regex '.*\(flac\|wma\|ape\|wav\)' ) | sort > "${TMP_DSTFILES}"
# cat "${TMP_SRCFILES}" | while read F; do echo "${F%.*}"; done > "${TMP_SRCFILES_NOEXT}"
# cat "${TMP_DSTFILES}" | while read F; do echo "${F%.*}"; done > "${TMP_DSTFILES_NOEXT}"
# onlyleft "${TMP_SRCFILES_NOEXT}" "${TMP_DSTFILES_NOEXT}" > "${TMP_ONLYINSRCFILES}"
# confirm "Print all the files that will be transcoded?" && more "${TMP_ONLYINSRCFILES}"
confirm "Start transcoding?" || exit 1
echo "Let's go..."
(
# only separate on new lines
IFS=$'\n'
# cat "${TMP_SRCFILES}" |
while read -u 10 SRCFILE; do
echo
echo "$SRCFILE"
echo "=========================================================================================="
FNOEXT="${SRCFILE%.*}"
FPATH="${SRCFILE%/*}"
FCONVERTED="${FNOEXT}.${TYPE}"
DSTPATH="${DSTDIR}/${FCONVERTED}"
if [ ! -e "${DSTPATH}" ]; then
# sleep 3
# echo
echo "---> Start converting [${DSTPATH}] <---"
echo
case "${TYPE}" in
aac)
# FOR AAC
#########
# libfdk_aac should be highest quality codec but not available on synology
# libfaac might be lower quality but the default aac is said to be experimental on the current version on diskstation
# -vn should remove the video channel with cover art that some flacs contain apparently
mkdir -p "${DSTDIR}/${FPATH}" && ffmpeg -hide_banner -loglevel error -stats -i "${SRCDIR}/${SRCFILE}" -ab 160000 -acodec aac -vn "${DSTPATH}"
# ffmpeg -i "$1" -b:a 160k -c:a libfdk_aac "${1%.flac}.m4a
;;
m4a)
# FOR AAC
#########
# libfdk_aac should be highest quality codec but not available on synology
# libfaac might be lower quality but the default aac is said to be experimental on the current version on diskstation
# -vn should remove the video channel with cover art that some flacs contain apparently
mkdir -p "${DSTDIR}/${FPATH}" && ffmpeg -hide_banner -loglevel error -stats -i "${SRCDIR}/${SRCFILE}" -ab 160000 -acodec aac -vn "${DSTPATH}"
;;
ogg|wma|mp3)
mkdir -p "${DSTDIR}/${FPATH}" && ffmpeg -hide_banner -loglevel error -stats -i "${SRCDIR}/${SRCFILE}" -ab 160000 -vn "${DSTPATH}"
;;
*)
echo "Unknown type: ${TYPE}"
exit 1
;;
esac
# remove the file if the previous command didn't exit properly??
# $? || rm "${DSTDIR}/${FNOEXT}.m4a"
else
>&2 echo
>&2 echo "----> Skipping [${DSTPATH}] because it already exists... <----"
>&2 echo
fi
done 10<"${TMP_SRCFILES}"
)