When I pass
http://www.youtube.com/watch?v=Zi8vJ_lMxQI
as a parameter, the HTML code is downloaded instead of the video.
public void StreamDownload(Uri currentUrl)
{
int dataLength;
int bytesRead;
WebRequest req = WebRequest.Create(currentUrl);
WebResponse response = req.GetResponse();
string oFileName = System.IO.Path.GetFileName(URLBox.Text);
oFileName = AdditionalFunctions.CorrectFname(oFileName); //this function replaces forbidden characters with '♥'.
Stream dataStream = response.GetResponseStream();
byte [] buffer = new byte[1024];
FileStream oFile = new FileStream(oFileName,FileMode.Append);
dataLength = (int)response.ContentLength;
do
{
bytesRead = dataStream.Read(buffer, 0, buffer.Length);
oFile.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
}
Edited due to the comments
Is there an universal algorithm to extract an video stream from a specified URL? Youtube was only an example.
As I stated in comment: WEBPAGE (what you get from your URL) is something that carries PLAYER component for the media that will be accessed from it by means of some kind of streaming.
So first, you have to parse the web page to find if your stream URL is there somewhere. If it isn't it would require some kind of network packet capture to determine what connection was recently open from the process that tries to load/play the video, and then capture the data from that connection.
Tricky stuff.
For youtube:
Downloading video from YouTube
Your code is probably good, but the url you are using is for the web page and not the video-file itself.
Check the html source that is rendered (Right-click page and "View source"). You should find the url someway there.
You could also traverse the html from your youtube-link to find the source url in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With