sreepathi-ravikumar commited on
Commit
42926fa
·
verified ·
1 Parent(s): 1194818

Update rust_highlight/src/lib.rs

Browse files
Files changed (1) hide show
  1. rust_highlight/src/lib.rs +72 -33
rust_highlight/src/lib.rs CHANGED
@@ -124,6 +124,37 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
124
  let mut frames_sent: usize = 0;
125
  let mut prev_visible_sub = String::new();
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  // PHASE 1: Animation phase (first 1/4 of duration)
128
  for &idx_in_full in visible_indices.iter() {
129
  let visible_sub = &full_text[0..=idx_in_full];
@@ -168,49 +199,57 @@ fn generate_video_clip(id: usize, text: String, audio_path: String, duration: f6
168
  pen_base_angle,
169
  pen_movement_amplitude,
170
  )?;
171
- stdin.write_all(frame_img.data_bytes().unwrap())
172
- .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write frame: {}", e)))?;
173
  frames_sent += 1;
 
 
 
 
 
 
 
174
  }
175
  prev_visible_sub = visible_sub.to_string();
176
  }
177
  }
178
 
 
 
 
 
 
 
 
179
  // PHASE 2: Static phase (remaining 3/4 of duration)
180
- // Render the complete text without pen, statically
181
  println!("Animation complete. Rendering {} static frames...", static_frames);
182
 
183
- let final_frame = render_frame(
184
- &full_text,
185
- -1, // pen_x = -1 means no pen
186
- 0,
187
- 0.0,
188
- width as i32,
189
- height as i32,
190
- &line_styles,
191
- &y_positions,
192
- margin_x,
193
- font,
194
- default_font_scale,
195
- header_font_scale,
196
- default_thickness,
197
- header_thickness,
198
- default_text_color,
199
- header_text_color,
200
- bg_color,
201
- pen_color,
202
- pen_tip_radius,
203
- pen_length,
204
- pen_thickness,
205
- pen_base_angle,
206
- pen_movement_amplitude,
207
- )?;
208
 
209
- let final_frame_bytes = final_frame.data_bytes().unwrap();
210
- for _ in 0..static_frames {
211
- stdin.write_all(final_frame_bytes)
212
- .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write static frame: {}", e)))?;
213
- frames_sent += 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  }
215
 
216
  drop(stdin);
 
124
  let mut frames_sent: usize = 0;
125
  let mut prev_visible_sub = String::new();
126
 
127
+ // Pre-render base frame with full text (optimization)
128
+ let base_frame = render_frame(
129
+ &full_text,
130
+ -1,
131
+ 0,
132
+ 0.0,
133
+ width as i32,
134
+ height as i32,
135
+ &line_styles,
136
+ &y_positions,
137
+ margin_x,
138
+ font,
139
+ default_font_scale,
140
+ header_font_scale,
141
+ default_thickness,
142
+ header_thickness,
143
+ default_text_color,
144
+ header_text_color,
145
+ bg_color,
146
+ pen_color,
147
+ pen_tip_radius,
148
+ pen_length,
149
+ pen_thickness,
150
+ pen_base_angle,
151
+ pen_movement_amplitude,
152
+ )?;
153
+
154
+ // Buffer for batch writing
155
+ let frame_size = (width * height * 3) as usize;
156
+ let mut buffer: Vec<u8> = Vec::with_capacity(frame_size * 100);
157
+
158
  // PHASE 1: Animation phase (first 1/4 of duration)
159
  for &idx_in_full in visible_indices.iter() {
160
  let visible_sub = &full_text[0..=idx_in_full];
 
199
  pen_base_angle,
200
  pen_movement_amplitude,
201
  )?;
202
+
203
+ buffer.extend_from_slice(frame_img.data_bytes().unwrap());
204
  frames_sent += 1;
205
+
206
+ // Flush buffer every 100 frames
207
+ if buffer.len() >= frame_size * 100 {
208
+ stdin.write_all(&buffer)
209
+ .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write frames: {}", e)))?;
210
+ buffer.clear();
211
+ }
212
  }
213
  prev_visible_sub = visible_sub.to_string();
214
  }
215
  }
216
 
217
+ // Flush remaining animation frames
218
+ if !buffer.is_empty() {
219
+ stdin.write_all(&buffer)
220
+ .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write frames: {}", e)))?;
221
+ buffer.clear();
222
+ }
223
+
224
  // PHASE 2: Static phase (remaining 3/4 of duration)
225
+ // Reuse the pre-rendered base frame
226
  println!("Animation complete. Rendering {} static frames...", static_frames);
227
 
228
+ let base_frame_bytes = base_frame.data_bytes().unwrap();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
+ // Write static frames in batches
231
+ let batch_size = 300;
232
+ let full_batches = static_frames / batch_size;
233
+ let remainder = static_frames % batch_size;
234
+
235
+ for _ in 0..full_batches {
236
+ for _ in 0..batch_size {
237
+ buffer.extend_from_slice(base_frame_bytes);
238
+ }
239
+ stdin.write_all(&buffer)
240
+ .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write static frames: {}", e)))?;
241
+ buffer.clear();
242
+ frames_sent += batch_size;
243
+ }
244
+
245
+ // Write remaining frames
246
+ if remainder > 0 {
247
+ for _ in 0..remainder {
248
+ buffer.extend_from_slice(base_frame_bytes);
249
+ }
250
+ stdin.write_all(&buffer)
251
+ .map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write static frames: {}", e)))?;
252
+ frames_sent += remainder;
253
  }
254
 
255
  drop(stdin);