Spaces:
Sleeping
Sleeping
Update rust_highlight/src/lib.rs
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556use pyo3::prelude::*;use std::process::Command;#[pyfunction]fn render_video(id: usize,image_path: &str,audio_path: &str,duration: f64,words: Vec<(String, (u32, u32, u32, u32))>,) -> PyResult<String> {let clip = format!("clip{}.mp4", id);let duration = duration.to_string();let mut vf = String::new();let n = words.len() as f64;for (i, (_, (x, y, w, h))) in words.iter().enumerate() {let start = (i as f64) * (duration.parse::<f64>().unwrap() / n);let end = start + (duration.parse::<f64>().unwrap() / n);vf.push_str(&format!("drawbox=x={}:y={}:w={}:h={}:color=yellow@0.5:enable='between(t,{:.3},{:.3})',",x, y, w, h, start, end));}vf = vf.trim_end_matches(',').to_string();let status = Command::new("ffmpeg").args(&["-y","-loop", "1","-i", image_path,"-i", audio_path,"-vf", &vf,"-t", &duration,"-c:v", "libx264","-pix_fmt", "yuv420p","-c:a", "aac",&clip,]).status().expect("ffmpeg failed");if status.success() {Ok(clip)} else {Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>("FFmpeg failed",))}}#[pymodule]fn rust_highlight(py: Python, m: &PyModule) -> PyResult<()> {m.add_function(wrap_pyfunction!(render_video, m)?)?;Ok(())}123456789101112use pyo3::prelude::*;#[pyfunction]fn rust_hello() -> PyResult<String> {Ok("Hello from Rust!".to_string())}#[pymodule]fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {m.add_function(wrap_pyfunction!(rust_hello, m)?)?;Ok(())}
rust_highlight/src/lib.rs+11-55