Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to download a video from a website?  (Read 2354 times)

0 Members and 1 Guest are viewing this topic.

jbos190

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to download a video from a website?
« on: January 23, 2014, 06:58:40 am »
When I use the http functionalities of sfml, I can download videos, but I can't play them properly.  For example, if I send a request to http://assets.ign.com/videos/zencoder/640/d092914f4631a98554ac9849165b5ee9-500000-1390433049-w.mp4 , and save the response body as a file with the .mp4 extention, the file does not play when I open it with VLC player.  When I enter the uri into IE browser, it downloads and then plays properly.  It also works when I download it using URLDownloadToFile() in Visual Studio.  Also, the file downloaded with sfml is about .3MB larger, although I couldn't see any noticeable differences when I opened the files as .txt.  I also encountered this same problem when using libcurl.  Does anyone know is causing this problem?
« Last Edit: January 23, 2014, 07:01:54 am by jbos190 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to download a video from a website?
« Reply #1 on: January 23, 2014, 08:03:02 am »
How do you save the file after downloading it?
Laurent Gomila - SFML developer

jbos190

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to download a video from a website?
« Reply #2 on: January 23, 2014, 05:51:51 pm »
Using std::ofstream, I give the file a name with the required ext:
sf::Http http;
http.setHost("http://assets.ign.com/");
sf::Http::Request request;     
request.setUri("/videos/zencoder/640/d092914f4631a98554ac9849165b5ee9-500000-1390433049-w.mp4");
sf::Http::Response response = http.sendRequest(request);       
std::string data = response.getBody();
std::ofstream fout("example.mp4");
fout << data;
fout.close();
 
I have tried this with .flv and .mp4 videos from various websites and get the same result - a file with extra bytes that doesn't play.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to download a video from a website?
« Reply #3 on: January 23, 2014, 06:52:00 pm »
Typical error: you forget to open the stream as binary (std::ios::bin in the constructor).
Laurent Gomila - SFML developer

jbos190

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to download a video from a website?
« Reply #4 on: January 23, 2014, 08:25:54 pm »
Thank you.