Repository: motion-canvas/motion-canvas
File: packages/ffmpeg/server/FFmpegExporterServer.ts (TypeScript)

Code before:
        offset: settings.audioOffset ?? 0,
      });
    }

    const filterSpec: FilterSpecification[] = [];
    const streams: string[] = [];

    const sampleRates: Map<string, number> = new Map();
    const getRate = async (file: string) => {
      const rate = sampleRates.get(file);
      if (rate !== undefined) {
        return rate;
      } else {
        const cmd = `${ffprobePath} -v error -show_entries stream=sample_rate -of default=nw=1:nk=1 ${file.slice(
          1,
        )}`;
        const rate = parseInt((await promisify(exec)(cmd)).stdout);
        sampleRates.set(file, rate);
        return rate;
      }
    };

    for (let i = 0; i < sounds.length; i++) {
      const sound = sounds[i];
      this.command.input(sound.audio.slice(1));

      const filters: AudioVideoFilter[] = [];

      if (sound.start || sound.end !== undefined) {
        filters.push({
          filter: 'atrim',
          options: {start: sound.start, end: sound.end},
        });
      }

      if (sound.gain) {
        filters.push({
          filter: 'volume',
          options: {volume: `${sound.gain}dB`},
        });
      }

      const playback =
        Math.pow(2, (sound.detune ?? 0) / 1200) * (sound.playbackRate ?? 1);
      if (playback !== 1) {
        const rate = Math.round((await getRate(sound.audio)) * playback);
        filters.push({
          filter: 'asetrate',
          options: {r: rate},
        });
      }

Review comment (bug):
This fails if `end` is `undefined`:
```
Error: ffmpeg exited with code 4294967274: Error applying option 'end' to filter 'atrim': Invalid argument
Failed to set value '[1:a]atrim=start=1:end=undefined,adelay=delays=2200:all=1[a1];[a1][2:a]amix=inputs=2:dropout_transition=0:normalize=0[a]' for option 'filter_complex': Invalid argument
```
You probably want to omit these options if they are `undefined`, or filter them out in `formatFilters`

Code after:
      });
    }

    const filterSpec: FilterSpecification[] = [];
    const streams: string[] = [];

    for (let i = 0; i < sounds.length; i++) {
      const sound = sounds[i];
      this.command.input(sound.audio.slice(1));

      const playback =
        Math.pow(2, (sound.detune ?? 0) / 1200) * (sound.playbackRate ?? 1);

      let trimmed = sound.start ?? 0;
      if (sound.offset < 0) {
        trimmed -= sound.offset * playback;
      }

      if (trimmed !== 0) {
        this.command.inputOptions(`-ss ${trimmed}`);
      }

      const filters: AudioVideoFilter[] = [];
      if (sound.end !== undefined) {
        filters.push({
          filter: 'atrim',
          options: {end: sound.end - trimmed},
        });
      }

      filters.push({
        filter: 'aresample',
        options: settings.audioSampleRate.toString(),
      });

      if (sound.gain) {
        filters.push({
          filter: 'volume',
          options: {volume: `${sound.gain}dB`},
        });
      }

      if (playback !== 1) {
        const rate = Math.round(settings.audioSampleRate * playback);
        filters.push({
          filter: 'asetrate',
          options: {r: rate},
        });
        filters.push({
          filter: 'aresample',
          options: settings.audioSampleRate.toString(),