-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtomp3
More file actions
executable file
·49 lines (38 loc) · 1.49 KB
/
Copy pathtomp3
File metadata and controls
executable file
·49 lines (38 loc) · 1.49 KB
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
#! /bin/sh
FORMATS_TO_BE_CONVERTED="flac|m4a|aac" # seperate by |
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ]; then
cat << EOF
$ tomp3 DIR
Recursively convert to mp3
format that are converted: $FORMATS_TO_BE_CONVERTED
Parameters:
\$1: directory to be converted
EOF
exit
fi
command -v ffmpeg >/dev/null || { echo "ffmpeg is not installed"; exit 127; }
DIR="$1"
[ -z "$1" ] && DIR="."
COUNT=$(mktemp)
find "$DIR" -regextype posix-egrep -type f -regex ".*\.($FORMATS_TO_BE_CONVERTED)" -exec sh -c \
'echo "converting $1"; echo "$1" >>'$COUNT'; ffmpeg -loglevel error -nostdin -i "$1" -ab 320k "${1%.*}".mp3' sh {} ';'
# src: https://unix.stackexchange.com/a/428414
# maybe "-c:a libmp3lame -b:a 320k" if 320 does not work
if [ -s "$COUNT" ]; then
COUNT_CONVERTED=$(wc -l <$COUNT)
COUNT_CURRENT_FILE=$(mktemp)
find "$DIR" -regextype posix-egrep -type f -regex ".*\.($FORMATS_TO_BE_CONVERTED)" >$COUNT_CURRENT_FILE
COUNT_CURRENT=$(wc -l <$COUNT_CURRENT_FILE)
if [ "$COUNT_CONVERTED" != "$COUNT_CURRENT" ]; then
echo "[WARN] discrepancy between the number of converted files vs current matched files."
echo " it is advised to rerun the script."
printf "diff: "
diff $COUNT $COUNT_CURRENT_FILE
printf "continue? (y/N): "
read con
[ "$ans" != "y" ] && exit 0
fi
printf "remove source files? (y/N): "
read ans
[ "$ans" = "y" ] && find "$DIR" -regextype posix-egrep -type f -regex ".*\.($FORMATS_TO_BE_CONVERTED)" -exec sh -c 'rm "$1"' sh {} ';'
fi