Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing the web response stream

Tags:

c#

url

stream

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.

like image 224
0x6B6F77616C74 Avatar asked Mar 22 '26 02:03

0x6B6F77616C74


2 Answers

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

like image 104
Daniel Mošmondor Avatar answered Mar 24 '26 16:03

Daniel Mošmondor


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.

like image 44
Mario S Avatar answered Mar 24 '26 14:03

Mario S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!