33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
from pathlib import Path
|
|
import subprocess
|
|
|
|
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]
|
|
# if DEBUG: print(f"audio duration: {result}")
|
|
res = 0
|
|
if not result:
|
|
raise ValueError(f"Could not extract length")
|
|
for i in range(len(result)):
|
|
res += float(result[i])*(60**i)
|
|
return res
|
|
|
|
printget_audio_duration(Path("/home/honney/mount/Media/tmp/serien/ready/Classroom of the Elite/Season 01/E01_original_audio2.mka")) |