Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading videos in flv format from youtube.

I cant really understand how youtube serves videos but I have been reading through what I can, it seems like the old method get_video is now obsolete and can't be used any more so because of this I am asking if there is another pythonic and simple method for collecting youtube videos.

like image 731
Jakob Bowyer Avatar asked Nov 26 '10 18:11

Jakob Bowyer


2 Answers

You might have some luck with youtube-dl

http://rg3.github.com/youtube-dl/documentation.html

I'm not sure if there's a good API, but it's written in Python, so theoretically you could do something a little better than Popen :)

like image 129
Hank Avatar answered Oct 03 '22 19:10

Hank


Here is a quick Python script which downloads a Youtube video. No bells and whistles, just scrapes out the necessary urls, hits the generate_204 url and then streams the data to a file:

import lxml.html
import re
import sys
import urllib
import urllib2

_RE_G204 = re.compile('"(http:.+.youtube.com.*\/generate_204[^"]+")', re.M)
_RE_URLS = re.compile('"fmt_url_map": "(\d*[^"]+)",.*', re.M)

def _fetch_url(url, ref=None, path=None):
    opener = urllib2.build_opener()
    headers = {}
    if ref:
        headers['Referer'] = ref
    request = urllib2.Request(url, headers=headers)
    handle = urllib2.urlopen(request)
    if not path:
        return handle.read()
    sys.stdout.write('saving: ')
    # write result to file
    with open(path, 'wb') as out:
        while True:
            part = handle.read(65536)
            if not part:
                break
            out.write(part)
            sys.stdout.write('.')
            sys.stdout.flush()
        sys.stdout.write('\nFinished.\n')

def _extract(html):
    tree = lxml.html.fromstring(html)
    res = {'204': _RE_G204.findall(html)[0].replace('\\', '')}
    for script in tree.findall('.//script'):
        text = script.text_content()
        if 'fmt_url_map' not in text:
            continue
        # found it, extract the urls we need
        for tmp in _RE_URLS.findall(text)[0].split(','):
            url_id, url = tmp.split('|')
            res[url_id] = url.replace('\\', '')
        break
    return res

def main():
    target = sys.argv[1]
    dest = sys.argv[2]
    html = _fetch_url(target)
    res = dict(_extract(html))
    # hit the 'generate_204' url first and remove it
    _fetch_url(res['204'], ref=target)
    del res['204']
    # download the video. now i grab the first 'download' url and use it.
    first = res.values()[0]
    _fetch_url(first, ref=target, path=dest)

if __name__ == '__main__':
    main()

Running it:

python youdown.py 'http://www.youtube.com/watch?v=Je_iqbgGXFw' stevegadd.flv
saving: ........................... finished.
like image 23
samplebias Avatar answered Oct 03 '22 19:10

samplebias