Update rust_highlight/src/lib.rs
Browse files- rust_highlight/src/lib.rs +33 -24
rust_highlight/src/lib.rs
CHANGED
@@ -1,20 +1,28 @@
|
|
|
|
1 |
use std::process::Command;
|
2 |
use std::path::Path;
|
3 |
|
4 |
-
|
|
|
5 |
id: usize,
|
6 |
-
image_path:
|
7 |
-
audio_path:
|
8 |
duration: f64,
|
9 |
words: Vec<(String, (u32, u32, u32, u32))>,
|
10 |
-
output_dir:
|
11 |
-
) ->
|
12 |
// Validate paths
|
13 |
-
if !Path::new(image_path).exists() {
|
14 |
-
return Err(format!(
|
|
|
|
|
|
|
15 |
}
|
16 |
-
if !Path::new(audio_path).exists() {
|
17 |
-
return Err(format!(
|
|
|
|
|
|
|
18 |
}
|
19 |
|
20 |
let output_path = format!("{}/clip{}.mp4", output_dir, id);
|
@@ -30,7 +38,6 @@ pub fn render_video(
|
|
30 |
let start = i as f64 * highlight_duration;
|
31 |
let end = start + highlight_duration;
|
32 |
|
33 |
-
// Create highlight box
|
34 |
filter_complex.push_str(&format!(
|
35 |
"[{last_out}]drawbox=x={x}:y={y}:w={w}:h={h}:[email protected]:t=fill:enable='between(t,{start:.3},{end:.3})'[v{i}];",
|
36 |
last_out = last_output,
|
@@ -48,39 +55,41 @@ pub fn render_video(
|
|
48 |
// Remove trailing semicolon
|
49 |
filter_complex.pop();
|
50 |
|
51 |
-
// FFmpeg command
|
52 |
let status = Command::new("ffmpeg")
|
53 |
.args(&[
|
54 |
-
"-y",
|
55 |
-
"-hwaccel", "auto",
|
56 |
"-loop", "1",
|
57 |
-
"-i", image_path,
|
58 |
-
"-i", audio_path,
|
59 |
"-filter_complex", &filter_complex,
|
60 |
"-map", &last_output,
|
61 |
"-map", "1:a",
|
62 |
"-c:v", "libx264",
|
63 |
-
"-preset", "fast",
|
64 |
-
"-crf", "18",
|
65 |
"-pix_fmt", "yuv420p",
|
66 |
-
"-movflags", "+faststart",
|
67 |
"-c:a", "aac",
|
68 |
-
"-b:a", "192k",
|
69 |
-
"-r", "60",
|
70 |
"-t", &duration_str,
|
71 |
-
"-vsync", "2",
|
72 |
&output_path,
|
73 |
])
|
74 |
.status();
|
75 |
|
76 |
match status {
|
77 |
Ok(exit_status) if exit_status.success() => Ok(output_path),
|
78 |
-
Ok(_) => Err("FFmpeg command failed"
|
79 |
-
Err(e) => Err(format!(
|
|
|
|
|
|
|
80 |
}
|
81 |
}
|
82 |
|
83 |
-
|
84 |
#[pymodule]
|
85 |
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
86 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|
|
|
1 |
+
use pyo3::prelude::*;
|
2 |
use std::process::Command;
|
3 |
use std::path::Path;
|
4 |
|
5 |
+
#[pyfunction]
|
6 |
+
fn render_video(
|
7 |
id: usize,
|
8 |
+
image_path: String,
|
9 |
+
audio_path: String,
|
10 |
duration: f64,
|
11 |
words: Vec<(String, (u32, u32, u32, u32))>,
|
12 |
+
output_dir: String,
|
13 |
+
) -> PyResult<String> {
|
14 |
// Validate paths
|
15 |
+
if !Path::new(&image_path).exists() {
|
16 |
+
return Err(pyo3::exceptions::PyFileNotFoundError::new_err(format!(
|
17 |
+
"Image not found: {}",
|
18 |
+
image_path
|
19 |
+
)));
|
20 |
}
|
21 |
+
if !Path::new(&audio_path).exists() {
|
22 |
+
return Err(pyo3::exceptions::PyFileNotFoundError::new_err(format!(
|
23 |
+
"Audio not found: {}",
|
24 |
+
audio_path
|
25 |
+
)));
|
26 |
}
|
27 |
|
28 |
let output_path = format!("{}/clip{}.mp4", output_dir, id);
|
|
|
38 |
let start = i as f64 * highlight_duration;
|
39 |
let end = start + highlight_duration;
|
40 |
|
|
|
41 |
filter_complex.push_str(&format!(
|
42 |
"[{last_out}]drawbox=x={x}:y={y}:w={w}:h={h}:[email protected]:t=fill:enable='between(t,{start:.3},{end:.3})'[v{i}];",
|
43 |
last_out = last_output,
|
|
|
55 |
// Remove trailing semicolon
|
56 |
filter_complex.pop();
|
57 |
|
58 |
+
// FFmpeg command
|
59 |
let status = Command::new("ffmpeg")
|
60 |
.args(&[
|
61 |
+
"-y",
|
62 |
+
"-hwaccel", "auto",
|
63 |
"-loop", "1",
|
64 |
+
"-i", &image_path,
|
65 |
+
"-i", &audio_path,
|
66 |
"-filter_complex", &filter_complex,
|
67 |
"-map", &last_output,
|
68 |
"-map", "1:a",
|
69 |
"-c:v", "libx264",
|
70 |
+
"-preset", "fast",
|
71 |
+
"-crf", "18",
|
72 |
"-pix_fmt", "yuv420p",
|
73 |
+
"-movflags", "+faststart",
|
74 |
"-c:a", "aac",
|
75 |
+
"-b:a", "192k",
|
76 |
+
"-r", "60",
|
77 |
"-t", &duration_str,
|
78 |
+
"-vsync", "2",
|
79 |
&output_path,
|
80 |
])
|
81 |
.status();
|
82 |
|
83 |
match status {
|
84 |
Ok(exit_status) if exit_status.success() => Ok(output_path),
|
85 |
+
Ok(_) => Err(pyo3::exceptions::PyRuntimeError::new_err("FFmpeg command failed")),
|
86 |
+
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
|
87 |
+
"Failed to execute FFmpeg: {}",
|
88 |
+
e
|
89 |
+
))),
|
90 |
}
|
91 |
}
|
92 |
|
|
|
93 |
#[pymodule]
|
94 |
fn rust_highlight(_py: Python, m: &PyModule) -> PyResult<()> {
|
95 |
m.add_function(wrap_pyfunction!(render_video, m)?)?;
|