Spaces:
Running
Running
Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +11 -55
rust_highlight/src/lib.rs
CHANGED
@@ -1,56 +1,12 @@
|
|
1 |
-
use pyo3::prelude::*;
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
)
|
12 |
-
let clip = format!("clip{}.mp4", id);
|
13 |
-
let duration = duration.to_string();
|
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 start = (i as f64) * (duration.parse::<f64>().unwrap() / n);
|
19 |
-
let end = start + (duration.parse::<f64>().unwrap() / n);
|
20 |
-
vf.push_str(&format!(
|
21 |
-
"drawbox=x={}:y={}:w={}:h={}:[email protected]:enable='between(t,{:.3},{:.3})',",
|
22 |
-
x, y, w, h, start, end
|
23 |
-
));
|
24 |
-
}
|
25 |
-
vf = vf.trim_end_matches(',').to_string();
|
26 |
-
|
27 |
-
let status = Command::new("ffmpeg")
|
28 |
-
.args(&[
|
29 |
-
"-y",
|
30 |
-
"-loop", "1",
|
31 |
-
"-i", image_path,
|
32 |
-
"-i", audio_path,
|
33 |
-
"-vf", &vf,
|
34 |
-
"-t", &duration,
|
35 |
-
"-c:v", "libx264",
|
36 |
-
"-pix_fmt", "yuv420p",
|
37 |
-
"-c:a", "aac",
|
38 |
-
&clip,
|
39 |
-
])
|
40 |
-
.status()
|
41 |
-
.expect("ffmpeg failed");
|
42 |
-
|
43 |
-
if status.success() {
|
44 |
-
Ok(clip)
|
45 |
-
} else {
|
46 |
-
Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
|
47 |
-
"FFmpeg failed",
|
48 |
-
))
|
49 |
-
}
|
50 |
-
}
|
51 |
-
|
52 |
-
#[pymodule]
|
53 |
-
fn rust_highlight(py: Python, m: &PyModule) -> PyResult<()> {
|
54 |
-
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
55 |
-
Ok(())
|
56 |
}
|
|
|
1 |
+
use pyo3::prelude::*;
|
2 |
+
|
3 |
+
#[pyfunction]
|
4 |
+
fn rust_hello() -> PyResult<String> {
|
5 |
+
Ok("Hello from Rust!".to_string())
|
6 |
+
}
|
7 |
+
|
8 |
+
#[pymodule]
|
9 |
+
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
10 |
+
m.add_function(wrap_pyfunction!(rust_hello, m)?)?;
|
11 |
+
Ok(())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|