Python code to download video from url

If you have access to urllib2 , you can use urlopen on the url , this would give back a response object , you can do response.read() to read the data and then write it to a file.

Example -

import urllib2
dwn_link = 'https://class.coursera.org/textanalytics-001/lecture/download.mp4?lecture_id=73'

file_name = 'trial_video.mp4' 
rsp = urllib2.urlopen(dwn_link)
with open(file_name,'wb') as f:
    f.write(rsp.read())

Also you need to make sure that you have authenticated to the server , if that is required for downloading the video.

I am not sure what kind of authentication coursera.org uses, but if its Basic HTTP Authentication (Which I highly doubt) , you can use -

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://class.coursera.org/"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)

# use the opener to fetch a URL
opener.open(dwn_link)

https://www.youtube.com/watch?v=Ige-YPaQQRQ&feature=youtu.be

In one of our previous tutorial we learnt to download videos from YouTube. We used a custom library called pytube3 for it. But what if we want to download videos using python from any other website? We can’t use pytube3 there nor can we have custom libraries for every website.

So to download videos from any website we will have to use our web scrapping libraries BeautifulSoup and Requests. In this tutorial we will learn how we can download videos from any website using our web scraping skills.

We will go to University of Munich’s website and download the videos. This website contains videos as well as some pdf’s and other files, we will only download videos. 

Python code to download video from url

If you notice carefully you can see that all the videos have mp4 extension, which is what we have to look for. Moreover all the files have an embedded link from where they can be downloaded. We can find all these links and then download files:

Python code to download video from url

Let’s get to the code:

import requests
from bs4 import BeautifulSoup
# specify the URL of the archive here
archive_url = "http://www-personal.umich.edu/~csev/books/py4inf/media/"
 
def get_video_links():
  #create response object
  r = requests.get(archive_url)
  #create beautiful-soup object
  soup = BeautifulSoup(r.content,'html5lib')
  #find all links on web-page
  links = soup.findAll('a')
  #filter the link ending with .mp4
  video_links = [archive_url + link['href'] for link in links if link['href'].endswith('mp4')]
 
  return video_links

Now that we have grabbed the links we can send get request to these links and download videos as below:

def download_video_series(video_links):
 
  for link in video_links:
    
   # iterate through all links in video_links
    # and download them one by one
    #obtain filename by splitting url and getting last string
    file_name = link.split('/')[-1]  
 
    print ("Downloading file:%s"%file_name)
 
    #create response object
    r = requests.get(link, stream = True)
 
    #download started
    with open(file_name, 'wb') as f:
      for chunk in r.iter_content(chunk_size = 1024*1024):
        if chunk:
          f.write(chunk)
 
    print ("%s downloaded!\n"%file_name)
 
  print ("All videos downloaded!")
  return
 
if __name__ == "__main__":
  #getting all video links
  video_links = get_video_links()
 
  #download all videos
  download_video_series(video_links)

Output:

Python code to download video from url

You can find the downloaded videos in your working directory. Know more ways to download videos using python from website.


Python provides different modules like urllib, requests etc to download files from the web. I am going to use the request library of python to efficiently download files from the URLs.

Let’s start a look at step by step procedure to download files using URLs using request library−

1. Import module

import requests
url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url, allow_redirects=True)

3. Save the content with name.

open('facebook.ico', 'wb').write(r.content)

save the file as facebook.ico.

Example

import requests


url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url, allow_redirects=True)

open('facebook.ico', 'wb').write(r.content)

Result

Python code to download video from url

We can see the file is downloaded(icon) in our current working directory.

But we may need to download different kind of files like image, text, video etc from the web. So let’s first get the type of data the url is linking to−

>>> r = requests.get(url, allow_redirects=True)
>>> print(r.headers.get('content-type'))
image/png

However, there is a smarter way, which involved just fetching the headers of a url before actually downloading it. This allows us to skip downloading files which weren’t meant to be downloaded.

>>> print(is_downloadable('https://www.youtube.com/watch?v=xCglV_dqFGI'))
False
>>> print(is_downloadable('https://www.facebook.com/favicon.ico'))
True

To restrict the download by file size, we can get the filezie from the content-length header and then do as per our requirement.

contentLength = header.get('content-length', None)
if contentLength and contentLength > 2e8: # 200 mb approx
return False

Get filename from an URL

To get the filename, we can parse the url. Below is a sample routine which fetches the last string after backslash(/).

url= "http://www.computersolution.tech/wp-content/uploads/2016/05/tutorialspoint-logo.png"
if url.find('/'):
print(url.rsplit('/', 1)[1]

Above will give the filename of the url. However, there are many cases where filename information is not present in the url for example – http://url.com/download. In such a case, we need to get the Content-Disposition header, which contains the filename information.

import requests
import re

def getFilename_fromCd(cd):
"""
Get filename from content-disposition
"""
if not cd:
return None
fname = re.findall('filename=(.+)', cd)
if len(fname) == 0:
return None
return fname[0]


url = 'http://google.com/favicon.ico'
r = requests.get(url, allow_redirects=True)
filename = getFilename_fromCd(r.headers.get('content-disposition'))
open(filename, 'wb').write(r.content)

The above url-parsing code in conjunction with above program will give you filename from Content-Disposition header most of the time.

Python code to download video from url

Updated on 30-Jul-2019 22:30:26

  • Related Questions & Answers
  • Downloading file using SAP .NET Connector
  • How are files extracted from a tar file using Python?
  • Rename multiple files using Python
  • Using SAP Web Service from WSDL file
  • Web Scraping using Python and Scrapy?
  • Python Implementing web scraping using lxml
  • How to copy files from one folder to another using Python?
  • How to copy files from one server to another using Python?
  • How to convert PDF files to Excel files using Python?
  • How to copy certain files from one folder to another using Python?
  • Implementing web scraping using lxml in Python?
  • Does HTML5 allow you to interact with local client files from within a web browser?
  • Generate temporary files and directories using Python
  • How to remove swap files using Python?
  • How to create powerpoint files using Python

How do I download a video from Python?

Downloading files from web using Python?.
Import module. import requests..
Get the link or url. url = 'https://www.facebook.com/favicon.ico' r = requests.get(url, allow_redirects=True).
Save the content with name. open('facebook.ico', 'wb').write(r.content) ... .
Get filename from an URL. To get the filename, we can parse the url..

How do I download a URL in Python?

To download a file from a URL using Python follow these three steps:.
Install requests module and import it to your project..
Use requests. get() to download the data behind that URL..
Write the file to a file in your system by calling open()..

How do I download Chrome videos using Python?

How to download a file using Selenium and Python.
Prerequisites:.
Step 1: Import required packages to Python test script..
Step 2: Set Chrome options..
Step 3: Create chrome driver object with options..
Step 4: Create a script to navigate to the website and click on download .csv..
Step 5: Run the test..

How do I automatically download a file from a website using Python?

“how to automate downloading a file from a website using python” Code Answer's.
import urllib. request..
pdf_path = "".
def download_file(download_url, filename):.
response = urllib. request. urlopen(download_url).
file = open(filename + ".pdf", 'wb').
file. write(response. read()).
file. close().