import subprocess from pathlib import Path def get_audio_duration(file_path: Path): """ Get the actual duration of the first audio stream by asking ffprobe to count packets/frames if the stream metadata is missing. Returns duration in seconds. """ # As a last resort, use the format duration result = subprocess.run([ "ffprobe", "-v", "error", "-select_streams", "a:0", "-show_entries", "stream_tags=DURATION", "-of", "default=noprint_wrappers=1:nokey=1", file_path ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) result = result.stdout.strip().split(":")[::-1] res = 0 for i in range(len(result)): res += float(result[i])*(60**i) return res audio_file = Path("/home/honney/mount/Media/tmp/serien/Maid Sama All/Kaichou wa Maid-sama!/S00/Maid-sama_S00E01 - OVA.mkv") duration = get_audio_duration(audio_file) print("Duration of first audio stream (approx.):", duration)