PDA

View Full Version : Downloading a video from youtube



Faster
27th November 2009, 11:35
Hi, what qt4 class could I use to download a video from youtube? I need to download a video from an url like this: http://youtube.com/get_video?video_id=Th7-EzW0Ccw&t=vjVQa1PpcFNQDXk1DXroxSYaUxdfZ23C8lngRxA5rvs=&fmt=18. Firefox download this video but Qhttp fails to download. Any suggestions?
This is my code:


QString video_url = "http://youtube.com/get_video";
QString argument = "?video_id=Th7-EzW0Ccw&t=vjVQa1PpcFNQDXk1DXroxSYaUxdfZ23C8lngRxA5rvs=&fmt=18";
QString dest = "/home/faster/video.mp4";

QUrl url(video_url);
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
if (fileName.isEmpty())
fileName = "index.html";

file = new QFile(dest);

QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, url.port() != -1 ? url.port() : 80);

if (!url.userName().isEmpty())
http->setUser(url.userName(), url.password());

httpRequestAborted = false;
QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
if (path.isEmpty())
path = "/";
httpGetId = http->get(video_url+argument, file);

yogeshgokul
27th November 2009, 11:47
Hi, what qt4 class could I use to download a video from youtube? There are many options.
1. QHttp
2. QTCPSocket
3. QNetworkAccessManager
4. QDesktopServices::openUrl();

Faster
27th November 2009, 12:08
How can I download a video from http://youtube.com/get_video?video_id=Th7-EzW0Ccw&t=vjVQa1PpcFNQDXk1DXroxSYaUxdfZ23C8lngRxA5rvs=&fmt=18 using Qhttp? I posted my code but I don't understand what's wrong! It downloads a web page instead of the mp4 file.

yogeshgokul
27th November 2009, 12:22
How can I download a video from http://youtube.com/get_video?video_id=Th7-EzW0Ccw&t=vjVQa1PpcFNQDXk1DXroxSYaUxdfZ23C8lngRxA5rvs=&fmt=18 using Qhttp?

You need to call QHttp::get() with appropriate URL.

Faster
27th November 2009, 12:28
I used this:


http->get("http://youtube.com/get_video?video_id=Th7-EzW0Ccw&t=vjVQa1PpcFNQDXk1DXroxSYaUxdfZ23C8lngRxA5rvs=&fmt=18", file);

But it downloads only a web page and not the video file. If I open this link with firefox it downloads the correct video file. Any idea?

squidge
27th November 2009, 12:59
Its downloading what youtube is sending. You need to parse the downloaded web page most likely.

Faster
27th November 2009, 15:05
This is the web page content:


Moved Permanently
The document has moved here.

Apache Server at www.youtube.com Port 80

I'm creating a program that allows the user to download a video from youtube. I written a code that generates a link to a mp4 file using http://youtube.com/get_video. The link generated is session-sensitive and changed every time the video is accessed. This url works with web browsers but doesn't work with my program.
Now I think that this url works only with web browsers. if my argumentation is right, how can I modify my code to make youtube believe that my program is a web browser, using qhttp?

Sorry for my poor English. I hope I explained myself.

squidge
27th November 2009, 15:26
Like I said before, you need to parse the returned content and process it. If a web browser receives "Moved Permanently The document has moved here.", then they will automatically follow that redirection. This is invisible to the user - they never see the message.

Faster
27th November 2009, 15:53
No, the web browser receives the correct video while qhttp receives a redirection web page! The redirection link contained in this web page is the same link that I have used to download the page!

wysota
27th November 2009, 16:02
When I follow the link you pasted using the browser, I get a blank page.

squidge
27th November 2009, 18:05
It doesn't work for me either. Maybe when Faster posts a link that works, we can help him :)

Faster
27th November 2009, 18:58
The link doesn't work because is session-sensitive. Here (http://www.qtcentre.org/forum/p-downloading-a-video-from-youtube-post124218/postcount7.html) I told that I have written a code that generates a link to download an mp4 file from youtube. So i can't post a link that works! :( The problem is that I can download this video with Firefox but not with qhttp!

wysota
27th November 2009, 19:06
Maybe the reason is that you are logged in to youtube in firefox and not when using QHttp?

Faster
27th November 2009, 19:30
I'm not logged to youtube with firefox! I also tried to download the generated link with the qt4's http example (in examples/network/http) but this program downloads the same redirection page and not the video!

wysota
27th November 2009, 19:38
Why don't you take a network sniffer and compare the traffic that goes from firefox to youtube and from your application to youtube and tell us what the difference is? Right now we're trying to guess some reasons with you only telling us "it works with firefox". We already know that, you don't have to repeat yourself. Or give us a working download link or tell us how to get one.

squidge
27th November 2009, 21:22
The link doesn't work because is session-sensitive. Here (http://www.qtcentre.org/forum/p-downloading-a-video-from-youtube-post124218/postcount7.html) I told that I have written a code that generates a link to download an mp4 file from youtube. So i can't post a link that works! Fine, ok. Instead of posting a working link, post the code to your program that creates a working link so we can create our own (and what was the point of posting the link in your first message if you knew it wouldn't work for anyone else?)

You may also wish to delete all off-line data from your browser, or maybe even install a different browser to check if your link works there. You may find it doesn't work until you have visited and logged into the site.

Faster
27th November 2009, 21:28
I'm posting my current project! This program generates a link to download a video from youtube that works. :) Click on the "Start download" and paste the generated url on a browser. The url should work!

The link work also with other browsers!

squidge
27th November 2009, 21:53
Ok, I've tried it. The browser downloads the mp4 file.

HOWEVER, The browser gets a redirect page exactly the same as your program does. In fact, it gets two:

http://youtube.com/get_video?video_id=...
REDIRECTED TO: http://www.youtube.com/get_video?video...
REDIRECTED TO: http://v15.lscache4.c.youtube.com/...

Which makes perfect sense. You request a video, and the server redirects your browser to the least loaded server to provide you with that video.

wysota
27th November 2009, 22:08
Which also means that if you used QNetworkAccessManager, you would probably instantly get the proper output - as far as I remember QNetworkAccessManager handles redirects by itself. If not, you need to handle them yourself.

Faster
27th November 2009, 22:21
Thanks a lot fatjuicymole and wysota! I don't understand how redirects works so I don't know how I can modify my program to manage redirects with qhttp... :( If QNetworkAccessManager handles redirects by itself, where can I find some examples about downloading with QNetworkAccessManager?

wysota
27th November 2009, 23:26
Thanks a lot fatjuicymole and wysota! I don't understand how redirects works so I don't know how I can modify my program to manage redirects with qhttp... :(
http://en.wikipedia.org/wiki/URL_redirection

If QNetworkAccessManager handles redirects by itself, where can I find some examples about downloading with QNetworkAccessManager?

In the docs.

Faster
28th November 2009, 12:46
Ok I will try with QNetworkAccessManager. :) Thanks for helping me! ;)