sreepathi-ravikumar commited on
Commit
9c7c1c2
·
verified ·
1 Parent(s): caea4ef

Update rust_highlight/src/lib.rs

Browse files
Files changed (1) hide show
  1. rust_highlight/src/lib.rs +23 -23
rust_highlight/src/lib.rs CHANGED
@@ -161,7 +161,27 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
161
 
162
  println!("Total animation frames to render: {}", frame_specs.len());
163
 
164
- // Parallel rendering using rayon
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  let animation_frames: Vec<Vec<u8>> = frame_specs
166
  .par_iter()
167
  .map(|(visible_sub, pen_x, pen_y, anim_step)| {
@@ -197,30 +217,10 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
197
 
198
  println!("Animation frames rendered in {:.3}s", t0.elapsed().as_secs_f64());
199
 
200
- // STEP 3: Write animation frames to FFmpeg
201
- let mut child = Command::new("ffmpeg")
202
- .arg("-y")
203
- .arg("-f").arg("rawvideo")
204
- .arg("-pix_fmt").arg("bgr24")
205
- .arg("-s").arg(format!("{}x{}", width, height))
206
- .arg("-r").arg(fps.to_string())
207
- .arg("-i").arg("-")
208
- .arg("-an")
209
- .arg("-c:v").arg("libx264")
210
- .arg("-preset").arg(ffmpeg_preset)
211
- .arg("-crf").arg(crf)
212
- .arg("-pix_fmt").arg("yuv420p")
213
- .arg(&animation_video_path)
214
- .stdin(Stdio::piped())
215
- .spawn()
216
- .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to spawn FFmpeg: {}", e)))?;
217
-
218
- let mut stdin = child.stdin.take().unwrap();
219
-
220
  // Write all animation frames in one large batch
221
  let mut buffer: Vec<u8> = Vec::with_capacity(animation_frames.len() * width as usize * height as usize * 3);
222
- for frame_data in animation_frames {
223
- buffer.extend_from_slice(&frame_data);
224
  }
225
 
226
  stdin.write_all(&buffer)
 
161
 
162
  println!("Total animation frames to render: {}", frame_specs.len());
163
 
164
+ // STEP 3: Start FFmpeg process first
165
+ let mut child = Command::new("ffmpeg")
166
+ .arg("-y")
167
+ .arg("-f").arg("rawvideo")
168
+ .arg("-pix_fmt").arg("bgr24")
169
+ .arg("-s").arg(format!("{}x{}", width, height))
170
+ .arg("-r").arg(fps.to_string())
171
+ .arg("-i").arg("-")
172
+ .arg("-an")
173
+ .arg("-c:v").arg("libx264")
174
+ .arg("-preset").arg(ffmpeg_preset)
175
+ .arg("-crf").arg(crf)
176
+ .arg("-pix_fmt").arg("yuv420p")
177
+ .arg(&animation_video_path)
178
+ .stdin(Stdio::piped())
179
+ .spawn()
180
+ .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to spawn FFmpeg: {}", e)))?;
181
+
182
+ let mut stdin = child.stdin.take().unwrap();
183
+
184
+ // Parallel rendering using rayon and streaming to FFmpeg
185
  let animation_frames: Vec<Vec<u8>> = frame_specs
186
  .par_iter()
187
  .map(|(visible_sub, pen_x, pen_y, anim_step)| {
 
217
 
218
  println!("Animation frames rendered in {:.3}s", t0.elapsed().as_secs_f64());
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  // Write all animation frames in one large batch
221
  let mut buffer: Vec<u8> = Vec::with_capacity(animation_frames.len() * width as usize * height as usize * 3);
222
+ for frame_data in &animation_frames {
223
+ buffer.extend_from_slice(frame_data);
224
  }
225
 
226
  stdin.write_all(&buffer)