How to download youtube videos using a python script

16,078

You need to parse the flashvars variable of the <embed> tag that contains the video. These change around, so some experimentation may be required to find the current variable names. Roughly speaking, you'll want to use a libraries like mechanize to grab the HTML of the page and BeautifulSoup to parse the HTML and extract the flashvars field of the <embed> element. Then look around at the variables to figure out which one contains the video URL.

e.g.,

  br = mechanize.Browser()
  # Browser options
  br.set_handle_equiv(True)
  br.set_handle_redirect(True)
  br.set_handle_referer(True)
  br.set_handle_robots(False)
  # Follows refresh 0 but not hangs on refresh > 0
  br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
  # User-Agent (this is cheating, ok?)
  br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
  br.open('%s?v=%s' % (YOUTUBE_URL, vidId))
  soup = BeautifulSoup.BeautifulSoup(br.response().read())
  flashVars = urllib2.urlparse.parse_qs(soup.find('embed').get('flashvars'))
  # Return the first second video source URL
  return flashVars['fmt_stream_map'][0].split('|')[1]
Share:
16,078
ConfusedAboutCPP
Author by

ConfusedAboutCPP

Updated on November 21, 2022

Comments

  • ConfusedAboutCPP
    ConfusedAboutCPP over 1 year

    I need to download videos from youtube using a python script. However i am unable to get the url of the video from the youtube page.

    For example, given the url: http://www.youtube.com/watch?v=5qcmCUsw4EQ&feature=g-all-u&context=G2633db8FAAAAAAAAAAA

    1. I need to download the video as a flv or any other format. Also i need to be able to download it multiple quality.
    2. I tried several scripts like youtube-dl and quvi but they all give errors and dont work. Please help. It shall be deeply appreciated.
    • phihag
      phihag over 12 years
      As the developer of youtube-dl, I'd be quite in interested in how it doesn't work. Feel free to report an issue or ask a stackoverflow question with the youtube-dl tag.
    • Wooble
      Wooble over 12 years
      What errors? How don't they work?
    • ConfusedAboutCPP
      ConfusedAboutCPP over 12 years
      quvi gives this error: error: /usr/share/quvi/lua/website/youtube.lua:117: no match: fmt_url_map
    • ConfusedAboutCPP
      ConfusedAboutCPP over 12 years
      i installed youtube-dl on my ubuntu installation from the repositories, and it gave errors. After running "youtube-dl -U" it is working. I guess the repos have a older version of youtube-dl
  • david_adler
    david_adler almost 11 years
    this no longer works... soup.find('embed') returns None as <embed> is not in the html (obvi). However, when I print out the soup and load it in a browser I can find the embed element. I guess this is because the browser does some kind of onload() stuff which loads the embed el. Any ideas how I can simulate this onload with mechanize?
  • jamesnvc
    jamesnvc almost 11 years
    YouTube has been constantly changing how they embed videos, precisely to thwart things like this. JWZ has been posting about this recently (jwz.org/blog/2013/08/another-day-another-cipher-change-2), which you may find instructive (see the "Previously" links at the bottom for more)
  • Devendra Bhat
    Devendra Bhat almost 6 years
    But if you want to simulate then you can use selenium as well