Jamesaf
Jamesaf

Reputation: 35

How to stream youtube audio with ffmpeg discord bot

so at the moment I got my disord bot to play audio from a filepath with this code

   var transmit = vnc.GetTransmitSink();

        var pcm = ConvertAudioToPcm(filepath);

        await pcm.CopyToAsync(transmit);

        Console.WriteLine(duration);
    }


    private Stream ConvertAudioToPcm(string filePath)
    {
        var ffmpeg = Process.Start(new ProcessStartInfo
        {
            FileName = "ffmpeg",
            Arguments = $@"-i ""{filePath}"" -ac 2 -f s16le -ar 48000 pipe:1",
            RedirectStandardOutput = true,
            UseShellExecute = false
        });
        return ffmpeg.StandardOutput.BaseStream;
    }

With YoutubeExplode I am able to get a stream from the URL but when I CopyToAsync I get very loud static on the discord bot. Does anybody have an idea on how to stream audio properly from a youtube URL using ffmpeg? Thank you in advance

EDIT: Got it to work with this code to create PCM stream, but it doesn't use youtube explode, it uses youtube-dl.exe

 private Stream ConvertURLToPcm(string url)
    {
        string args = $"/C youtube-dl --ignore-errors -o - {url} | ffmpeg -err_detect ignore_err -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1";
        var ffmpeg = Process.Start(new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = args,
            RedirectStandardOutput = true,
            UseShellExecute = false
        });
        return ffmpeg.StandardOutput.BaseStream;
    }

Upvotes: 1

Views: 1005

Answers (0)

Related Questions