1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
class YoutubeDownload
{
public YoutubeDownload()
{
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
}
private WebClient webClient = new WebClient();
public delegate void DownloadVideoCompletedEventhandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e);
public event DownloadVideoCompletedEventhandler DownloadVideoCompleted;
public delegate void DownloadVideoProgressChangedEventhandler(object sender, DownloadProgressChangedEventArgs e);
public event DownloadVideoProgressChangedEventhandler DownloadVideoProgressChanged;
public string GetYoutubeUrl(string youtubeUrl)
{
string flashvars = "";
string fmt_stream_map = "";
string[] seperator = { "%7C" };
Regex flashvarsRegex = new Regex("(?<flashvars>flashvars=\".*\")");
Regex fmt_stream_mapRegex = new Regex("(?<fmt_stream_map>fmt_stream_map=.*\")");
flashvars = flashvarsRegex.Match(webClient.DownloadString(youtubeUrl)).Groups["flashvars"].Value;
fmt_stream_map = fmt_stream_mapRegex.Match(flashvars).Groups["fmt_stream_map"].Value;
return HttpUtility.UrlDecode(fmt_stream_map.Split(seperator, StringSplitOptions.RemoveEmptyEntries)[5]);
}
public void DownloadVideo(string downloadUrl, string fullFilename)
{
webClient.DownloadFileAsync(new Uri(downloadUrl), fullFilename);
}
private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (DownloadVideoProgressChanged != null)
{
DownloadVideoProgressChanged(sender, e);
}
}
private void webClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (DownloadVideoCompleted != null)
{
DownloadVideoCompleted(sender, e);
}
}
public string GetVideoTitle(string youtubeUrl)
{
Regex titleRegex = new Regex("title=\".*\"");
string title = titleRegex.Match(webClient.DownloadString(youtubeUrl)).NextMatch().Value;
title = title.Replace("title=\"", "");
title = title.Remove((title.Length - 1), 1);
return title;
}
}
|