decodeWithFfmpeg function

Future<Float32List?> decodeWithFfmpeg(
  1. String path, {
  2. String ffmpeg = 'ffmpeg',
})

Decodes path to mono floats at analysisSampleRate using ffmpeg (the executable name or full path). Returns null when ffmpeg is missing, fails, or produces no audio.

Implementation

Future<Float32List?> decodeWithFfmpeg(String path,
    {String ffmpeg = 'ffmpeg'}) async {
  try {
    final proc = await Process.start(ffmpeg, [
      '-v',
      'error',
      '-i',
      path,
      '-ac',
      '1',
      '-ar',
      '$analysisSampleRate',
      '-f',
      's16le',
      '-',
    ]);
    final bb = BytesBuilder(copy: false);
    proc.stderr.drain<void>();
    await for (final c in proc.stdout) {
      bb.add(c);
    }
    final code = await proc.exitCode;
    if (code != 0 || bb.length < 4) return null;
    final bytes = bb.takeBytes();
    return pcm16ToFloat(
        Int16List.view(bytes.buffer, bytes.offsetInBytes, bytes.length ~/ 2));
  } catch (_) {
    return null;
  }
}