Spaces:
Sleeping
Sleeping
Update app/video_preprocess.py
Browse files- app/video_preprocess.py +142 -159
app/video_preprocess.py
CHANGED
|
@@ -1,159 +1,142 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import shutil
|
| 5 |
-
import subprocess
|
| 6 |
-
from dataclasses import dataclass
|
| 7 |
-
from datetime import datetime
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
@dataclass
|
| 11 |
-
class PreprocessResult:
|
| 12 |
-
input_path: str
|
| 13 |
-
output_path: str
|
| 14 |
-
used_ffmpeg: bool
|
| 15 |
-
message: str
|
| 16 |
-
mode: str
|
| 17 |
-
ffmpeg_cmd: list[str]
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def check_ffmpeg_available() -> bool:
|
| 21 |
-
return shutil.which("ffmpeg") is not None
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def ensure_dir(path: str) -> None:
|
| 25 |
-
os.makedirs(path, exist_ok=True)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def build_preprocessed_video_path(output_dir: str, input_path: str, mode: str) -> str:
|
| 29 |
-
ensure_dir(output_dir)
|
| 30 |
-
stem = os.path.splitext(os.path.basename(input_path))[0]
|
| 31 |
-
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 32 |
-
return os.path.join(output_dir, f"{stem}_{mode}_preprocessed_{ts}.mp4")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def _build_ffmpeg_cmd(
|
| 36 |
-
input_path: str,
|
| 37 |
-
output_path: str,
|
| 38 |
-
*,
|
| 39 |
-
mode: str = "analysis",
|
| 40 |
-
remove_audio: bool = False,
|
| 41 |
-
) -> list[str]:
|
| 42 |
-
"""
|
| 43 |
-
三种模式:
|
| 44 |
-
- preview: 调试/预览,体积更小
|
| 45 |
-
- analysis: 正式分析,推荐,尽量保真
|
| 46 |
-
- archival: 更高保真,体积更大
|
| 47 |
-
|
| 48 |
-
说明:
|
| 49 |
-
- 音频默认保留
|
| 50 |
-
- 使用 H.264 + AAC,兼容性最好
|
| 51 |
-
- 使用 CRF 控质量;CRF 越小越保真
|
| 52 |
-
"""
|
| 53 |
-
mode = (mode or "analysis").lower().strip()
|
| 54 |
-
if mode not in {"preview", "analysis", "archival"}:
|
| 55 |
-
raise ValueError(f"不支持的预处理模式: {mode}")
|
| 56 |
-
|
| 57 |
-
if mode == "preview":
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
"-
|
| 85 |
-
"-
|
| 86 |
-
"-
|
| 87 |
-
"-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
"-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
input_path:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
input_path=input_path,
|
| 136 |
-
output_path=output_path,
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
stdout=subprocess.PIPE,
|
| 144 |
-
stderr=subprocess.PIPE,
|
| 145 |
-
text=True,
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
if proc.returncode != 0:
|
| 149 |
-
raise RuntimeError(f"ffmpeg 预处理失败:\n{proc.stderr}")
|
| 150 |
-
|
| 151 |
-
return PreprocessResult(
|
| 152 |
-
input_path=input_path,
|
| 153 |
-
output_path=output_path,
|
| 154 |
-
used_ffmpeg=True,
|
| 155 |
-
message=f"视频预处理完成(mode={mode}, audio={'removed' if remove_audio else 'kept'})",
|
| 156 |
-
mode=mode,
|
| 157 |
-
ffmpeg_cmd=cmd,
|
| 158 |
-
)
|
| 159 |
-
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import subprocess
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@dataclass
|
| 11 |
+
class PreprocessResult:
|
| 12 |
+
input_path: str
|
| 13 |
+
output_path: str
|
| 14 |
+
used_ffmpeg: bool
|
| 15 |
+
message: str
|
| 16 |
+
mode: str
|
| 17 |
+
ffmpeg_cmd: list[str]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def check_ffmpeg_available() -> bool:
|
| 21 |
+
return shutil.which("ffmpeg") is not None
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def ensure_dir(path: str) -> None:
|
| 25 |
+
os.makedirs(path, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def build_preprocessed_video_path(output_dir: str, input_path: str, mode: str) -> str:
|
| 29 |
+
ensure_dir(output_dir)
|
| 30 |
+
stem = os.path.splitext(os.path.basename(input_path))[0]
|
| 31 |
+
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 32 |
+
return os.path.join(output_dir, f"{stem}_{mode}_preprocessed_{ts}.mp4")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def _build_ffmpeg_cmd(
|
| 36 |
+
input_path: str,
|
| 37 |
+
output_path: str,
|
| 38 |
+
*,
|
| 39 |
+
mode: str = "analysis",
|
| 40 |
+
remove_audio: bool = False,
|
| 41 |
+
) -> list[str]:
|
| 42 |
+
"""
|
| 43 |
+
我在这里设置了三种可选择的模式:
|
| 44 |
+
- preview: 调试/预览,体积更小
|
| 45 |
+
- analysis: 正式分析,推荐,尽量保真
|
| 46 |
+
- archival: 更高保真,体积更大
|
| 47 |
+
|
| 48 |
+
说明一下:
|
| 49 |
+
- 音频默认保留
|
| 50 |
+
- 使用 H.264 + AAC,兼容性最好
|
| 51 |
+
- 使用 CRF 控质量;CRF 越小越保真
|
| 52 |
+
"""
|
| 53 |
+
mode = (mode or "analysis").lower().strip()
|
| 54 |
+
if mode not in {"preview", "analysis", "archival"}:
|
| 55 |
+
raise ValueError(f"不支持的预处理模式: {mode}")
|
| 56 |
+
|
| 57 |
+
if mode == "preview":
|
| 58 |
+
width = 640
|
| 59 |
+
fps = 12
|
| 60 |
+
crf = 26
|
| 61 |
+
preset = "veryfast"
|
| 62 |
+
audio_bitrate = "96k"
|
| 63 |
+
elif mode == "analysis":
|
| 64 |
+
width = 1280
|
| 65 |
+
fps = 15
|
| 66 |
+
crf = 18
|
| 67 |
+
preset = "slow"
|
| 68 |
+
audio_bitrate = "128k"
|
| 69 |
+
else: # archival
|
| 70 |
+
width = 1280
|
| 71 |
+
fps = 20
|
| 72 |
+
crf = 16
|
| 73 |
+
preset = "slow"
|
| 74 |
+
audio_bitrate = "160k"
|
| 75 |
+
|
| 76 |
+
vf = f"scale='min({width},iw)':-2,fps={fps}"
|
| 77 |
+
|
| 78 |
+
cmd = [
|
| 79 |
+
"ffmpeg",
|
| 80 |
+
"-y",
|
| 81 |
+
"-i", input_path,
|
| 82 |
+
"-vf", vf,
|
| 83 |
+
"-c:v", "libx264",
|
| 84 |
+
"-preset", preset,
|
| 85 |
+
"-crf", str(crf),
|
| 86 |
+
"-movflags", "+faststart",
|
| 87 |
+
"-pix_fmt", "yuv420p",
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
if remove_audio:
|
| 91 |
+
cmd += ["-an"]
|
| 92 |
+
else:
|
| 93 |
+
cmd += [
|
| 94 |
+
"-c:a", "aac",
|
| 95 |
+
"-b:a", audio_bitrate,
|
| 96 |
+
"-ac", "1",
|
| 97 |
+
"-ar", "16000",
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
cmd.append(output_path)
|
| 101 |
+
return cmd
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def preprocess_video(
|
| 105 |
+
input_path: str,
|
| 106 |
+
output_dir: str,
|
| 107 |
+
mode: str = "analysis",
|
| 108 |
+
remove_audio: bool = False,
|
| 109 |
+
) -> PreprocessResult:
|
| 110 |
+
if not os.path.exists(input_path):
|
| 111 |
+
raise FileNotFoundError(f"视频文件不存在: {input_path}")
|
| 112 |
+
|
| 113 |
+
if not check_ffmpeg_available():
|
| 114 |
+
raise RuntimeError("未检测到 ffmpeg,请先安装 ffmpeg。")
|
| 115 |
+
|
| 116 |
+
output_path = build_preprocessed_video_path(output_dir, input_path, mode)
|
| 117 |
+
cmd = _build_ffmpeg_cmd(
|
| 118 |
+
input_path=input_path,
|
| 119 |
+
output_path=output_path,
|
| 120 |
+
mode=mode,
|
| 121 |
+
remove_audio=remove_audio,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
proc = subprocess.run(
|
| 125 |
+
cmd,
|
| 126 |
+
stdout=subprocess.PIPE,
|
| 127 |
+
stderr=subprocess.PIPE,
|
| 128 |
+
text=True,
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
if proc.returncode != 0:
|
| 132 |
+
raise RuntimeError(f"ffmpeg 预处理失败:\n{proc.stderr}")
|
| 133 |
+
|
| 134 |
+
return PreprocessResult(
|
| 135 |
+
input_path=input_path,
|
| 136 |
+
output_path=output_path,
|
| 137 |
+
used_ffmpeg=True,
|
| 138 |
+
message=f"视频预处理完成(mode={mode}, audio={'removed' if remove_audio else 'kept'})",
|
| 139 |
+
mode=mode,
|
| 140 |
+
ffmpeg_cmd=cmd,
|
| 141 |
+
)
|
| 142 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|