When I pass
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