sreepathi-ravikumar commited on
Commit
04a1f81
·
verified ·
1 Parent(s): fbb769b

Update rust_highlight/src/lib.rs

Browse files
Files changed (1) hide show
  1. 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
- pub fn render_video(
 
5
  id: usize,
6
- image_path: &str,
7
- audio_path: &str,
8
  duration: f64,
9
  words: Vec<(String, (u32, u32, u32, u32))>,
10
- output_dir: &str,
11
- ) -> Result<String, String> {
12
  // Validate paths
13
- if !Path::new(image_path).exists() {
14
- return Err(format!("Image not found: {}", image_path));
 
 
 
15
  }
16
- if !Path::new(audio_path).exists() {
17
- return Err(format!("Audio not found: {}", audio_path));
 
 
 
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 with optimized settings
52
  let status = Command::new("ffmpeg")
53
  .args(&[
54
- "-y", // Overwrite output
55
- "-hwaccel", "auto", // Enable hardware acceleration
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", // Balance between speed and compression
64
- "-crf", "18", // High quality (lower = better quality)
65
  "-pix_fmt", "yuv420p",
66
- "-movflags", "+faststart", // For web streaming
67
  "-c:a", "aac",
68
- "-b:a", "192k", // Higher audio bitrate
69
- "-r", "60", // Higher framerate for smoother animation
70
  "-t", &duration_str,
71
- "-vsync", "2", // Frame rate conversion method
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".to_string()),
79
- Err(e) => Err(format!("Failed to execute FFmpeg: {}", e)),
 
 
 
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)?)?;