Dowload a YouTube Video Usong C#.Net .How can download YouTube Video using C#.net in windows application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Net;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
namespace DownloadYouTube
{
public partial class Form1 : Form
{
string current_working_directory = Application.StartupPath; // This is used to determine where to save the video.
string youtube_video_title = "video"; // This gets set near the end of get_download_url()
string strUrl = @"http://www.youtube.com/watch?v=_QUrWjEGj34&feature=fvhl";///* insert video URL here*/
public Form1()
{
InitializeComponent();
}
private void btnDownload_Click(object sender, EventArgs e)
{
string youtube_url = makeYouTubeURLCompliant(strUrl); // This makes the URL compliant with the code.
string download_url = get_download_url(youtube_url); // This extracts the download URL from the YouTube website HTMl source code.
download_video_from_url(download_url); // This actually downloads the video. I haven't tested this as I used someone elses queue download class for my own downloading needs.
}
public string get_download_url(string url)
{
string download_url = null;
string buffer = getYouTubePageContent(url);
string title = string.Empty;
if (buffer.IndexOf("Error:") < 0)
{
int start = 0, end = 0;
string startTag = "/watch_fullscreen?";
string endTag = ";";
start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase);
end = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase);
string str = buffer.Substring(start + startTag.Length, end - (start + startTag.Length));
string vid = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"));
string l = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"));
string t = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"));
title = str.Substring(str.IndexOf("&title=") + 7);
download_url = "http://youtube.com/get_video?" + vid + l + t;
}
else
{
MessageBox.Show("Error downloading video");
}
youtube_video_title = title;
return download_url;
}
private string makeYouTubeURLCompliant(string url)
{
url = url.Replace("www.youtube.com", "youtube.com");
if (url.IndexOf("http://youtube.com/v/") >= 0)
{
url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=");
}
if (url.IndexOf("http://youtube.com/watch?v=") < 0)
{
url = "";
}
return (url);
}
// This is a helper method is used by get_download_url()
private string getYouTubePageContent(string url)
{
string buffer;
try
{
string outputBuffer = "where=46038";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentLength = outputBuffer.Length;
req.ContentType = "application/x-www-form-urlencoded";
StreamWriter swOut = new StreamWriter(req.GetRequestStream());
swOut.Write(outputBuffer);
swOut.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
buffer = sr.ReadToEnd();
sr.Close();
}
catch (Exception exp)
{
buffer = "Error: " + exp.Message.ToString();
}
return (buffer);
}
private void download_video_from_url(string url)
{
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(url), @current_working_directory + "\\" + makeTitleFileCompliant(youtube_video_title) + ".flv");
}
// This method makes the YouTube title compliant as a filename
public string makeTitleFileCompliant(string title_uncomp)
{
return title_uncomp.Replace("\\", "").Replace("/", "").Replace("*", "").Replace(":", "").Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "");
}
}
}
Articles
- Articles(C#.NET) (3)
- ASP.NET (6)
- Asp.Net(Email) (1)
- Asp.net(Image) (2)
- Asp.Net(Web.Config) (2)
- C#.NET (5)
- C#.NET Threading (3)
- C#.NET(ASP.NET) (4)
- Comments in PHP (1)
- Encryption In PHP (1)
- iPhone (Articles) (1)
- JavaScript (1)
- Json with PHP (1)
- LINQ (1)
- PHP (2)
- PHP Constant (1)
- PHP Operators (1)
- PHP Print Command (1)
- PHP Tutorial (2)
- Strings In PHP (1)
- Variable Declaration In PHP (1)
- WPF (1)
- XAML (2)
About Me
Help Link
Followers
Posted by
Sreejith A.K
Thursday, October 1, 2009
Labels:
C#.NET
1 comments:
-
whoah this weblog is wonderful i like studying your articles.
Keep up the good work! You recognize, many individuals are hunting round for this information, you could aid them greatly.
Also visit my web page ... stock tips for today
Subscribe to:
Post Comments (Atom)