4

I'm writing a CLI for a music-media-platform. One of the features is going to be that you can directly play YouTube videos from the CLI. I don't really have an idea of how to do it, but this one sounded the most reasonable:

I'm going to use of those sites where you can download music from YouTube, for example, http://keepvid.com/ and then I directly stream and play this, but I have one problem. Is there any Python library capable of doing this and if so, do you have any concrete examples?

I've been looking, but I found nothing, even not with GStreamer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wvd
  • 1,189
  • 2
  • 12
  • 23

2 Answers2

2

You need two things to be able to download a YouTube video, the video id, which is represented by the v= section of the URL, and a hidden field t= which is present in the page source. I have no idea what this t value is, but it's what you need :)

You can then download the video using a URL in the format;

http://www.youtube.com/get_video?video_id=*******&t=*******

Where the stars represent the values obtained.

I'm guessing you can ask for the video id from user input, as it's straightforward to obtain. Your program would then download the HTML source for that video, parse the source for the t value, then download the video using the newly constructed URL.

For example, if you open this link in your browser, it should download the video, or you can use a downloading program such as Wget;

http://www.youtube.com/get_video?video_id=3HrSN7176XI&t=vjVQa1PpcFNM4c8MbEhsnGaNvYKoYERIJ-hK7ErLpUI=

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • I'm pretty sure YouTube blocked this way of getting their videos, also did you try to example you gave yourself? – wvd May 22 '10 at 08:06
  • Yes. It worked fine for me last night, but now doesn't work. I guess the location of the video changed.... – Jivings May 22 '10 at 09:58
  • Where do you exactly find the "t=***" in a source file of the website? I tried searching it but didn't really found anything which looked like that. – wvd May 22 '10 at 10:30
  • Open the source and search for `&t=`. On the pages that I've tried that returns one or two results, either of which is the value you're looking for. It's located in a Javascript function that determines the source for the video. – Jivings May 22 '10 at 11:17
  • I *think* this no longer works... (I can still see a t value in the source - but putting it together like above doesn't work) – Andy Hayden Sep 09 '12 at 22:33
  • @hayden Yeah, this answer is quite old. I wouldn't be surprised if the method has changed. – Jivings Sep 10 '12 at 06:03
0

It appears that KeepVid is simply a JavaScript bookmarklet that links you to a KeepVid download page where you can then download the YouTube video in any one of a variety of formats. So, unless you want to figure out how to stream the file that it links you to, it's not easily doable. You'd have to scrape the page returned and figure out which URL you wanted to download, and then you'd have to stream from that URL (and some of the formats may or may not be streamable anyway).

And as an aside, even though they don't have a terms of service specified, I'd imagine that since they appear to be mostly advertisement-supported that abusing their functionality by going around their advertisement-supported webpage would be ethically questionable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • Well, I was just reffering to KeepVid as an example, if there's any better sites I would be happy to hear that. – wvd May 21 '10 at 21:13