Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +48 -0
rust_highlight/src/lib.rs
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
use pyo3::prelude::*;
|
2 |
use std::process::Command;
|
3 |
use std::path::Path;
|
|
|
|
|
|
|
|
|
4 |
|
5 |
#[pyfunction]
|
6 |
fn render_video(
|
@@ -76,8 +80,52 @@ fn render_video(
|
|
76 |
}
|
77 |
}
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
#[pymodule]
|
80 |
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
81 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
82 |
Ok(())
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
}
|
|
|
1 |
use pyo3::prelude::*;
|
2 |
use std::process::Command;
|
3 |
use std::path::Path;
|
4 |
+
use pyo3::wrap_pyfunction;
|
5 |
+
use std::fs::File;
|
6 |
+
use std::io::Write;
|
7 |
+
use uuid::Uuid;
|
8 |
|
9 |
#[pyfunction]
|
10 |
fn render_video(
|
|
|
80 |
}
|
81 |
}
|
82 |
|
83 |
+
|
84 |
+
#[pyfunction]
|
85 |
+
fn combine_clips(clips: Vec<String>) -> PyResult<String> {
|
86 |
+
let paths: Vec<&str> = clips.iter().map(|s| s.as_str()).collect();
|
87 |
+
concat_videos(paths)
|
88 |
+
}
|
89 |
+
|
90 |
+
fn concat_videos(clips: Vec<&str>) -> PyResult<String> {
|
91 |
+
// Step 1: Write file list
|
92 |
+
let list_file = format!("/tmp/clips_{}.txt", Uuid::new_v4());
|
93 |
+
let mut file = File::create(&list_file)
|
94 |
+
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;
|
95 |
+
for clip in &clips {
|
96 |
+
writeln!(file, "file '{}'", clip)
|
97 |
+
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?; }
|
98 |
+
|
99 |
+
// Step 2: Run FFmpeg
|
100 |
+
let output_file = format!("/tmp/final_video_{}.mp4", Uuid::new_v4());
|
101 |
+
let status = Command::new("ffmpeg")
|
102 |
+
.args(&[
|
103 |
+
"-y",
|
104 |
+
"-f", "concat",
|
105 |
+
"-safe", "0",
|
106 |
+
"-i", &list_file,
|
107 |
+
"-c", "copy",
|
108 |
+
&output_file,
|
109 |
+
])
|
110 |
+
.status()
|
111 |
+
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
|
112 |
+
|
113 |
+
if status.success() {
|
114 |
+
Ok(output_file)
|
115 |
+
} else {
|
116 |
+
Err(pyo3::exceptions::PyRuntimeError::new_err("FFmpeg command failed"))
|
117 |
+
}
|
118 |
+
|
119 |
+
}
|
120 |
+
|
121 |
#[pymodule]
|
122 |
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
123 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
124 |
Ok(())
|
125 |
+
}
|
126 |
+
|
127 |
+
#[pymodule]
|
128 |
+
fn rust_combiner(_py: Python, m: &PyModule) -> PyResult<()> {
|
129 |
+
m.add_function(wrap_pyfunction!(combine_clips, m)?)?;
|
130 |
+
Ok(())
|
131 |
}
|