diff --git a/tv_audio_encoder.py b/tv_audio_encoder.py index 5539b68..9693caf 100644 --- a/tv_audio_encoder.py +++ b/tv_audio_encoder.py @@ -201,7 +201,16 @@ def main(no_downmix=False): audio_tracks_to_remux = [] audio_streams = [s for s in ffprobe_info.get("streams", []) if s.get("codec_type") == "audio"] + # Build mkvmerge track mapping by track ID + mkv_audio_tracks = {t["id"]: t for t in mkv_info.get("tracks", []) if t.get("type") == "audio"} + + # Build mediainfo track mapping by StreamOrder + media_tracks_data = media_info.get("media", {}).get("track", []) + mediainfo_audio_tracks = {int(t.get("StreamOrder", -1)): t for t in media_tracks_data if t.get("@type") == "Audio"} + for stream in audio_streams: + # Extract these values once per stream for clarity + stream_index = stream["index"] stream_index = stream["index"] codec = stream.get("codec_name") channels = stream.get("channels", 2) @@ -226,6 +235,7 @@ def main(no_downmix=False): # Convert any codec that is not in REMUX_CODECS opus_file = convert_audio_track( stream_index, channels, language, audio_temp_dir, str(input_file_abs), not no_downmix + stream_index, channels, language, audio_temp_dir, str(input_file_abs), not no_downmix ) processed_audio_files.append({ "Path": opus_file, @@ -239,6 +249,7 @@ def main(no_downmix=False): # Final mux print("Assembling final file with mkvmerge...") mkvmerge_args = ["mkvmerge", "-o", str(intermediate_output_file), str(created_encoded_video_path)] + for file_info in processed_audio_files: sync_switch = ["--sync", f"0:{file_info['Delay']}"] if file_info["Delay"] else [] mkvmerge_args += [ @@ -247,6 +258,7 @@ def main(no_downmix=False): ] + sync_switch + [str(file_info["Path"])] source_copy_args = ["--no-video"] + if audio_tracks_to_remux: source_copy_args += ["--audio-tracks", ",".join(audio_tracks_to_remux)] else: