transcribe-cli 0.0.4

Whisper CLI transcription pipeline on CTranslate2 with CPU and optional CUDA support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::time::Duration;

use anyhow::{Context, Result, bail};
use reqwest::Client;
use symphonia::core::audio::SampleBuffer;
use symphonia::core::codecs::{CODEC_TYPE_NULL, CodecParameters, DecoderOptions};
use symphonia::core::errors::Error as SymphoniaError;
use symphonia::core::formats::{FormatOptions, FormatReader};
use symphonia::core::io::MediaSourceStream;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
use tempfile::{Builder, NamedTempFile};
use tokio::fs;
use url::Url;

use crate::video::is_video_file;

pub struct PreparedAudio {
    pub display_name: String,
    pub metadata: AudioMetadata,
    pub samples: Vec<f32>,
    _temp_file: Option<NamedTempFile>,
}

#[derive(Debug, Clone)]
pub struct AudioMetadata {
    pub source_sample_rate: Option<u32>,
    pub target_sample_rate: u32,
    pub channels: Option<u16>,
    pub duration: Option<Duration>,
    pub codec: String,
}

pub async fn prepare_audio_source(input: &str) -> Result<PreparedAudio> {
    prepare_audio_source_for_rate(input, 16_000).await
}

pub async fn prepare_audio_source_for_rate(
    input: &str,
    target_sample_rate: u32,
) -> Result<PreparedAudio> {
    if let Ok(url) = Url::parse(input) {
        if matches!(url.scheme(), "http" | "https") {
            return download_remote_audio(url, target_sample_rate).await;
        }
    }

    let path = resolve_local_audio_path(input).await?;
    let (metadata, samples) = inspect_audio_file(&path, target_sample_rate)?;

    Ok(PreparedAudio {
        display_name: path.display().to_string(),
        metadata,
        samples,
        _temp_file: None,
    })
}

async fn resolve_local_audio_path(input: &str) -> Result<std::path::PathBuf> {
    let requested_path = Path::new(input);
    let resolved_path = if requested_path.is_absolute() {
        requested_path.to_path_buf()
    } else {
        std::env::current_dir()
            .context("failed to resolve current working directory")?
            .join(requested_path)
    };

    fs::canonicalize(&resolved_path).await.with_context(|| {
        format!(
            "failed to resolve audio path `{}` from current directory",
            input
        )
    })
}

async fn download_remote_audio(url: Url, target_sample_rate: u32) -> Result<PreparedAudio> {
    let suffix = url
        .path_segments()
        .and_then(|segments| segments.last())
        .and_then(|name| Path::new(name).extension())
        .and_then(|ext| ext.to_str())
        .map(|ext| format!(".{ext}"))
        .unwrap_or_else(|| ".audio".to_string());

    let mut temp_file = Builder::new()
        .prefix("transcribe-cli-")
        .suffix(&suffix)
        .tempfile()
        .context("failed to create temporary audio file")?;

    let client = Client::builder()
        .user_agent("transcribe-cli/0.1.0")
        .build()
        .context("failed to build HTTP client")?;
    let response = client
        .get(url.clone())
        .send()
        .await
        .with_context(|| format!("failed to download audio from `{url}`"))?
        .error_for_status()
        .with_context(|| format!("audio download returned an error for `{url}`"))?;

    let bytes = response
        .bytes()
        .await
        .with_context(|| format!("failed to read audio body from `{url}`"))?;
    temp_file
        .write_all(bytes.as_ref())
        .context("failed to save downloaded audio")?;

    let local_path = temp_file.path().to_path_buf();
    let (metadata, samples) = inspect_audio_file(&local_path, target_sample_rate)?;

    Ok(PreparedAudio {
        display_name: url.to_string(),
        metadata,
        samples,
        _temp_file: Some(temp_file),
    })
}

fn inspect_audio_file(path: &Path, target_sample_rate: u32) -> Result<(AudioMetadata, Vec<f32>)> {
    let file = File::open(path)
        .with_context(|| format!("failed to open audio file `{}`", path.display()))?;
    let source = MediaSourceStream::new(Box::new(file), Default::default());
    let mut hint = Hint::new();

    if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
        hint.with_extension(extension);
    }

    let probe = symphonia::default::get_probe()
        .format(
            &hint,
            source,
            &FormatOptions::default(),
            &MetadataOptions::default(),
        )
        .map_err(|error| {
            anyhow::anyhow!(
                "failed to parse audio input `{}`: {error}. This build uses the Rust Symphonia core with support for mp3, wav, flac, ogg, mkv/webm audio, m4a/mp4, aac, caf, aiff, alac, adpcm, and pcm",
                path.display()
            )
        })?;

    let mut format = probe.format;
    let (mut codec_params, track_id) = select_audio_track(format.as_ref()).with_context(|| {
        if is_video_file(path) {
            format!(
                "failed to find a supported audio track in video input `{}`",
                path.display()
            )
        } else {
            format!(
                "failed to find a supported audio track in `{}`",
                path.display()
            )
        }
    })?;

    let mut decoder = symphonia::default::get_codecs()
        .make(&codec_params, &DecoderOptions::default())
        .context("failed to create audio decoder")?;
    let mut source_sample_rate = codec_params.sample_rate;
    let mut source_channels = codec_params.channels.map(|channels| channels.count());

    let mut mono_samples = Vec::new();

    loop {
        let packet = match format.next_packet() {
            Ok(packet) => packet,
            Err(SymphoniaError::IoError(error))
                if error.kind() == std::io::ErrorKind::UnexpectedEof =>
            {
                break;
            }
            Err(SymphoniaError::ResetRequired) => {
                bail!("audio stream reset is required and is not supported")
            }
            Err(error) => {
                return Err(error)
                    .with_context(|| format!("failed to read packet from `{}`", path.display()));
            }
        };

        if packet.track_id() != track_id {
            continue;
        }

        let decoded = match decoder.decode(&packet) {
            Ok(decoded) => decoded,
            Err(SymphoniaError::DecodeError(_)) => continue,
            Err(error) => {
                return Err(error)
                    .with_context(|| format!("failed to decode `{}`", path.display()));
            }
        };

        let spec = *decoded.spec();
        if source_sample_rate.is_none() {
            source_sample_rate = Some(spec.rate);
            codec_params.with_sample_rate(spec.rate);
        }
        if source_channels.is_none() {
            source_channels = Some(spec.channels.count());
            codec_params.with_channels(spec.channels);
        }

        let source_channels =
            source_channels.context("audio stream does not expose channel information")?;
        let mut sample_buffer = SampleBuffer::<f32>::new(decoded.capacity() as u64, spec);
        sample_buffer.copy_interleaved_ref(decoded);

        for frame in sample_buffer.samples().chunks(source_channels) {
            let mono = frame.iter().copied().sum::<f32>() / source_channels as f32;
            mono_samples.push(mono.clamp(-1.0, 1.0));
        }
    }

    let source_sample_rate =
        source_sample_rate.context("audio stream does not expose a sample rate")?;
    let duration = codec_params
        .n_frames
        .zip(codec_params.sample_rate)
        .map(|(frames, sample_rate)| Duration::from_secs_f64(frames as f64 / sample_rate as f64));
    let resampled_samples = if source_sample_rate == target_sample_rate {
        mono_samples
    } else {
        linear_resample(&mono_samples, source_sample_rate, target_sample_rate)
    };

    Ok((
        extract_metadata(
            &codec_params,
            duration,
            source_sample_rate,
            target_sample_rate,
        ),
        resampled_samples,
    ))
}

fn select_audio_track(format: &dyn FormatReader) -> Result<(CodecParameters, u32)> {
    let decoder_options = DecoderOptions::default();

    select_audio_track_with(format, |codec_params| {
        codec_params.codec != CODEC_TYPE_NULL
            && symphonia::default::get_codecs()
                .make(codec_params, &decoder_options)
                .is_ok()
    })
}

fn select_audio_track_with<F>(
    format: &dyn FormatReader,
    is_supported: F,
) -> Result<(CodecParameters, u32)>
where
    F: Fn(&CodecParameters) -> bool,
{
    if let Some(track) = format
        .tracks()
        .iter()
        .find(|track| is_supported(&track.codec_params))
    {
        return Ok((track.codec_params.clone(), track.id));
    }

    bail!("media input does not contain a decodable audio track")
}

fn extract_metadata(
    codec: &CodecParameters,
    duration: Option<Duration>,
    source_sample_rate: u32,
    target_sample_rate: u32,
) -> AudioMetadata {
    AudioMetadata {
        source_sample_rate: Some(source_sample_rate),
        target_sample_rate,
        channels: codec.channels.map(|channels| channels.count() as u16),
        duration,
        codec: format!("{:?}", codec.codec),
    }
}

fn linear_resample(samples: &[f32], source_rate: u32, target_rate: u32) -> Vec<f32> {
    if samples.is_empty() || source_rate == target_rate {
        return samples.to_vec();
    }

    let ratio = target_rate as f64 / source_rate as f64;
    let output_len = ((samples.len() as f64) * ratio).round() as usize;
    let mut resampled = Vec::with_capacity(output_len);

    for index in 0..output_len {
        let source_position = index as f64 / ratio;
        let left_index = source_position.floor() as usize;
        let right_index = (left_index + 1).min(samples.len().saturating_sub(1));
        let fraction = (source_position - left_index as f64) as f32;
        let left = samples[left_index];
        let right = samples[right_index];
        resampled.push(left + (right - left) * fraction);
    }

    resampled
}

#[cfg(test)]
mod tests {
    use symphonia::core::codecs::{CODEC_TYPE_NULL, CodecParameters, decl_codec_type};
    use symphonia::core::formats::{
        Cue, FormatOptions, FormatReader, Packet, SeekMode, SeekTo, Track,
    };
    use symphonia::core::io::MediaSourceStream;
    use symphonia::core::meta::{Metadata, MetadataLog};

    use super::select_audio_track_with;

    struct TestFormatReader {
        tracks: Vec<Track>,
        metadata: MetadataLog,
    }

    impl FormatReader for TestFormatReader {
        fn try_new(
            _source: MediaSourceStream,
            _options: &FormatOptions,
        ) -> symphonia::core::errors::Result<Self>
        where
            Self: Sized,
        {
            unreachable!()
        }

        fn cues(&self) -> &[Cue] {
            &[]
        }

        fn metadata(&mut self) -> Metadata<'_> {
            self.metadata.metadata()
        }

        fn seek(
            &mut self,
            _mode: SeekMode,
            _to: SeekTo,
        ) -> symphonia::core::errors::Result<symphonia::core::formats::SeekedTo> {
            unreachable!()
        }

        fn next_packet(&mut self) -> symphonia::core::errors::Result<Packet> {
            unreachable!()
        }

        fn tracks(&self) -> &[Track] {
            &self.tracks
        }

        fn into_inner(self: Box<Self>) -> MediaSourceStream {
            unreachable!()
        }
    }

    #[test]
    fn selects_first_decodable_audio_track_instead_of_default_track() {
        let fake_audio_codec = decl_codec_type(b"aud");
        let video_track = Track::new(
            1,
            CodecParameters::new()
                .for_codec(decl_codec_type(b"vid"))
                .clone(),
        );
        let audio_track = Track::new(
            2,
            CodecParameters::new().for_codec(fake_audio_codec).clone(),
        );
        let format = TestFormatReader {
            tracks: vec![video_track, audio_track],
            metadata: MetadataLog::default(),
        };

        let (codec_params, track_id) = select_audio_track_with(&format, |codec_params| {
            codec_params.codec == fake_audio_codec
        })
        .expect("audio track");

        assert_eq!(track_id, 2);
        assert_eq!(codec_params.codec, fake_audio_codec);
    }

    #[test]
    fn rejects_inputs_without_decodable_audio_tracks() {
        let unknown_track =
            Track::new(1, CodecParameters::new().for_codec(CODEC_TYPE_NULL).clone());
        let format = TestFormatReader {
            tracks: vec![unknown_track],
            metadata: MetadataLog::default(),
        };

        assert!(select_audio_track_with(&format, |_| false).is_err());
    }
}