Untitled

 avatar
unknown
plain_text
a month ago
2.0 kB
2
Indexable
import subprocess
import os

def extract_audio_channels(input_file, output_dir):
    # Check if output directory exists, create it if not
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Probe the input file to determine audio details
    try:
        ffmpeg_probe_command = [
            "ffprobe", "-i", input_file, "-show_entries",
            "stream=channels", "-select_streams", "a", "-of", "json", "-v", "quiet"
        ]
        result = subprocess.run(ffmpeg_probe_command, capture_output=True, text=True, check=True)
    except Exception as e:
        print(f"Error probing the input file: {e}")
        return

    import json
    probe_data = json.loads(result.stdout)
    audio_channels = probe_data.get("streams", [{}])[0].get("channels", 0)

    if not audio_channels:
        print("Could not determine the number of audio channels.")
        return

    print(f"Detected {audio_channels} audio channels.")

    # Extract each channel and save as MP3
    for channel in range(1, audio_channels + 1):
        output_file = os.path.join(output_dir, f"channel_{channel}.mp3")
        ffmpeg_extract_command = [
            "ffmpeg", "-i", input_file,
            "-map", f"0:a:0",  # First audio stream
            "-ac", "1",        # Extract a single channel
            "-filter_complex", f"channelsplit=channel_layout=5.1[CH{channel}]",
            "-map", f"[CH{channel}]",
            "-c:a", "libmp3lame", "-q:a", "2",  # MP3 output quality
            output_file
        ]

        try:
            subprocess.run(ffmpeg_extract_command, check=True)
            print(f"Extracted channel {channel} to {output_file}")
        except Exception as e:
            print(f"Error extracting channel {channel}: {e}")


# Example usage
input_mkv = "input.bdrip.mkv"  # Path to your MKV file
output_directory = "output_channels"  # Output directory for MP3 files
extract_audio_channels(input_mkv, output_directory)
Editor is loading...
Leave a Comment