sreepathi-ravikumar commited on
Commit
213a727
·
verified ·
1 Parent(s): aa5fa1d

Upload 2 files

Browse files
rust_highlight/Cargo.toml ADDED
File without changes
rust_highlight/src/lib.rs ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use pyo3::prelude::*;
2
+ use std::process::Command;
3
+
4
+ #[pyfunction]
5
+ fn render_video(
6
+ id: usize,
7
+ image_path: &str,
8
+ audio_path: &str,
9
+ duration: f64,
10
+ words: Vec<(String, (u32, u32, u32, u32))>,
11
+ ) -> PyResult<String> {
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
+ }