discord.js icon indicating copy to clipboard operation
discord.js copied to clipboard

Add seek function and audio filter by adding ffmpeg options to AudioPlayer class or AudioResource class

Open Fyphen1223 opened this issue 2 years ago • 5 comments
trafficstars

Which package is this feature request for?

discord.js/voice

Feature

Just seek, audio filter using ffmpeg.

Ideal solution or implementation

Add seek function and audio filter by adding ffmpeg options to AudioPlayer class or AudioResource class. I want to do that easily, but there's no way that install them easily. I have to use some complicatede packages, and the like such as fluent-ffmpeg or discord-player. But I don't want to use discord-player package. So, I'm sending this feauture requests. I hope that this requests get accept soon!

Sincerely,

Alternative solutions or implementations

Ffmpeg, fluent-ffmpeg, discord-player(I don't want to use this)

Other context

No response

Fyphen1223 avatar Mar 07 '23 10:03 Fyphen1223

Seek was provided by discord.js until v12 inclusive, see here: https://old.discordjs.dev/#/docs/discord.js/12.3.1/typedef/StreamOptions

It was never implemented in the 13+ pipeline.

Protected avatar Sep 02 '23 17:09 Protected

As a band-aid solution, here's a hacky drop-in replacement for createAudioResource (in JavaScript) that accepts a seekTo parameter. It doesn't have any additional dependencies (prism-media is already a dependency of discord.js/voice):

import prism from 'prism-media';
import { AudioResource } from '@discordjs/voice';

const FFMPEG_PCM_ARGUMENTS = ['-analyzeduration', '0', '-loglevel', '0', '-f', 's16le', '-ar', '48000', '-ac', '2'];

function createAudioResourceAndSeek(input, options) {

    let ffmpegArguments = [...FFMPEG_PCM_ARGUMENTS];
    
    if (options.seekTo) {
        ffmpegArguments = ['-ss', options.seekTo, ...ffmpegArguments];
    }

    const edgeArbitraryToRaw = {
        type: 'ffmpeg pcm with seek',
        to: null,  //Not actually using TransformerGraph
        cost: 2,
        transformer: (input) =>
            new prism.FFmpeg({
                args: ['-i', input, ...ffmpegArguments],
            }),
    };

    const volumeTransformer = {  //Unmodified
        type: 'volume transformer',
        to: null,  //Not actually using TransformerGraph
        cost: 0.5,
        transformer: () => new prism.VolumeTransformer({ type: 's16le' }),
    }

    const edgeRawToOpus = {  //Unmodified
        type: 'opus encoder',
        to: null,  //Not actually using TransformerGraph
        cost: 1.5,
        transformer: () => new prism.opus.Encoder({ rate: 48_000, channels: 2, frameSize: 960 }),
    }

    const transformerPipeline = [edgeArbitraryToRaw];
    if (options.inlineVolume) transformerPipeline.push(volumeTransformer);
    transformerPipeline.push(edgeRawToOpus);

    const streams = transformerPipeline.map((edge) => edge.transformer(input));

    return new AudioResource(
        transformerPipeline,
        streams,
        options.metadata ?? null,
        options.silencePaddingFrames ?? 5,
    );
}

Keep in mind that:

  • This pipeline doesn't have the libopus optimization path.
  • The input is expected to be a path and the type can't be changed from (effectively) StreamType.Arbitrary .

It would probably be best to start a pull request to add the option to the SDK, since it's so straightforward. I'd imagine TransformerGraph just needs to add -ss to the ffmpeg edges if seekTo is present in the options (possibly also force a ffmpeg path to ensure the option is respected). I'll do it if I have time but anyone's welcome to jump in ahead of me.

After studying the module in the context of trying to solve this problem, my conclusion is that it would be much better if the TransformerGraph was properly exposed through discord.js so users can add their own nodes and edges without having to change discord.js/voice.

Protected avatar Sep 02 '23 23:09 Protected

I know, see https://github.com/discordjs/guide/pull/1483

Fyphen1223 avatar Sep 02 '23 23:09 Fyphen1223

Is there a known reason not to just add this to CreateAudioResourceOptions? It would be considerably lower friction...

Protected avatar Sep 02 '23 23:09 Protected

Please ask to discordjs developer, idk.

Fyphen1223 avatar Sep 02 '23 23:09 Fyphen1223