I need to figure out the difference in loudness between two files (A
and B
) expressed in decibels
, so that I can pass this value to an application as an argument so file A
's audible volume can be played back at a similar level as file B
's.
Borrowing code from this answer I have a function that extracts data from both audio and video files:
import numpy as np
from moviepy.editor import AudioFileClip
def get_volume(fname):
clip = AudioFileClip(fname)
cut = lambda i: clip.subclip(i,i+1).to_soundarray()
volume = lambda array: np.sqrt(((1.0*array)**2).mean())
return np.array([volume(cut(i)) for i in range(0,int(clip.duration-2))]).max()
With this code I can extract values out of both audio and video files:
# in this example, the video file is louder than the audio file
A = get_volume(<path_to_some_video_file>).max() # 0.12990663749524628
B = get_volume(<path_to_some_audio_file>).max() # 0.10334934486576164
delta = A-B # 0.02655729262948464
In this example the video file A
's volume is louder than the audio file B
's. I need to convert the delta in decibels
so that in a cli
I can pass that value as an argument to either boost or reduce the audio output so the file A
can be played back at a volume matches file B
's.
# CLI example command where lets say the delta (0.026...) is converted to -12db
# so the video file's volume will match the audio's loudness with
<my application> -volume -12 <path_to_file_video_file>
QUESTION
What is the proper way to take the difference between both file's audio output and calculate a difference expressed in decibels
.