Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +26 -21
rust_highlight/src/lib.rs
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
use pyo3::prelude::*;
|
2 |
-
use
|
|
|
|
|
3 |
use std::path::Path;
|
4 |
-
use
|
5 |
-
use std::fs::File;
|
6 |
-
use std::io::Write;
|
7 |
use uuid::Uuid;
|
8 |
|
|
|
9 |
#[pyfunction]
|
10 |
fn render_video(
|
11 |
id: usize,
|
@@ -62,7 +63,7 @@ fn render_video(
|
|
62 |
"-c:a", "aac",
|
63 |
"-b:a", "192k",
|
64 |
"-r", "60",
|
65 |
-
"-fps_mode", "vfr",
|
66 |
"-t", &duration_str,
|
67 |
&output_path,
|
68 |
])
|
@@ -80,23 +81,24 @@ 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(&[
|
@@ -106,22 +108,25 @@ fn concat_videos(clips: Vec<&str>) -> PyResult<String> {
|
|
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 |
-
m.add_function(wrap_pyfunction!(combine_clips, m)?)?;
|
125 |
Ok(())
|
126 |
}
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
1 |
use pyo3::prelude::*;
|
2 |
+
use pyo3::wrap_pyfunction;
|
3 |
+
use std::fs::File;
|
4 |
+
use std::io::Write;
|
5 |
use std::path::Path;
|
6 |
+
use std::process::Command;
|
|
|
|
|
7 |
use uuid::Uuid;
|
8 |
|
9 |
+
/// Renders a video by highlighting regions over time
|
10 |
#[pyfunction]
|
11 |
fn render_video(
|
12 |
id: usize,
|
|
|
63 |
"-c:a", "aac",
|
64 |
"-b:a", "192k",
|
65 |
"-r", "60",
|
66 |
+
"-fps_mode", "vfr",
|
67 |
"-t", &duration_str,
|
68 |
&output_path,
|
69 |
])
|
|
|
81 |
}
|
82 |
}
|
83 |
|
84 |
+
/// Combines multiple video clips into one
|
85 |
+
#[pyfunction]
|
86 |
+
fn combine_clips(clips: Vec<String>) -> PyResult<String> {
|
87 |
+
let paths: Vec<&str> = clips.iter().map(|s| s.as_str()).collect();
|
88 |
+
concat_videos(paths)
|
89 |
}
|
90 |
|
91 |
+
fn concat_videos(clips: Vec<&str>) -> PyResult<String> {
|
92 |
+
// Step 1: Write file list
|
93 |
+
let list_file = format!("/tmp/clips_{}.txt", Uuid::new_v4());
|
94 |
let mut file = File::create(&list_file)
|
95 |
+
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;
|
96 |
for clip in &clips {
|
97 |
writeln!(file, "file '{}'", clip)
|
98 |
+
.map_err(|e| pyo3::exceptions::PyIOError::new_err(e.to_string()))?;
|
99 |
+
}
|
100 |
|
101 |
+
// Step 2: Run FFmpeg
|
102 |
let output_file = format!("/tmp/final_video_{}.mp4", Uuid::new_v4());
|
103 |
let status = Command::new("ffmpeg")
|
104 |
.args(&[
|
|
|
108 |
"-i", &list_file,
|
109 |
"-c", "copy",
|
110 |
&output_file,
|
111 |
+
])
|
112 |
.status()
|
113 |
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
|
114 |
+
|
115 |
if status.success() {
|
116 |
Ok(output_file)
|
117 |
} else {
|
118 |
Err(pyo3::exceptions::PyRuntimeError::new_err("FFmpeg command failed"))
|
119 |
}
|
120 |
+
}
|
|
|
121 |
|
122 |
#[pymodule]
|
123 |
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
124 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
|
|
125 |
Ok(())
|
126 |
}
|
127 |
|
128 |
+
#[pymodule]
|
129 |
+
fn rust_combiner(_py: Python, m: &PyModule) -> PyResult<()> {
|
130 |
+
m.add_function(wrap_pyfunction!(combine_clips, m)?)?;
|
131 |
+
Ok(())
|
132 |
+
}
|