Save a video / audio file locally (Fiddlercore)

3 posts, 1 answers
  1. Hendrik
    Hendrik avatar
    10 posts
    Member since:
    Aug 2015

    Posted 13 Oct 2015 Link to this post

    Hi Eric,

    I have created a program that "grabs" a link to any video or audio files that are being played in a browser.

    (basic summary of what it looks like)

    FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS)
                {
                    uriAllowedMediaTypes = oS.oResponse["Content-Type"].ToLower().Contains("video") || oS.oResponse["Content-Type"].ToLower().Contains("audio") ||                 oS.oResponse["Content-Type"].ToLower().Contains("media");
                    if (uriAllowedMediaTypes)
                    {
                        uriStr = oS.fullUrl.ToString();                    
                        File.AppendAllText(@"C:\Desktop\medialinks.txt", oS.fullUrl + Environment.NewLine);
                    }
                };



    So for example I can watch a 1mb size video in my browser and I can then later manually download that video, since I know what the url to download that video is (using a download manager of course).
    By the time I have finished the manual download I would of used 2mb data (total, 1mb watching and 1mb download). This was my original plan all along.

    I then had a lightbulb moment. I wondered if it was possible to watch the video and while the video is playing I was hoping that my fiddlercore program could "copy" the bytes of the video / audio going through the proxy to my local drive.

    This way I can have a backup of all the videos I watch on the internet without using extra bandwidth.

    So I would watch the video and automatically have a copy saved locally.

    In the end instead of using 2mb like my first example. I would of only used 1mb and still have a local copy of the video.

    Is there a way to do this in c#? (and if possible also have a variable which holds the number of bytes that have so far been downloaded).

    I did some research but only found ways to save the text response and not much else.

    Regards
    Hendrik 

    p.s Would buffering be necessary ?


  2. Answer
    Eric Lawrence
    Admin
    Eric Lawrence avatar
    833 posts

    Posted 13 Oct 2015 Link to this post

    Hello, Hendrik--

    Yes, collecting binary files like you've described is absolutely a supported scenario. Within Fiddler you can easily do this using the File > Export > Raw Files exporter. 

    From script or FiddlerCore, there are many different approaches you can take. The simplest is to let Fiddler/FiddlerCore collect the complete response and then use your BeforeResponse handler to write it to disk when the response is completed. (Interestingly, the BeforeResponse handler actually runs AFTER the response has been returned if the response is configured to stream with bBufferResponse=false).

    To write the response to disk, simply write the responseBodyBytes array, e.g.

    FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS)
                {
                  if ((oS.responseCode == 200) && (oS.responseBodyBytes.Length > 1000))
                  {
                    string sMIME = oS.oResponse.MIMEType.ToLower();
                    if (sMIME.StartsWith("audio/") ||
                        sMIME.StartsWith("video/") ||
                        sMIME.Contains("media"))
                    {
                        oS.utilDecodeResponse();
                        oS.SaveResponseBody();                  

                    }
                  }
                };

    The SaveResponseBody method writes to your \Captures\ folder. If you prefer, you can write to a different path, e.g. 

         File.WriteAllBytes("C:\\dumped\\" + oS.SuggestedFilename, oS.responseBodyBytes);

    ... or equivalent.

    Regards,
    Eric Lawrence
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Hendrik
    Hendrik avatar
    10 posts
    Member since:
    Aug 2015

    Posted 18 Oct 2015 Link to this post

    Thanks, this helped.
Back to Top