使用ffmpeg进行音频响度标准化可以减少音频文件的持续时间?

时间:2020-04-26 17:57:22

标签: python ffmpeg

我正在使用以下代码来规范音频响度,但是运行代码后,音频的长度/持续时间会减少...我不知道我该在哪里进行更改..有人可以帮我吗为此....如果可能,请也解释代码。...我对此并不陌生,我不知道为什么。.

def r128Stats(filePath):

ffargs = ['ffmpeg',
          '-nostats',
          '-i',
          filePath,
          '-filter_complex',
          'ebur128',
          '-f',
          'null',
          '-']

proc = subprocess.Popen(ffargs, stderr=subprocess.PIPE)
stats = proc.communicate()[1]
summaryIndex = stats.rfind('Summary:'.encode())
summaryList = stats[summaryIndex:].split()
ILufs = float(summaryList[summaryList.index('I:'.encode()) + 1])
IThresh = float(summaryList[summaryList.index('I:'.encode()) + 4])
LRA = float(summaryList[summaryList.index('LRA:'.encode()) + 1])
LRAThresh = float(summaryList[summaryList.index('LRA:'.encode()) + 4])
LRALow = float(summaryList[summaryList.index('low:'.encode()) + 1])
LRAHigh = float(summaryList[summaryList.index('high:'.encode()) + 1])
statsDict = {'I': ILufs, 'I Threshold': IThresh, 'LRA': LRA,
             'LRA Threshold': LRAThresh, 'LRA Low': LRALow,
             'LRA High': LRAHigh}
return statsDict

def linearGain(iLUFS, goalLUFS=-23):
""" 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)


def ffApplyGain(inPath, outPath, linearAmount):

ffargs = ['ffmpeg', '-y', '-i', inPath,
          '-af', 'volume=' + str(linearAmount)]
if outPath[-10:].lower() == '.mp3':
    ffargs += ['-acodec', 'libmp3lame', '-aq', '0']
ffargs += [outPath]
subprocess.Popen(ffargs, stderr=subprocess.PIPE)


def notAudio(filePath):
if os.path.basename(filePath).startswith("audio"):
    return True
thisMime = mimetypes.guess_type(re.escape(filePath))[0]
if thisMime is None or not thisMime.startswith("audio"):
    return True
return False


def neg23File(filePath):
if notAudio(filePath):
    print("Not a valid audio file.")
    return False
print("Scanning " + filePath + " for loudness...")
try:
    loudnessStats = r128Stats(filePath)
except:
    print("neg23 encountered an error scanning " + filePath)
gainAmount = linearGain(loudnessStats['I'])
print("Creating -23LUFS file at " + filePath)
try:
    # applying the normalisation and saving it to the same place as it was (replacing)
    ffApplyGain(filePath, filePath, gainAmount)
except:
    print("neg23 encountered an error applying gain to " + filePath)
print("Done")


def normalise(args):
source = os.path.abspath(args.source)
results = {"Successfully normalised": 0, "Unsuccessful": 0}

for filename in glob.glob(source + '/**/*.wav', recursive=True):
    try:
        neg23File(filename)
        results["Successfully normalised"] += 1
    except:
        results["Unsuccessful"] += 1           

print("\n")
print(results)

0 个答案:

没有答案