2017-05-23 38 views
3

我想下载你使用C#代码管视频,但我没有得到正确的代码。我搜索了很多链接,但没有得到任何正确的链接和代码。使用c#下载你管视频?

我想用c#代码在本地文件夹中下载you tube视频。我已经尝试了一个链接,但该代码只是让我的本地文件夹中的空视频,所以任何人都有想法,怎么可以做到这一点。

下面是我到目前为止尝试过的代码。

var VedioUrl = "https://www.youtube.com/embed/" + objYouTube.VideoID + ".mp4"; 

WebRequest MyRequest = HttpWebRequest.Create(VedioUrl); 
WebResponse MyResponse = MyRequest.GetResponse(); 
string RealURL = MyResponse.ResponseUri.ToString(); 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RealURL); 
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
Stream receiveStream = myHttpWebResponse.GetResponseStream(); 
Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); 
StreamReader readStream = new StreamReader(receiveStream, encode); 
StreamWriter writer = new StreamWriter(Server.MapPath("~/youtube/" + objYouTube.VideoID + ".mp4"), true); 
writer.Write(readStream.ReadToEnd()); 
writer.Close(); 

所以这里是我试图下载我的视频网址:“https://www.youtube.com/embed/UCsiNPbLbwZk43FOCRrdKBlA.mp4

+0

[youtube-dl](https://youtube-dl.org/)是一个不错的开源解决方案。您可以将其添加到您的应用程序中。 –

+0

@AchaBill但如何使用它的任何想法,请给我一些提示。 – coderwill

+0

查看[Process class](https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v = vs.110).aspx) –

回答

2

我必须找到使用C#代码下载你管的视频解决方案。

首先需要在Visual Studio安装上 “libvideo的NuGet包管理器控制台。

这里包管理器控制台上触发此命令:

Install-Package VideoLibrary 

首先在你的控制器添加在上面这个命名空间:

using VideoLibrary; 

现在这里只写只是代码,并通过URL链接:

var VedioUrl = "https://www.youtube.com/embed/" + objYouTube.VideoID + ".mp4"; 
var youTube = YouTube.Default; 
var video = youTube.GetVideo(VedioUrl); 
System.IO.File.WriteAllBytes(Server.MapPath("~/youtube/" + video.FullName + ".mp4"), video.GetBytes()); 
0

你可以在你的应用程序中嵌入youtube-dl
它提供了广泛的Youtube下载选项。

基本上,你做这样的事情。

using System; 
using System.Diagnostics; 
using System.ComponentModel; 

namespace MyProcessSample 
{ 
    class MyProcess 
    { 
     public static void Main() 
     { 
      Process myProcess = new Process(); 

      try 
      { 
       myProcess.StartInfo.UseShellExecute = false; 
       myProcess.StartInfo.FileName = @"yourpath\youtube-dl.exe"; 
       myProcess.StartInfo.CreateNoWindow = false; 
       myProcess.StartInfo.Arguments = "https://www.youtube.com/watch?v=KFqrp4KSxio"; 
       myProcess.Start(); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
      } 
     } 
    } 
} 

您可以使用此C# wapper for youtube-dl
您可以扩展它以满足您的需求。

Process Class

+0

所以我需要将youtube-dl参考添加到我们的.net c#解决方案中吗? – coderwill

+0

您需要二进制文件。即可执行文件。 你可以使用这个[C#wrapper](https://github.com/detaybey/WrapYoutubeDl)。 –

+0

好吧,我会尝试和你的解决方案,然后任何问题的脸,你可以请帮助我只是给你一个平好 – coderwill

0

有关更多最新解决方案,请查看YoutubeExplode。它提供了丰富的API来查询和下载Youtube视频,而且与其他图书馆不同,它仍在积极维护。