From 93c61640ebb6af4de1c00290ce251d8a13c052d7 Mon Sep 17 00:00:00 2001 From: 17639638825 <562985990@qq.com> Date: Wed, 26 Nov 2025 18:27:42 +0800 Subject: [PATCH] =?UTF-8?q?bug:=E5=A4=9A=E9=9F=B3=E9=81=93=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E5=85=BC=E5=AE=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/utils/video_utils.py | 51 +++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/app/core/utils/video_utils.py b/app/core/utils/video_utils.py index 54cd6885..18bc516c 100644 --- a/app/core/utils/video_utils.py +++ b/app/core/utils/video_utils.py @@ -1,7 +1,7 @@ import os import re import shutil -import subprocess +import subprocess,json import tempfile from contextlib import contextmanager from pathlib import Path @@ -40,6 +40,35 @@ def temporary_subtitle_file(subtitle_path: str): # 自动清理临时文件 Path(temp_path).unlink(missing_ok=True) +def get_best_audio_track(input_file: str) -> int: + """ + 自动选可用音轨:优先 AAC,其次 AAC 5.1,最后 EAC3,若无则用 0 + """ + + try: + r = subprocess.run( + ["ffprobe", "-v", "quiet", "-print_format", "json", "-show_streams", "-select_streams", "a", input_file], + capture_output=True, text=True + ) + streams = json.loads(r.stdout).get("streams", []) + + priority = [ + ("aac", 2), # 最希望:AAC 立体声 + ("aac", None), + ("eac3", None) # 兼容保底 + ] + + for codec, ch in priority: + for i,s in enumerate(streams): + if s.get("codec_name")==codec and (ch==None or s.get("channels")==ch): + logger.info(f"匹配到最优音轨 → index={i}, codec={codec}, channels={s.get('channels')}") + return i + + logger.warning("⚠ 找不到优先音轨,将使用默认音轨0") + return 0 + + except: + return 0 def video2audio(input_file: str, output: str = "", audio_track_index: int = 0) -> bool: """使用 ffmpeg 将视频转换为音频 @@ -52,6 +81,8 @@ def video2audio(input_file: str, output: str = "", audio_track_index: int = 0) - Returns: 转换是否成功 """ + # 自动选择最佳音轨 + audio_track_index = get_best_audio_track(input_file) output_path = Path(output) output_path.parent.mkdir(parents=True, exist_ok=True) output = str(output_path) @@ -61,8 +92,8 @@ def video2audio(input_file: str, output: str = "", audio_track_index: int = 0) - "ffmpeg", "-i", input_file, - "-map", - f"0:a:{audio_track_index}", + "-map", f"0:a:{audio_track_index}", + # f"0:a:{audio_track_index}", "-vn", "-ac", "1", # 单声道 @@ -75,6 +106,16 @@ def video2audio(input_file: str, output: str = "", audio_track_index: int = 0) - logger.info(f"转换为音频执行命令: {' '.join(cmd)}") try: + # result = subprocess.run( + # cmd, + # capture_output=True, + # check=True, + # encoding="utf-8", + # errors="replace", + # creationflags=( + # getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0 + # ), + # ) result = subprocess.run( cmd, capture_output=True, @@ -82,8 +123,8 @@ def video2audio(input_file: str, output: str = "", audio_track_index: int = 0) - encoding="utf-8", errors="replace", creationflags=( - getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0 - ), + getattr(subprocess,"CREATE_NO_WINDOW",0) if os.name=="nt" else 0 + ), ) if result.returncode == 0 and Path(output).is_file(): logger.info("音频转换成功")