-
Notifications
You must be signed in to change notification settings - Fork 0
/
abook-speed
executable file
·75 lines (57 loc) · 2.15 KB
/
abook-speed
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
#!/bin/bash
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
set -o errexit
set -o functrace
set -o nounset
if [ "$#" -lt 1 ]; then
echo "Usage: abook-speed SOURCE [SPEED_MULTIPLIER] [VOLUME_BOOST_DECIBELS]"
echo
echo " SPEED_MULTIPLIER: 1.4 by default"
exit
fi
function parse_extension { FILE_NAME_WITH_EXT="$1"
echo "$FILE_NAME_WITH_EXT" | rev | cut -f 1 -d '.' | rev
}
# TODO replace with basename filename ext?
function parse_file_name_no_ext { FILE_NAME_WITH_EXT="$1"
EXTENSION=$(parse_extension "$FILE_NAME_WITH_EXT")
#echo "DEBUG: parse_file_name_no_ext: EXTENSION: $EXTENSION"
echo "$FILE_NAME_WITH_EXT" | sed -e s/\.${EXTENSION}$//g
}
function mediainfo_duration { FILE_PATH="$1"
DURATION=$(mediainfo "$FILE_PATH" | grep Duration | head -n 1 | sed -n -e 's/^Duration[ ]*:[ ]*//p')
echo "$DURATION"
}
set +o nounset
SOURCE_PATH="$1"
SPEED_MULTIPLIER="$2"
VOLUME_BOOST_DECIBELS="$3"
VOLUME_BOOST_COMMAND_PART=
VOLUME_BOOST_FILE_NAME_PART=
set -o nounset
if [ -z "$SPEED_MULTIPLIER" ]; then
SPEED_MULTIPLIER=1.25
fi
if [ -n "$VOLUME_BOOST_DECIBELS" ]; then
VOLUME_BOOST_COMMAND_PART="norm ${VOLUME_BOOST_DECIBELS}"
VOLUME_BOOST_FILE_NAME_PART="__vol${VOLUME_BOOST_DECIBELS}"
fi
SOURCE_DURATION=$(mediainfo_duration "$SOURCE_PATH")
echo "Source Duration: $SOURCE_DURATION"
echo
SOURCE_DIR=$(dirname "$SOURCE_PATH")
SOURCE_FILENAME=$(basename "$SOURCE_PATH")
SOURCE_EXT=$(parse_extension "$SOURCE_FILENAME")
SOURCE_NAME_NO_EXT=$(parse_file_name_no_ext "$SOURCE_FILENAME")
DEST_DIR="$SOURCE_DIR/speed"
DEST_PATH="${DEST_DIR}/${SOURCE_NAME_NO_EXT}__${SPEED_MULTIPLIER}x${VOLUME_BOOST_FILE_NAME_PART}.${SOURCE_EXT}"
mkdir -p "$DEST_DIR"
(
set -x;
# https://madskjeldgaard.dk/posts/sox-tutorial-batch-processing/#normalize
# norm effect which only takes one parameter which is the sound level to normalize to. A reasonable normalization level is -0.1 dB so let us use that in our conversion process.
/usr/bin/time -f "Conversion process time:\t%E" sox "$SOURCE_PATH" "$DEST_PATH" tempo "$SPEED_MULTIPLIER" $VOLUME_BOOST_COMMAND_PART
)
DEST_DURATION=$(mediainfo_duration "$DEST_PATH")
echo
echo "Dest Duration: $DEST_DURATION"