从youtube视频获取视频流的代码 YoutubeClient youtube =新的YoutubeClient()var StreamManifest =等待youtube.Videos.GetManifestAsync()var StreamInfo =等待StreamManifest.GetAudioOnly().WithHighestBitrate();var stream = youtube.Videos.Streams.GetAsync(StreamInfo) 允许管道输入标准输入并获取标准输出的代码. var memoryStream = new MemoryStream();等待Cli.Wrap("ffmpeg").WithArguments("-hide_banner -loglevel panic -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1").WithStandardInputPipe(PipeSource.FromStream(strean)).WithStandardOutputPipe(PipeTarget.ToStream(memoryStream)).ExecuteAsync(); 播放内存流不和谐! 使用(var discord = AudioClient.CreatePCMStream(AudioApplication.Mixed)){尝试{await AudioClient.WriteAsync(memoryStream.ToArray(),0,(int)memoryStream.Length);}最后{等待AudioClient.FlushAsync();}} I am creating a discord bot and it should be able to play music from youtube. I've already created the code to play music but i still need to download the music. When I download it I get this output: Downloading (VIDEO NAME) - YouTube.mp4 to C:\Users\Laurin\source\repos\DiscordBot\DiscordBot\bin\Release\netcoreapp2.0\win10-x64Downloaded. Saving to disk16:46:15 Gateway A MessageReceived handler is blocking the gateway task.My current code: [Command("yt")] public async Task ytCommand([Remainder] string url) { SaveVideoToDisk(url); } void SaveVideoToDisk(string link) { var youTube = YouTube.Default; // starting point for YouTube actions var video = youTube.GetVideo(link); // gets a Video object with info about the video Console.WriteLine("Downloading " + video.FullName + " to " + Directory.GetCurrentDirectory()); byte[] bytes = video.GetBytes(); Console.WriteLine("Downloaded. Saving to disk"); File.WriteAllBytes(Directory.GetCurrentDirectory() + video.FullName, bytes); }I'm using libvideo/VideoLibrary to download the video 解决方案 Instead of downloading the video, you can use Youtube Explode, To get the Stream object and pipe the stream into ffmpeg.Edit: System.Diagnostics.process does not do well with Piping So I used CliwrapCode to get a stream from a youtube videoYoutubeClient youtube = new YoutubeClient()var StreamManifest = await youtube.Videos.GetManifestAsync()var StreamInfo = await StreamManifest.GetAudioOnly().WithHighestBitrate();var stream = youtube.Videos.Streams.GetAsync(StreamInfo)Code to allow pipe to stdin and get the Stdout.var memoryStream = new MemoryStream(); await Cli.Wrap("ffmpeg") .WithArguments(" -hide_banner -loglevel panic -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1") .WithStandardInputPipe(PipeSource.FromStream(strean)) .WithStandardOutputPipe(PipeTarget.ToStream(memoryStream)) .ExecuteAsync();Play Memory Stream To Discord!using (var discord = AudioClient.CreatePCMStream(AudioApplication.Mixed)) { try {await AudioClient.WriteAsync(memoryStream.ToArray(), 0, (int)memoryStream.Length); } finally { await AudioClient.FlushAsync(); } } 这篇关于在c#和discord.net中下载YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 18:44