Open In App

Download Facebook video using Python

Last Updated : 23 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to create a user-friendly Python program to download Facebook videos using the video URL. Using Python programming, there are several ways to solve the same problem. we going to use a Python module called ‘youtube_dl’ to achieve the target. 

Description of modules and methods used

Before diving into the solution, let’s take a look at the modules that are required to complete our script. Also, we will learn about the functionalities of all the methods used in our code. 

The chdir() method

The chdir() method is used to change the current working directory. It is used for navigating system directories in python. We can specify the absolute or relative folder path as a parameter to this function. We are using this method to specify the path for downloading the Facebook video.

Syntax: os.chdir(path)

Python youtube_dl module

Youtube_dl is actually a command-line tool that can be called from any programming language. In python, it is available as a module itself. This module is capable of downloading video files from websites. The module comes with several functionalities such as video quality selection,  defining minimum and maximum file size, etc. You can check out all the available options in the options.py file of the module. We have used only one method from this module which is the most important method in our program.

pip install youtube_dl

YoutubeDL() method loads all the options that are fed into it in the form of a python dictionary, which means that the options are present in the form of key-value pairs. 

Syntax: YoutubeDL(arguments in the form of dictionary)

youtube_dl_opts = { ‘source_address’: value1,

        ‘call_home’: value2,

        ‘sleep_interval’: value3,

        ‘max_sleep_interval’: value4,

        ‘external_downloader’: value5,

        ‘list_thumbnails’: value6,

        ‘playlist_items’: value7   }

The download() method

The download method accepts the URL of the video as a parameter. It basically sends an HTTP GET request to download the video from the web to your local machine. Anyways, the youtube_dl module deals with all the underlying concepts and provides abstraction. We just need to write a simple script to feed the URL to this method and provide the necessary options to download the video. 

syntax: download(URL)

Get the link to a Facebook Video

Visit the webpage where the video is found in a browser. Right-click the video or click on the options button to view a list of options.Select copy link to copy the link of the video. This copied link will get saved in the clipboard of your device which can then be used in the program.

Download Facebook video using Python

Getting the link of  a Facebook video

Download the Facebook video using Python

start the program, then Bring all necessary packages into play. To handle any exceptions that may arise while processing the input, use the try and except block. Ask the user for the video URL. Obtain the user’s video resolution. You can allocate options based on user input by using the if and elif blocks. Save in a variable the location where the download file must be saved. Utilizing the available options, download the video. Publish the results. Terminate the program.

Python3




# import section
import sys
import youtube_dl
import os
  
# Using try and except blocks to handle exceptions
try:
    # getting URL from the user
    url = input("Enter the url of the facebook video to be downloaded: ")
    print("Choose a video resolution to start downloading:-")
    print("1. 1080p(HD)")
    print("2. 720p")
    print("3. 480p(SD)")
    print("4. 360p")
    print("5. 180p")
    print("6.Any available resolution")
    # getting value of resolution from user
    option = int(input("Enter your option (1 - 6): "))
    if(option == 1):
        y = {"format": "(mp4,webm)[height<=1080]"}
    elif(option == 2):
        y = {"format": "(mp4,webm)[height<=720]"}
    elif (option == 3):
        y = {"format": "(mp4,webm)[height<=480]"}
    elif (option == 4):
        y = {"format": "(mp4,webm)[height<=360]"}
    elif (option == 5):
        y = {"format": "(mp4,webm)[height<=180]"}
    elif (option == 6):
        y = {}
  
    else:
        print("Option not available")
  
    # setting path to download the video
    path = "C:\py_facebook_videos"
    os.chdir(path)
    # start downloading the video using provided options
    with youtube_dl.YoutubeDL(y) as u:
        print("Downloading........."+url)
        # download the video
        u.download([url])
except:
    print("Invalid link or selected resolution unavailable!")
    sys.exit(1)
  
print("Download completed !!!")


Output:

Download Facebook video using Python

 

Note:

  • Replace the value of the ‘path’ variable in the code with your own path.
  • Before choosing the resolution, check if the video can actually be played in the desired resolution.
  • The scope of the URL is not restricted to Facebook alone, the youtube_dl module supports several other websites including youtube. So don’t miss playing with different URLs.
  • You will find the downloaded video in the provided path after the execution of the program.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads