Spaces:
Sleeping
Sleeping
Update app/video_preprocess.py
Browse files- app/video_preprocess.py +15 -26
app/video_preprocess.py
CHANGED
|
@@ -36,42 +36,32 @@ def _build_ffmpeg_cmd(
|
|
| 36 |
input_path: str,
|
| 37 |
output_path: str,
|
| 38 |
*,
|
| 39 |
-
mode: str = "
|
| 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 |
preset = "veryfast"
|
| 62 |
-
audio_bitrate = "
|
| 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 |
|
|
@@ -104,7 +94,7 @@ def _build_ffmpeg_cmd(
|
|
| 104 |
def preprocess_video(
|
| 105 |
input_path: str,
|
| 106 |
output_dir: str,
|
| 107 |
-
mode: str = "
|
| 108 |
remove_audio: bool = False,
|
| 109 |
) -> PreprocessResult:
|
| 110 |
if not os.path.exists(input_path):
|
|
@@ -138,5 +128,4 @@ def preprocess_video(
|
|
| 138 |
message=f"视频预处理完成(mode={mode}, audio={'removed' if remove_audio else 'kept'})",
|
| 139 |
mode=mode,
|
| 140 |
ffmpeg_cmd=cmd,
|
| 141 |
-
)
|
| 142 |
-
|
|
|
|
| 36 |
input_path: str,
|
| 37 |
output_path: str,
|
| 38 |
*,
|
| 39 |
+
mode: str = "preview",
|
| 40 |
remove_audio: bool = False,
|
| 41 |
) -> list[str]:
|
| 42 |
+
mode = (mode or "preview").lower().strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if mode not in {"preview", "analysis", "archival"}:
|
| 44 |
raise ValueError(f"不支持的预处理模式: {mode}")
|
| 45 |
|
| 46 |
if mode == "preview":
|
| 47 |
+
# 更适合 Hugging Face Space
|
| 48 |
+
width = 480
|
| 49 |
+
fps = 8
|
| 50 |
+
crf = 30
|
| 51 |
preset = "veryfast"
|
| 52 |
+
audio_bitrate = "64k"
|
| 53 |
elif mode == "analysis":
|
| 54 |
+
width = 960
|
| 55 |
+
fps = 12
|
| 56 |
+
crf = 24
|
| 57 |
+
preset = "fast"
|
| 58 |
+
audio_bitrate = "96k"
|
| 59 |
+
else:
|
| 60 |
width = 1280
|
| 61 |
fps = 15
|
| 62 |
crf = 18
|
| 63 |
preset = "slow"
|
| 64 |
audio_bitrate = "128k"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
vf = f"scale='min({width},iw)':-2,fps={fps}"
|
| 67 |
|
|
|
|
| 94 |
def preprocess_video(
|
| 95 |
input_path: str,
|
| 96 |
output_dir: str,
|
| 97 |
+
mode: str = "preview",
|
| 98 |
remove_audio: bool = False,
|
| 99 |
) -> PreprocessResult:
|
| 100 |
if not os.path.exists(input_path):
|
|
|
|
| 128 |
message=f"视频预处理完成(mode={mode}, audio={'removed' if remove_audio else 'kept'})",
|
| 129 |
mode=mode,
|
| 130 |
ffmpeg_cmd=cmd,
|
| 131 |
+
)
|
|
|