Spaces:
Sleeping
Sleeping
Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +10 -24
rust_highlight/src/lib.rs
CHANGED
|
@@ -30,42 +30,26 @@ fn render_video(
|
|
| 30 |
let n_words = words.len() as f64;
|
| 31 |
let highlight_duration = duration / n_words;
|
| 32 |
|
| 33 |
-
// Build
|
| 34 |
-
let mut
|
| 35 |
-
let mut last_output = "0:v".to_string();
|
| 36 |
-
|
| 37 |
for (i, (_, (x, y, w, h))) in words.iter().enumerate() {
|
| 38 |
let start = i as f64 * highlight_duration;
|
| 39 |
let end = start + highlight_duration;
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"[{last_out}]drawbox=x={x}:y={y}:w={w}:h={h}:color=yellow@0.5:t=fill:enable='between(t,{start:.3},{end:.3})'[v{i}];",
|
| 43 |
-
last_out = last_output,
|
| 44 |
-
x = x,
|
| 45 |
-
y = y,
|
| 46 |
-
w = w,
|
| 47 |
-
h = h,
|
| 48 |
-
start = start,
|
| 49 |
-
end = end,
|
| 50 |
-
i = i
|
| 51 |
));
|
| 52 |
-
last_output = format!("[v{i}]");
|
| 53 |
}
|
| 54 |
|
| 55 |
-
|
| 56 |
-
filter_complex.pop();
|
| 57 |
|
| 58 |
// FFmpeg command
|
| 59 |
let status = Command::new("ffmpeg")
|
| 60 |
.args(&[
|
| 61 |
"-y",
|
| 62 |
-
"-hwaccel", "auto",
|
| 63 |
"-loop", "1",
|
| 64 |
"-i", &image_path,
|
| 65 |
"-i", &audio_path,
|
| 66 |
-
"-
|
| 67 |
-
"-map", &last_output,
|
| 68 |
-
"-map", "1:a",
|
| 69 |
"-c:v", "libx264",
|
| 70 |
"-preset", "fast",
|
| 71 |
"-crf", "18",
|
|
@@ -74,15 +58,17 @@ fn render_video(
|
|
| 74 |
"-c:a", "aac",
|
| 75 |
"-b:a", "192k",
|
| 76 |
"-r", "60",
|
|
|
|
| 77 |
"-t", &duration_str,
|
| 78 |
-
"-vsync", "2",
|
| 79 |
&output_path,
|
| 80 |
])
|
| 81 |
.status();
|
| 82 |
|
| 83 |
match status {
|
| 84 |
Ok(exit_status) if exit_status.success() => Ok(output_path),
|
| 85 |
-
Ok(_) => Err(pyo3::exceptions::PyRuntimeError::new_err(
|
|
|
|
|
|
|
| 86 |
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
|
| 87 |
"Failed to execute FFmpeg: {}",
|
| 88 |
e
|
|
|
|
| 30 |
let n_words = words.len() as f64;
|
| 31 |
let highlight_duration = duration / n_words;
|
| 32 |
|
| 33 |
+
// Build filter graph
|
| 34 |
+
let mut filters = vec![];
|
|
|
|
|
|
|
| 35 |
for (i, (_, (x, y, w, h))) in words.iter().enumerate() {
|
| 36 |
let start = i as f64 * highlight_duration;
|
| 37 |
let end = start + highlight_duration;
|
| 38 |
+
filters.push(format!(
|
| 39 |
+
"drawbox=x={x}:y={y}:w={w}:h={h}:color=yellow@0.5:t=fill:enable='between(t,{start:.2},{end:.2})'"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
));
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
+
let filter_chain = filters.join(",");
|
|
|
|
| 44 |
|
| 45 |
// FFmpeg command
|
| 46 |
let status = Command::new("ffmpeg")
|
| 47 |
.args(&[
|
| 48 |
"-y",
|
|
|
|
| 49 |
"-loop", "1",
|
| 50 |
"-i", &image_path,
|
| 51 |
"-i", &audio_path,
|
| 52 |
+
"-vf", &filter_chain,
|
|
|
|
|
|
|
| 53 |
"-c:v", "libx264",
|
| 54 |
"-preset", "fast",
|
| 55 |
"-crf", "18",
|
|
|
|
| 58 |
"-c:a", "aac",
|
| 59 |
"-b:a", "192k",
|
| 60 |
"-r", "60",
|
| 61 |
+
"-fps_mode", "vfr", // Replaces deprecated -vsync
|
| 62 |
"-t", &duration_str,
|
|
|
|
| 63 |
&output_path,
|
| 64 |
])
|
| 65 |
.status();
|
| 66 |
|
| 67 |
match status {
|
| 68 |
Ok(exit_status) if exit_status.success() => Ok(output_path),
|
| 69 |
+
Ok(_) => Err(pyo3::exceptions::PyRuntimeError::new_err(
|
| 70 |
+
"FFmpeg command failed. Check filter syntax.",
|
| 71 |
+
)),
|
| 72 |
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
|
| 73 |
"Failed to execute FFmpeg: {}",
|
| 74 |
e
|