#!/usr/bin/env python3 """Extract, remux, or encode one selected audio stream from media files.""" from __future__ import annotations import sys from tools.audio_batch import collect_audio_inputs, run_audio_batch from tools.audio_settings import choose_settings, select_stream_indexes from tools.console import launch_terminal_if_needed, pause_if_interactive from tools.executables import require_executable from tools.filesystem import determine_working_dir, input_paths def run(argv: list[str]) -> int: try: args = input_paths(argv) if not args: print("No inputs selected.") return 0 working_dir = determine_working_dir(args) ffmpeg = require_executable("ffmpeg") ffprobe = require_executable("ffprobe") inputs, unreadable = collect_audio_inputs(ffprobe, args) except RuntimeError as exc: print(exc) return 1 for path in unreadable: print(f"Warning: could not probe {path}") without_audio = [item for item in inputs if not item.streams] if without_audio: print(f"Warning: {len(without_audio)} input(s) have no audio stream and will be skipped.") inputs = [item for item in inputs if item.streams] if not inputs: print("No audio streams found.") return 1 print(f"Found {len(inputs)} input(s) with audio.") print("Audio streams:") for item in inputs: details = [] for stream in item.streams: channels = f", {stream.channels} channels" if stream.channels else "" rate = f", {stream.sample_rate} Hz" if stream.sample_rate else "" bitrate = f", {round(stream.bit_rate / 1000)} kbit/s" if stream.bit_rate else "" details.append(f"{stream.codec}{channels}{rate}{bitrate}") print(f" {item.path.name}: {'; '.join(details)}") try: stream_indexes = select_stream_indexes(inputs) settings = choose_settings(ffmpeg, inputs, stream_indexes, working_dir) except RuntimeError as exc: print(exc) return 1 if settings is None: print("Nothing changed.") return 0 failures = run_audio_batch(ffmpeg, inputs, stream_indexes, settings) if failures: print("\nSome files failed:") for failure in failures: print(f" - {failure}") return 1 print("\nDone.") return 0 def main(argv: list[str]) -> int: if launch_terminal_if_needed(argv): return 0 try: return run(argv) finally: pause_if_interactive() if __name__ == "__main__": raise SystemExit(main(sys.argv))