Created
January 28, 2011 03:33
-
-
Save spaetzel/799788 to your computer and use it in GitHub Desktop.
Attempt at partially downloading a video for use in ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static StreamReader DownloadPartial(SUri url, int bytesToGet) | |
{ | |
string result = string.Empty; | |
HttpWebRequest request; | |
request = WebRequest.Create(url) as HttpWebRequest; | |
//get first 1000 bytes | |
request.AddRange(0, bytesToGet - 1); | |
// the following code is alternative, you may implement the function after your needs | |
WebResponse response = request.GetResponse(); | |
return new StreamReader(response.GetResponseStream()); | |
} | |
public static string DownloadFile(string directory, SpaetzelUtilities.SUri url, int bytesToGet) | |
{ | |
if (!url.IsNullOrEmpty() ) | |
{ | |
if (!Directory.Exists(directory)) | |
{ | |
Directory.CreateDirectory(directory); | |
} | |
// string fileName = url.Substring(url.LastIndexOf("/") + 1); | |
string extension = Utilities.GetFilenameExtension(url.ToString()); | |
string fileName = Spaetzel.UtilityLibrary.Utilities.CalculateMd5Hash(url.ToString()) + "." + extension; | |
string fullPath = directory + "/" + fileName; | |
if (!File.Exists(fullPath)) | |
{ | |
// Don't download the file twice | |
try | |
{ | |
Console.WriteLine(String.Format("Downloading {0}", url)); | |
var result = DownloadPartial(url, bytesToGet); | |
using (BinaryWriter writer = new BinaryWriter(File.Open(fullPath, FileMode.Create))) | |
{ | |
var file = result.ReadToEnd(); | |
writer.Write(file); | |
} | |
} | |
catch (WebException) | |
{ | |
// Got a 404 or 500 from the site | |
return ""; | |
//return fullPath; | |
} | |
} | |
// var fullPath = Utilities.DownloadFile(directory, url); | |
return fullPath; | |
} | |
else | |
{ | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment