Replacement of the audio normalization in mkvopusenc
This commit is contained in:
@@ -32,7 +32,7 @@ class Tee:
|
||||
|
||||
def check_tools():
|
||||
"""Checks if all required command-line tools are in the system's PATH."""
|
||||
required_tools = ["ffmpeg", "ffprobe", "mkvmerge", "sox_ng", "opusenc", "mediainfo"]
|
||||
required_tools = ["ffmpeg", "ffprobe", "mkvmerge", "opusenc", "mediainfo"]
|
||||
print("--- Prerequisite Check ---")
|
||||
all_found = True
|
||||
for tool in required_tools:
|
||||
@@ -78,9 +78,24 @@ def convert_audio_track(stream_index, channels, temp_dir, source_file, should_do
|
||||
ffmpeg_args.extend(["-c:a", "flac", str(temp_extracted)])
|
||||
run_cmd(ffmpeg_args)
|
||||
|
||||
# Step 2: Normalize the track with SoX
|
||||
print(" - Normalizing with SoX...")
|
||||
run_cmd(["sox_ng", "--show-progress", str(temp_extracted), str(temp_normalized), "--temp", str(temp_dir), "loudness", "-18"])
|
||||
# Step 2: Normalize the track with ffmpeg's loudnorm (2-pass)
|
||||
print(" - Normalizing with ffmpeg (loudnorm 2-pass)...")
|
||||
# First pass: Analyze the audio to get loudnorm stats
|
||||
print(" - Pass 1: Analyzing...")
|
||||
result = subprocess.run(
|
||||
["ffmpeg", "-v", "info", "-i", str(temp_extracted), "-af", "loudnorm=I=-14:LRA=7:tp=-1:print_format=json", "-f", "null", "-"],
|
||||
capture_output=True, text=True, check=True)
|
||||
stats_lines = result.stderr.strip().split('\n')
|
||||
stats_json_str = "".join(stats_lines[-12:]) # The JSON block is the last 12 lines
|
||||
stats = json.loads(stats_json_str)
|
||||
|
||||
# Second pass: Apply the normalization using the stats from the first pass
|
||||
print(" - Pass 2: Applying normalization...")
|
||||
run_cmd([
|
||||
"ffmpeg", "-v", "quiet", "-stats", "-y", "-i", str(temp_extracted), "-af",
|
||||
f"loudnorm=I=-14:LRA=7:tp=-1:measured_i={stats['input_i']}:measured_lra={stats['input_lra']}:measured_tp={stats['input_tp']}:measured_thresh={stats['input_thresh']}:offset={stats['target_offset']}",
|
||||
"-c:a", "flac", str(temp_normalized)
|
||||
])
|
||||
|
||||
# Step 3: Encode to Opus with the correct bitrate
|
||||
bitrate = "192k" # Fallback
|
||||
|
||||
Reference in New Issue
Block a user