How can I download video files programmatically in C# [modified]
-
Hi All, I have a web application and I would like to download video files i.e. .flv or .wmv files from a web site, for example, youtube or google programmatically in my c# code. If you know how to do this please respond to this message. I appreciate your help. Thanks. -- modified at 19:23 Thursday 14th September, 2006
-
Hi All, I have a web application and I would like to download video files i.e. .flv or .wmv files from a web site, for example, youtube or google programmatically in my c# code. If you know how to do this please respond to this message. I appreciate your help. Thanks. -- modified at 19:23 Thursday 14th September, 2006
Are you saying you want to offer video files to be viewed on your website, direct from your web servers hard drive? Or a way to retrieve video files from google video and YouTube to be used in your application some how?
Sunday Ironfoot www.dominicpettifer.co.uk (work in progress)
-
Hi All, I have a web application and I would like to download video files i.e. .flv or .wmv files from a web site, for example, youtube or google programmatically in my c# code. If you know how to do this please respond to this message. I appreciate your help. Thanks. -- modified at 19:23 Thursday 14th September, 2006
Similar to something I posted a few messages up... you'll want to modify this to be a bit more asynchronous, especially if you're downloading large files like videos. This function downloads the contents of the web file and stores it in a temp file and returns the temp file name.
public string GetWebFile(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
Stream responseStream = null;
FileStream fileStream = null;
string filename = "";try { // Create the web request webRequest = (HttpWebRequest)HttpWebRequest.Create(url); // Get the response webResponse = (HttpWebResponse)webRequest.GetResponse(); // Get the response stream responseStream = webResponse.GetResponseStream(); // write it to a temporary file in 1000 byte chunks filename = Path.GetTempFileName(); fileStream = new FileStream(filename, FileMode.Append); int bytesRead = 0; byte\[\] data = new byte\[1000\]; // keep looping until we read nothing do { // read the next 1000 bytes of data bytesRead = responseStream.Read(data, 0, 1000); // write the data to the file fileStream.Write(data, 0, bytesRead); } while (bytesRead > 0); } catch (Exception e) { MessageBox.Show("Error : " + e.Message); } finally { // free any resources if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } if (responseStream != null) { responseStream.Close(); responseStream.Dispose(); } if (webResponse != null) { webResponse.Close(); } } // return the contents return filename;
}