2017-05-23 36 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視頻,而且與其他圖書館不同,它仍在積極維護。