2017-02-05 42 views
0

如何使用Multiline TextBox從YouTube下載多個視頻?如何使用Multiline TextBox從YouTube下載多個視頻?

這是到目前爲止我的代碼:

var split = txtURL.Text.Split(','); 
for (int i = 0; i < split.Length; i++) { 
    IEnumerable<VideoInfo> videolar = DownloadUrlResolver.GetDownloadUrls(split[i]); 
    VideoInfo video = videolar.First(p => p.VideoType == VideoType.Mp4 && 
     p.Resolution == Convert.ToInt32(comboBox1.Text)); 
    if (video.RequiresDecryption) { 
     DownloadUrlResolver.DecryptDownloadUrl(video); 
    } 
    VideoDownloader downloader = new VideoDownloader(video, 
     Path.Combine(Application.StartupPath + "\\ " + video.Title + 
      GetSafeFileName(video.Title, '_') + video.VideoExtension)); 
    downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged; 
    Thread thread = new Thread(() => { downloader.Execute(); }) { IsBackground = true }; 
    thread.Start(); 
} 
+0

請幫我:( –

+0

有沒有簡單的方法來獲得YouTube視頻下載的鏈接,你對此有與YouTube搜索? –

+0

是的,我能得到URL任何想法,但我不能讓散裝下載 –

回答

0

人誰想要開始這個,必須添加此NuGet包:

安裝,包裝YoutubeExtractor

這裏是該頁:YoutubeExtractor

最後,我下載URL提取器並創建洞項目。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 
    } 
    BackgroundWorker bw = new BackgroundWorker(); 
    int currentLine = 0; 
    ProgressBar progressBar1 = new ProgressBar(); 
    string[] lines; 
    string resolution; 

    private void btnIndir_Click(object sender, EventArgs e) 
    { 
     resolution = comboBox1.Text; //360, 480, 720, 1080 

     lines = txtURL.Text.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
     bw.DoWork += new DoWorkEventHandler(bw_DoWork); 
     bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DownloadNextLine); 
     bw.RunWorkerAsync(); 
    } 

    public void bw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     progressBar1.Value = 0; 
     var split = lines[currentLine].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries); 
     for (int i = 0; i < split.Length; i++) 
     { 
      IEnumerable<VideoInfo> videolar = DownloadUrlResolver.GetDownloadUrls(split[i]); 
      VideoInfo video = videolar.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(resolution)); 
      if (video.RequiresDecryption) 
      { 
       DownloadUrlResolver.DecryptDownloadUrl(video); 
      } 

      VideoDownloader downloader = new VideoDownloader(video, System.IO.Path.Combine(Application.StartupPath + "\\ " + GetSafeFileName(video.Title) + '_' + video.VideoExtension)); 
      downloader.DownloadProgressChanged += downloader_DownloadProgressChanged; 
      downloader.Execute(); 
     } 
    } 

    private string GetSafeFileName(string title) 
    { 
     title.Replace("/", "").Replace(",", "").Replace("\\", "").Replace("+", ""); 
     return title; 
    } 

    void downloader_DownloadProgressChanged(object sender, ProgressEventArgs e) 
    { 
     SetPbValue((int)e.ProgressPercentage); 
    } 

    void SetPbValue(int percent) 
    { 
     if (progressBar2.InvokeRequired) 
      progressBar2.Invoke(new Action<int>(SetPbValue), percent); 
     else 
      progressBar2.Value = percent; 
    } 

    public void DownloadNextLine(object sender, RunWorkerCompletedEventArgs e) 
    { 
     currentLine++; 
     if (currentLine == lines.Length) 
      return; 
     bw.RunWorkerAsync(); 
    } 

} 

它測試和工作完美。現在我很高興有這個有用的應用程序。但是如果您的錯誤仍然存​​在,我肯定其中一行有無效的視頻網頁網址。最後的逗號','是錯誤解釋,而不是網址的結尾。因爲someText.Split(',')中的項目將不會有任何逗號。

+0

我的傻瓜... 我正在測試 –

+0

不行.. 下載我想要的所有行。 –

+0

我分享了整個代碼,請看看 –

0
private void btnIndir_Click(object sender, EventArgs e) 
    { 
        progressBar1.Minimum = 0; 
        progressBar1.Maximum = 100; 
        var split = txtURL.Text.Split(','); 
        for (int i = 0; i < split.Length; i++) 
        { 
         IEnumerable<VideoInfo> videolar = DownloadUrlResolver.GetDownloadUrls(split[i]); 
         VideoInfo video = videolar.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(comboBox1.Text)); 
         if (video.RequiresDecryption) 
         { 
          DownloadUrlResolver.DecryptDownloadUrl(video); 
         } 
         VideoDownloader downloader = new VideoDownloader(video, Path.Combine(Application.StartupPath + "\\ " + video.Title + GetSafeFileName(video.Title, '_') + video.VideoExtension)); 
         downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged; 
         Thread thread = new Thread(() => { downloader.Execute(); }) { IsBackground = true }; 
         thread.Start(); 
      } 
     }