在c#和discord.net中下载YouTube视频 [英] Download YouTube video in c# and discord.net

查看:122
本文介绍了在c#和discord.net中下载YouTube视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个不和谐的机器人,它应该能够播放youtube上的音乐.我已经创建了播放音乐的代码,但是我仍然需要下载音乐.当我下载它时,得到以下输出:

 正在下载(视频名称)-YouTube.mp4到C:\ Users \ Laurin \ source \ repos \ DiscordBot \ DiscordBot \ bin \ Release \ netcoreapp2.0 \ win10-x64已下载.保存到磁盘16:46:15网关MessageReceived处理程序正在阻止网关任务. 

我当前的代码:

  [Command("yt")]公共异步任务ytCommand([Remainder]字符串url){SaveVideoToDisk(url);}void SaveVideoToDisk(字符串链接){var youTube = YouTube.Default;//YouTube动作的起点var video = youTube.GetVideo(link);//获取带有有关视频信息的Video对象Console.WriteLine(正在下载" + video.FullName +到" + Directory.GetCurrentDirectory());byte []个字节= video.GetBytes();Console.WriteLine(已下载.正在保存到磁盘");File.WriteAllBytes(Directory.GetCurrentDirectory()+ video.FullName,字节);} 

我正在使用 libvideo/VideoLibrary 下载视频

解决方案

代替下载视频,您可以使用Youtube爆炸,获取Stream对象并将流通过管道传输到ffmpeg.

System.Diagnostics.process在管道方面效果不佳,所以我使用了 Cliwrap

从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-x64
Downloaded. Saving to disk
16: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 Cliwrap

Code to get a stream from a youtube video

YoutubeClient 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视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆