From 1177f5499656da8e34e804d8ad7f6bed05d8ca25 Mon Sep 17 00:00:00 2001 From: pat-e Date: Mon, 22 Sep 2025 13:04:20 +0200 Subject: [PATCH] Reverted Backup to working function --- MkvOpusEnc.py | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/MkvOpusEnc.py b/MkvOpusEnc.py index 587c643..3e2fec1 100644 --- a/MkvOpusEnc.py +++ b/MkvOpusEnc.py @@ -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", "opusenc", "mediainfo"] + required_tools = ["ffmpeg", "ffprobe", "mkvmerge", "sox_ng", "opusenc", "mediainfo"] print("--- Prerequisite Check ---") all_found = True for tool in required_tools: @@ -78,40 +78,9 @@ 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 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=-18:LRA=7:tp=-1:print_format=json", "-f", "null", "-"], - capture_output=True, text=True, check=True) - - # Find the start and end of the JSON block to avoid parsing extra data. - stderr_output = result.stderr - json_start_index = stderr_output.find('{') - if json_start_index == -1: - raise ValueError("Could not find start of JSON block in ffmpeg output for loudnorm analysis.") - - brace_level = 0 - json_end_index = -1 - for i, char in enumerate(stderr_output[json_start_index:]): - if char == '{': - brace_level += 1 - elif char == '}': - brace_level -= 1 - if brace_level == 0: - json_end_index = json_start_index + i + 1 - break - - stats = json.loads(stderr_output[json_start_index:json_end_index]) - - # 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=-18: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 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 3: Encode to Opus with the correct bitrate bitrate = "192k" # Fallback