-
Notifications
You must be signed in to change notification settings - Fork 0
/
loudness-cli.py
59 lines (48 loc) · 2.05 KB
/
loudness-cli.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
#!/usr/bin/env python
import argparse
import os
import subprocess
import re
def r128Stats(filePath, stream=0):
""" takes a path to an audio file, returns a dict with the loudness
stats computed by the ffmpeg ebur128 filter """
ffargs = ["ffmpeg",
'-nostats',
'-i',
filePath,
'-filter_complex',
'[a:%s]ebur128' % stream,
'-f',
'null',
'-']
proc = subprocess.Popen(ffargs, stderr=subprocess.PIPE)
stats = proc.communicate()[1]
summaryIndex = stats.rfind('Summary:')
summaryList = stats[summaryIndex:].split()
ILufs = float(summaryList[summaryList.index('I:') + 1])
IThresh = float(summaryList[summaryList.index('I:') + 4])
LRA = float(summaryList[summaryList.index('LRA:') + 1])
LRAThresh = float(summaryList[summaryList.index('LRA:') + 4])
LRALow = float(summaryList[summaryList.index('low:') + 1])
LRAHigh = float(summaryList[summaryList.index('high:') + 1])
statsDict = {'I': ILufs, 'I Threshold': IThresh, 'LRA': LRA,
'LRA Threshold': LRAThresh, 'LRA Low': LRALow,
'LRA High': LRAHigh}
return statsDict
def linearGain(iLUFS, goalLUFS=-16):
""" takes a floating point value for iLUFS, returns the necessary
multiplier for audio gain to get to the goalLUFS value """
gainLog = -(iLUFS - goalLUFS)
return 10 ** (gainLog / 20)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Loudness Analyser.')
parser.add_argument("-i", "--input", help="The video or audio filename ie /path/to/file/filename.mp4", required=True)
parser.add_argument("-s", "--stream", help="The audio stream index", required=False, default=0)
parser.add_argument("-t", "--target", help="The audio stream index", required=False, default=-16)
args = parser.parse_args()
if os.path.isfile(args.input):
statsDict = r128Stats(args.input, args.stream)
statsDict["gain"] = linearGain(statsDict["I"], args.target);
print(statsDict)
else:
print ("%s not found" % args.input)