-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
__main__.py
218 lines (167 loc) · 7.44 KB
/
__main__.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
"""
ffmpeg-normalize
ffmpeg / avconv macro for normalizing audio
Audio normalization script, normalizing media files to WAV output
This program normalizes audio to a certain dB level. The default is an RMS-based
normalization where the mean is lifted. Peak normalization is possible with the
-m/--max option. It takes any audio or video file as input, and writes the audio
part as output WAV file.
Usage:
ffmpeg-normalize [options] <input-file>...
Options:
-f --force Force overwriting existing files
-l, --level dB level to normalize to [default: -26]
-p --prefix <prefix> Normalized file prefix [default: 'normalized']
-m --max Normalize to the maximum (peak) volume instead of RMS
-v --verbose Enable verbose output
-n --dry-run Show what would be done, do not convert
-d --debug Show debug output
Examples:
ffmpeg-normalize -v file.mp3
ffmpeg-normalize -v *.avi
"""
#
# The MIT License (MIT)
#
# Copyright (c) 2014 Werner Robitza
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from docopt import docopt
import subprocess
import os
import re
import logging
from . import __version__
logger = logging.getLogger('ffmpeg_normalize')
logger.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
args = dict()
# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, __ = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
FFMPEG_CMD = which('ffmpeg') or which('avconv') or None
if 'avconv' in FFMPEG_CMD:
NORMALIZE_CMD = which('normalize-audio')
if not NORMALIZE_CMD:
raise SystemExit(
"avconv needs the normalize-audio command:\n"
" sudo apt-get install normalize-audio"
)
if not FFMPEG_CMD:
raise SystemExit("Could not find ffmpeg or avconv")
def run_command(cmd, raw=False, dry=False):
cmd = cmd.replace(" ", " ")
cmd = cmd.replace(" ", " ")
logger.debug("[command] {0}".format(cmd))
if dry:
return
if raw:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
else:
p = subprocess.Popen(cmd.split(" "), stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode == 0:
return stdout
else:
logger.error("Error running command: {}".format(cmd))
logger.error(str(stderr))
def ffmpeg_get_mean(input_file):
cmd = FFMPEG_CMD + ' -i "' + input_file + '" -filter:a "volumedetect" -vn -sn -f null /dev/null'
output = run_command(cmd, True)
logger.debug(output)
mean_volume_matches = re.findall(r"mean_volume: ([\-\d\.]+) dB", output)
if (mean_volume_matches):
mean_volume = float(mean_volume_matches[0])
else:
logger.error("could not get mean volume for " + input_file)
raise SystemExit
max_volume_matches = re.findall(r"max_volume: ([\-\d\.]+) dB", output)
if (max_volume_matches):
max_volume = float(max_volume_matches[0])
else:
logger.error("could not get max volume for " + input_file)
raise SystemExit
return mean_volume, max_volume
def ffmpeg_adjust_volume(input_file, gain, output):
global args
if not args['--force'] and os.path.exists(output):
logger.warning("output file " + output + " already exists, skipping. Use -f to force overwriting.")
return
cmd = FFMPEG_CMD + ' -y -i "' + input_file + '" -vn -sn -filter:a "volume=' + str(gain) + 'dB" -c:a pcm_s16le "' + output + '"'
output = run_command(cmd, True, args['--dry-run'])
# -------------------------------------------------------------------------------------------------
def main():
global args
args = docopt(__doc__, version=str(__version__), options_first=False)
if args['--debug']:
ch.setLevel(logging.DEBUG)
elif args['--verbose']:
ch.setLevel(logging.INFO)
logger.debug(args)
for input_file in args['<input-file>']:
if not os.path.exists(input_file):
logger.error("file " + input_file + " does not exist")
continue
path, filename = os.path.split(input_file)
basename = os.path.splitext(filename)[0]
output_file = os.path.join(path, args['--prefix'] + "-" + basename + ".wav")
if 'ffmpeg' in FFMPEG_CMD:
logger.info("reading file " + input_file)
mean, maximum = ffmpeg_get_mean(input_file)
logger.warning("mean volume: " + str(mean))
logger.warning("max volume: " + str(maximum))
target_level = float(args['--level'])
if args['--max']:
adjustment = target_level - maximum
else:
adjustment = target_level - mean
logger.warning("file needs " + str(adjustment) + " dB gain to reach " + str(args['--level']) + " dB")
if maximum + adjustment > 0:
logger.warning("adjusting " + input_file + " will lead to clipping of " + str(maximum + adjustment) + "dB")
ffmpeg_adjust_volume(input_file, adjustment, output_file)
else:
# avconv doesn't seem to have a way to measure volume level, so
# instead we use it to convert to wav, then use a separate programme
# and then convert back to the desired format.
# http://askubuntu.com/questions/247961/normalizing-video-volume-using-avconv
cmd = FFMPEG_CMD + ' -i ' + input_file + ' -c:a pcm_s16le -vn "' + output_file + '"'
output = run_command(cmd, True, args['--dry-run'])
cmd = NORMALIZE_CMD + ' "' + output_file + '"'
output = run_command(cmd, True, args['--dry-run'])
logger.info(output)
if __name__ == '__main__':
main()