-
Notifications
You must be signed in to change notification settings - Fork 0
/
abook-long-split
executable file
·99 lines (78 loc) · 2.57 KB
/
abook-long-split
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
#!/bin/bash
# `sudo apt install mp3splt` required before first run
function mediainfo_readable_duration { FILE_PATH="$1"
DURATION=$(mediainfo "$FILE_PATH" | grep Duration | head -n 1 | sed -n -e 's/^Duration[ ]*:[ ]*//p')
echo "$DURATION"
}
SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# human-readable conditions by qzb (https://github.com/qzb/is.sh)
source "$SCRIPT_DIR/is"
FILE_REL_PATH="$1"
if is empty "$FILE_REL_PATH"; then
echo "Usage: $SCRIPT_NAME file_name [target_dir=./split] [split_by_minutes=10]"
exit
fi
SPLIT_TO_PATH="$2"
SPLIT_BY_MINUTES=$3
if is empty "$SPLIT_BY_MINUTES"; then
SPLIT_BY_MINUTES=10
fi
FILE_ABS_PATH=$(readlink -f "$FILE_REL_PATH")
FILE_DIR_PATH=$(dirname "$FILE_ABS_PATH")
FILE_NAME_WITH_EXT=$(basename "$FILE_REL_PATH")
if is empty "$SPLIT_TO_PATH"; then
SPLIT_TO_PATH="$FILE_DIR_PATH/split"
mkdir -p "$SPLIT_TO_PATH"
fi
#FILE_EXTENSION="${FILE_NAME_WITH_EXT#*.}"
#FILE_NAME="${FILE_NAME_WITH_EXT%*.$FILE_EXTENSION}"
FILE_EXTENSION=$(echo "$FILE_NAME_WITH_EXT" | rev | cut -f 1 -d '.' | rev)
FILE_NAME_NO_EXT=$(echo "$FILE_NAME_WITH_EXT" | sed -e s/\.${FILE_EXTENSION}$//g)
if is not a file "$FILE_ABS_PATH"; then
echo "Error: \"$FILE_ABS_PATH\" is not a file!"
exit 10
fi
if is not a dir "$SPLIT_TO_PATH"; then
echo "\"$SPLIT_TO_PATH\" is not a dir"
exit 20
fi
cd "$SPLIT_TO_PATH"
EXISTING_FILES=$(find -maxdepth 1 -wholename "./$FILE_NAME_NO_EXT*.$FILE_EXTENSION")
if is not empty "$EXISTING_FILES"; then
echo "Target already have file:"
echo -e "$EXISTING_FILES"
exit 30
fi
FILE_DURATION_MS=$(mediainfo --Inform="General;%Duration%" "$FILE_ABS_PATH")
let "FILE_DURATION_S=$FILE_DURATION_MS/1000"
let "SPLIT_BY_S=SPLIT_BY_MINUTES*60"
if is le "$FILE_DURATION_S" "$SPLIT_BY_S"; then
FILE_DURATION_READABLE=$(mediainfo_readable_duration "$FILE_ABS_PATH")
echo "File is too short to split: $FILE_DURATION_READABLE. Copying instead"
CP_TARGET_PATH="$SPLIT_TO_PATH/$FILE_NAME_WITH_EXT"
cp "$FILE_ABS_PATH" "$CP_TARGET_PATH"
ls -lh "$CP_TARGET_PATH"
exit
fi
cd "$FILE_DIR_PATH"
# 5 seconds overlap
mp3splt \
\
`# -T: TAGS_VERSION: id3 v1 + v2` \
-T12 \
\
`# -t: time mode: fixed time length of file parts` \
-t ${SPLIT_BY_MINUTES}.00 \
\
-d "$SPLIT_TO_PATH" \
\
`# -o FORMAT: Output format:` \
`# @f: input filename without extension` \
`# @N2: autoincrement track tag number, 2+ digits` \
-o "@f-@N2-part" \
\
`# -O TIME: Overlap split files: seconds` \
-O 0.5 \
\
"$FILE_ABS_PATH"