Spaces:
Sleeping
Sleeping
Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +26 -22
rust_highlight/src/lib.rs
CHANGED
|
@@ -10,38 +10,42 @@ fn render_video(
|
|
| 10 |
words: Vec<(String, (u32, u32, u32, u32))>,
|
| 11 |
) -> PyResult<String> {
|
| 12 |
let clip = format!("clip{}.mp4", id);
|
| 13 |
-
let
|
| 14 |
|
|
|
|
| 15 |
let mut vf = String::new();
|
| 16 |
let n = words.len() as f64;
|
| 17 |
-
for (i, (_, (x, y, w, h))) in words.iter().enumerate() {
|
| 18 |
-
let word_duration = duration.parse::<f64>().unwrap() / n;
|
| 19 |
-
let start = (i as f64) * word_duration;
|
| 20 |
-
let end = start + word_duration;
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
vf.push_str(&format!(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
}
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
let status = Command::new("ffmpeg")
|
| 30 |
.args(&[
|
| 31 |
-
"-y",
|
| 32 |
-
"-loop", "1",
|
| 33 |
-
"-i", image_path,
|
| 34 |
-
"-i", audio_path,
|
| 35 |
-
"-vf", &
|
| 36 |
-
"-t", &
|
| 37 |
-
"-c:v", "libx264",
|
| 38 |
-
"-pix_fmt", "yuv420p",
|
| 39 |
-
"-c:a", "aac",
|
| 40 |
-
&clip,
|
| 41 |
])
|
| 42 |
.status()
|
| 43 |
-
.expect("ffmpeg
|
| 44 |
|
|
|
|
| 45 |
if status.success() {
|
| 46 |
Ok(clip)
|
| 47 |
} else {
|
|
@@ -52,7 +56,7 @@ fn render_video(
|
|
| 52 |
}
|
| 53 |
|
| 54 |
#[pymodule]
|
| 55 |
-
fn rust_highlight(
|
| 56 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
| 57 |
Ok(())
|
| 58 |
}
|
|
|
|
| 10 |
words: Vec<(String, (u32, u32, u32, u32))>,
|
| 11 |
) -> PyResult<String> {
|
| 12 |
let clip = format!("clip{}.mp4", id);
|
| 13 |
+
let duration_str = duration.to_string();
|
| 14 |
|
| 15 |
+
// --- FFmpeg drawbox filters for each word ---
|
| 16 |
let mut vf = String::new();
|
| 17 |
let n = words.len() as f64;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
for (i, (_, (x, y, w, h))) in words.iter().enumerate() {
|
| 20 |
+
let start = (i as f64) * (duration / n);
|
| 21 |
+
let end = start + (duration / n);
|
| 22 |
vf.push_str(&format!(
|
| 23 |
+
"drawbox=x={}:y={}:w={}:h={}:color=yellow@0.5:enable='between(t,{:.3},{:.3})',",
|
| 24 |
+
x, y, w, h, start, end
|
| 25 |
+
));
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// Trim trailing comma and add pixel format fix
|
| 29 |
+
let vf_with_format = format!("{},format=yuv420p", vf.trim_end_matches(','));
|
| 30 |
|
| 31 |
+
// --- FFmpeg command ---
|
| 32 |
let status = Command::new("ffmpeg")
|
| 33 |
.args(&[
|
| 34 |
+
"-y", // Overwrite output
|
| 35 |
+
"-loop", "1", // Loop image
|
| 36 |
+
"-i", image_path, // Input image
|
| 37 |
+
"-i", audio_path, // Input audio
|
| 38 |
+
"-vf", &vf_with_format, // Video filter with highlights
|
| 39 |
+
"-t", &duration_str, // Duration
|
| 40 |
+
"-c:v", "libx264", // Video codec
|
| 41 |
+
"-pix_fmt", "yuv420p", // Pixel format (for compatibility)
|
| 42 |
+
"-c:a", "aac", // Audio codec
|
| 43 |
+
&clip, // Output file
|
| 44 |
])
|
| 45 |
.status()
|
| 46 |
+
.expect("Failed to run ffmpeg");
|
| 47 |
|
| 48 |
+
// Return result
|
| 49 |
if status.success() {
|
| 50 |
Ok(clip)
|
| 51 |
} else {
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
#[pymodule]
|
| 59 |
+
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
| 60 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
| 61 |
Ok(())
|
| 62 |
}
|