-
Notifications
You must be signed in to change notification settings - Fork 53
/
ringtonemaker
executable file
·130 lines (111 loc) · 3.16 KB
/
ringtonemaker
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
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
readonly program="$(basename "${0}")"
# Default values
readonly max_duration='40' # Maxium allowed duration for iPhone ringtones
duration="${max_duration}"
cut_start='00:00:00'
fade_duration='1'
function depends_on {
local -r all_deps=("${@}")
local missing_deps=()
for dep in "${all_deps[@]}"; do
hash "${dep}" 2> /dev/null || missing_deps+=("${dep}")
done
if [[ "${#missing_deps[@]}" -gt 0 ]]; then
echo 'Missing required tools:' >&2
printf ' %s\n' "${missing_deps[@]}" >&2
exit 1
fi
}
function get_output_path {
local -r ext="${1}"
local -r input_path="${2}"
local -r init_output_path="${3}"
if [[ -n "${init_output_path}" ]]; then
[[ "${init_output_path##*.}" == "${ext##*.}" ]] && echo "${init_output_path}" || echo "${init_output_path}${ext}"
else
echo "$(pwd -P)/$(basename "${input_path%.*}${ext}")"
fi
}
function try_overwrite {
local -r force="${1}"
local -r input_path="${2}"
if [[ "${force}" == 'true' ]]; then
mkdir -p "$(dirname "${input_path}")"
return 0
fi
if [[ ! -d "$(dirname "${input_path}")" ]]; then
echo "Cannot create '${input_path}'. Parent directory does not exist." >&2
exit 1
fi
if [[ -e "${input_path}" ]]; then
echo "Cannot write to '${input_path}'. Already exists." >&2
exit 1
fi
}
function usage {
echo "
Usage:
${program} [-d <duration>] [-s <time>] [-o <output_file>] <input_file>
Options:
-d <number>, --duration <number> Duration (in seconds) of the output file. Maximum is "${max_duration}".
-f, --fade-out <number> Duration of audio fade out at the end. Default is ${fade_duration}.
-o <file>, --output-file <file> File to output to. Default is with same name on current directory.
-O, --overwrite Create intermediary directories and overwrite output.
-s <time>, --start <time> Start time from when to start cutting, in the form xx:xx:xx
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Options
args=()
while [[ "${1}" ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-d | --duration)
duration="${2}"
shift
;;
-f | --fade-out)
fade_duration="${2}";
shift
;;
-o | --output-file)
given_output_path="${2}"
shift
;;
-O | --overwrite)
readonly overwrite='true'
;;
-s | --start)
cut_start="${2}"
shift
;;
--)
shift
args+=("${@}")
break
;;
-*)
echo "Unrecognised option: ${1}"
exit 1
;;
*)
args+=("${1}")
;;
esac
shift
done
set -- "${args[@]}"
depends_on 'ffmpeg'
readonly input_file="${1}"
readonly output_file="$(get_output_path '.m4r' "${input_file}" "${given_output_path}")"
try_overwrite "${overwrite:-false}" "${output_file}"
if [[ "${#}" -ne 1 || ! -f "${input_file}" ]]; then
usage
exit 1
fi
[[ "${duration}" -gt "${max_duration}" ]] && duration="${max_duration}"
ffmpeg -i "${input_file}" -af "afade=t=out:st=$(bc <<< "${duration} - ${fade_duration}"):d=${fade_duration}" -vn -acodec aac -f mp4 -ss "${cut_start}" -t "${duration}" "${output_file}"