Skip to content

Commit 32d96df

Browse files
committed
don't panic when whisper-rs full_get_segment_text() fails
1 parent 9f3ac14 commit 32d96df

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

src/filter/imp.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -132,41 +132,43 @@ impl WhisperFilter {
132132
if n_segments > 0 {
133133
assert!(n_segments == 1);
134134

135-
let segment = state
136-
.whisper_state
137-
.full_get_segment_text(0)
138-
.unwrap()
139-
.replace("[BLANK_AUDIO]", "")
140-
.replace("[ Silence ]", "")
141-
.replace("[silence]", "")
142-
.replace("(silence)", "")
143-
.replace("[ Pause ]", "")
144-
.trim()
145-
.to_owned();
146-
147-
if !segment.is_empty() {
148-
let start_ts = state.whisper_state.full_get_segment_t0(0).unwrap();
149-
let end_ts = state.whisper_state.full_get_segment_t1(0).unwrap();
150-
151-
gstreamer::info!(CAT, "{}", segment);
152-
153-
let segment = format!("{}\n", segment);
154-
let mut buffer = Buffer::with_size(segment.len()).map_err(|_| FlowError::Error)?;
155-
let buffer_mut = buffer.get_mut().ok_or(FlowError::Error)?;
156-
buffer_mut.set_pts(
157-
chunk
158-
.start_pts
159-
.checked_add(ClockTime::from_mseconds(start_ts as u64 * 10))
160-
.unwrap(),
161-
);
162-
buffer_mut.set_duration(ClockTime::from_mseconds(
163-
(end_ts as u64 - start_ts as u64) * 10,
164-
));
165-
buffer_mut
166-
.copy_from_slice(0, segment.as_bytes())
167-
.map_err(|_| FlowError::Error)?;
168-
169-
Ok(Some(buffer))
135+
if let Ok(segment) = state.whisper_state.full_get_segment_text(0) {
136+
let segment = segment
137+
.replace("[BLANK_AUDIO]", "")
138+
.replace("[ Silence ]", "")
139+
.replace("[silence]", "")
140+
.replace("(silence)", "")
141+
.replace("[ Pause ]", "")
142+
.trim()
143+
.to_owned();
144+
145+
if !segment.is_empty() {
146+
let start_ts = state.whisper_state.full_get_segment_t0(0).unwrap();
147+
let end_ts = state.whisper_state.full_get_segment_t1(0).unwrap();
148+
149+
gstreamer::info!(CAT, "{}", segment);
150+
151+
let segment = format!("{}\n", segment);
152+
let mut buffer = Buffer::with_size(segment.len()).map_err(|_| FlowError::Error)?;
153+
let buffer_mut = buffer.get_mut().ok_or(FlowError::Error)?;
154+
buffer_mut.set_pts(
155+
chunk
156+
.start_pts
157+
.checked_add(ClockTime::from_mseconds(start_ts as u64 * 10))
158+
.unwrap(),
159+
);
160+
buffer_mut.set_duration(ClockTime::from_mseconds(
161+
(end_ts as u64 - start_ts as u64) * 10,
162+
));
163+
buffer_mut
164+
.copy_from_slice(0, segment.as_bytes())
165+
.map_err(|_| FlowError::Error)?;
166+
167+
Ok(Some(buffer))
168+
}
169+
else {
170+
Ok(None)
171+
}
170172
}
171173
else {
172174
Ok(None)

0 commit comments

Comments
 (0)