Files
momentry_core/src/core/proxy.rs
T
Accusys e0eb7b9ce8 fix: resolve P2 hardcoded paths to use runtime resolution (A-4.2 P2)
Issues: Direct hardcoded paths without fallback
- proxy.rs:108 - audio_track_probe.py
- tool_selector.rs:136 - output_dir
- ocr.rs:241 - swift_ocr binary
- files.rs:1190 - legacy output dirs

Impact: These paths don't exist on release machines

Solution:
1. proxy.rs: Use path_utils::scripts_dir()
2. tool_selector.rs: Use config::OUTPUT_DIR
3. ocr.rs: Use swift_processor_path() helper
4. face.rs: Make swift_processor_path() public
5. files.rs: Reduce duplicate legacy paths

Changes:
- src/core/proxy.rs: Resolve audio_track_probe.py
- src/core/processor/tool_selector.rs: Use OUTPUT_DIR from config
- src/core/processor/ocr.rs: Use swift_processor_path()
- src/core/processor/face.rs: Export swift_processor_path()
- src/api/files.rs: Deduplicate legacy paths

Verification: cargo check --lib passes

Remaining: P3 (unwrap_or_else type, ~89 locations)

References:
- A-4.2 P2 in Momentry_Release_Requirements_20261004.md v1.1
- G-3 gate condition
2026-10-04 16:06:41 +08:00

182 lines
5.0 KiB
Rust

use anyhow::{Context, Result};
use std::path::PathBuf;
use std::process::Command;
use crate::core::config::{OUTPUT_DIR, PYTHON_PATH};
const PROXY_HEIGHT: u32 = 720;
const PROXY_CRF: u32 = 23;
const MIN_HEIGHT_FOR_PROXY: u32 = 1080;
pub fn needs_proxy(width: u32, height: u32) -> bool {
height >= MIN_HEIGHT_FOR_PROXY || width >= MIN_HEIGHT_FOR_PROXY
}
pub fn proxy_output_path(file_path: &str) -> Result<PathBuf> {
let src = std::path::Path::new(file_path);
let stem = src
.file_stem()
.context("Cannot get file stem")?
.to_string_lossy();
let parent = src.parent().context("Cannot get parent dir")?;
Ok(parent.join(format!("{}_720p.mp4", stem)))
}
fn ffmpeg_cmd() -> Command {
let ffmpeg_path = std::env::var("MOMENTRY_FFMPEG").unwrap_or_else(|_| "ffmpeg".to_string());
let mut cmd = Command::new(&ffmpeg_path);
let full_lib = "/opt/homebrew/opt/ffmpeg-full/lib";
if std::path::Path::new(full_lib).exists() {
cmd.env("DYLD_LIBRARY_PATH", full_lib);
}
cmd
}
pub async fn generate_proxy(file_path: &str, file_uuid: &str) -> Result<Option<String>> {
let src = std::path::Path::new(file_path);
if !src.exists() {
tracing::warn!("[PROXY] Source file not found: {}", file_path);
return Ok(None);
}
let out = proxy_output_path(file_path)?;
if out.exists() {
tracing::info!("[PROXY] Proxy already exists: {}", out.display());
return Ok(Some(out.to_string_lossy().to_string()));
}
let out_str = out.to_string_lossy().to_string();
let file_path_owned = file_path.to_string();
let status = tokio::task::spawn_blocking(move || {
ffmpeg_cmd()
.args([
"-i",
&file_path_owned,
"-vf",
&format!("scale=-2:{}", PROXY_HEIGHT),
"-c:v",
"libx264",
"-preset",
"fast",
"-crf",
&PROXY_CRF.to_string(),
"-c:a",
"aac",
"-b:a",
"128k",
"-movflags",
"+faststart",
"-y",
&out_str,
])
.status()
})
.await
.context("Spawn blocking failed")?
.context("Failed to spawn ffmpeg")?;
if !status.success() {
let _ = tokio::fs::remove_file(&out).await;
tracing::error!(
"[PROXY] ffmpeg failed for {} (exit: {:?})",
file_uuid,
status.code()
);
return Ok(None);
}
tracing::info!(
"[PROXY] Generated 720p proxy: {} -> {}",
file_uuid,
out.display()
);
Ok(Some(out.to_string_lossy().to_string()))
}
/// Audio track classification result
#[derive(Debug, Clone)]
pub struct AudioTrackResult {
pub classification: String,
pub speech_ratio: f64,
pub speech_segments: i32,
pub duration: f64,
}
/// Probe audio track and classify using VAD
pub async fn probe_audio_track(file_path: &str) -> Result<Option<AudioTrackResult>> {
let scripts_dir = crate::core::path_utils::scripts_dir();
let script = scripts_dir.join("audio_track_probe.py");
if !script.exists() {
tracing::warn!("[AUDIO_TRACK] Script not found: {}", script.display());
return Ok(None);
}
let file_path_owned = file_path.to_string();
let script_str = script.to_string_lossy().to_string();
let python_path = PYTHON_PATH.as_str().to_string();
let output = tokio::task::spawn_blocking(move || {
Command::new(&python_path)
.arg(&script_str)
.arg("--file")
.arg(&file_path_owned)
.arg("--json")
.output()
})
.await
.context("Spawn blocking failed")?
.context("Failed to spawn python")?;
if !output.status.success() {
tracing::warn!(
"[AUDIO_TRACK] Probe failed: {}",
String::from_utf8_lossy(&output.stderr)
);
return Ok(None);
}
let stdout = String::from_utf8_lossy(&output.stdout);
let json: serde_json::Value = match serde_json::from_str(&stdout) {
Ok(v) => v,
Err(e) => {
tracing::warn!("[AUDIO_TRACK] JSON parse error: {}", e);
return Ok(None);
}
};
let classification = json
.get("classification")
.and_then(|v| v.as_str())
.unwrap_or("speech_only")
.to_string();
let speech_ratio = json
.get("speech_ratio")
.and_then(|v| v.as_f64())
.unwrap_or(0.5);
let speech_segments = json
.get("speech_segments")
.and_then(|v| v.as_i64())
.unwrap_or(0) as i32;
let duration = json.get("duration").and_then(|v| v.as_f64()).unwrap_or(0.0);
tracing::info!(
"[AUDIO_TRACK] {} -> {} (ratio: {:.2}, segments: {})",
file_path,
classification,
speech_ratio,
speech_segments
);
Ok(Some(AudioTrackResult {
classification,
speech_ratio,
speech_segments,
duration,
}))
}