How to get flv file from YouTube page

How can you get the flv file from the youtube page, for example: www.youtube.com/watch? v = T8YCSJpF4g4 ... Any ideas?

So far I have tried:

import urllib import os import sys page=urllib.urlopen('http://www.youtube.com/watch?v=FuLDIgfPae8') result = page.read() #result=open("test.html").read() print result startindex = result.index("var swfArgs") result = result[startindex:] endindex=result.index("};") result=result[:endindex] result = result.split(",") get={} print result def download(url,output): """Copy the contents of a file from a given URL to a local file.""" webFile = urllib.urlretrieve(url,output+".flv") for line in result: if "video_id" in line or "\"t\"" in line : v,vnum = line.replace("\"","").split(":") get[v.strip()]=vnum.strip() dload = "http://www.youtube.com/get_video?video_id=%s&t=%s" % ( get['video_id'],get['t']) try : download(dload,get['video_id']) except Exception,e: print "Error: ",e 

but this gives me an error:

 startindex = result.index("var swfArgs") ValueError: substring not found 

and im not sure how to fix this error.

@icktoofay

How would I respond to all these requests ?:

enter image description here

Please help, this is killing me.

+4
source share
2 answers

I suggest a look at youtube-dl . This is a brief description taken on his website:

youtube-dl is a small command-line tool for downloading videos from YouTube.com and several other sites. This requires a Python interpreter, version 2.x (x is at least 5), and it is platform independent. This should work in your Unix window, on Windows or Mac OS X. It is released into the public domain, which means that you can change it, redistribute it, or use it as you like.

+3
source

The problem with your current code is that, indeed, var swfArgs does not appear in the source of this webpage. YouTube is constantly changing its pages, so I’ll tell you how to find what you need to do, and not what YouTube is doing right now.

Open the page in a browser with good debugging tools. For example, Firefox with Firebug or Chrome with developer tools. Open developer tools and go to the "Network" tab or whatever your tools call.

Chrome Developer Tools Tab:
Chrome developer tools network tab

Firebug Developer Toolbar:
Firebug network tab

Find the FLV. Find the options in the URL and other related places. See if any of this data is on the web page. Also see if there is any of this data from other requests that may have parameters that are on the web page.

Just re-create the requests in Python and you will get the FLV, just like the official YouTube site does.

+4
source

All Articles