Facebook Video Downloader

How to Build a Facebook Video Downloader using Python

How to Build a Facebook Video Downloader using Python

Hello coder, welcome to the codewithrandom blog. In this blog, we will building a Facebook video downloader using Python with Complete Source Code. Facebook is one of the most popular social media platforms with millions of users worldwide. Sometimes you might want to download a Facebook video to watch it offline or share it with someone who does not have an internet connection. In this blog, we will Build a Facebook video downloader using Python.

Facebook Video Downloader

Here’s a step-by-step guide on how to build a Facebook video downloader using Python:

step 1: open any python code Editor.

step 2: Importing the Required Modules.

For this Facebook video downloader project, we need to install Tkinter  Gui. You can install these packages in your terminal.

$ pip insatll tk 

$ pip3 install -r requirements.txt

step 3: Copy the code for Facebook video downloader using Python, which I provided Below in this article, and save it in a file named “main.py” (or any other name you prefer).

step 4: Run this  python file main.py to start the Facebook video downloader

Facebook Video Downloader

How to Build a YouTube Video Downloader using Python

how to create a web browser Using python

Complete Source Code For the Facebook video downloader (copy the code and run )👇👇👇

# ALL Imports
import time
from tkinter.ttk import *
import tkinter as tk
from requests import get, HTTPError, ConnectionError
from re import findall
from urllib.parse import unquote
from threading import Thread
import queue
from queue import Empty

def Invalid_Url():
    """ Sets Status bar label to error message """
    Status["text"] = "Invalid URL..."
    Status["fg"] = "red"

def get_downloadlink(url):

    url = url.replace("www", "mbasic")
    try:
        r = get(url, timeout=5, allow_redirects=True)
        if r.status_code != 200:
            raise HTTPError
        a = findall("/video_redirect/", r.text)
        if len(a) == 0:
            print("[!] Video Not Found...")
            exit(0)
        else:
            return unquote(r.text.split("?src=")[1].split('"')[0])
    except (HTTPError, ConnectionError):
        print("[x] Invalid URL")
        exit(1)



def Download_vid():

    # Validates Link and download Video
    global Url_Val
    url=Url_Val.get()

    Status["text"]="Downloading"
    Status["fg"]="green"


    # Validating Input

    if not "www.facebook.com" in url:
        Invalid_Url()
        return

    link=get_downloadlink(url)

    start_downloading()

    download_thread=VideoDownload(link)
    download_thread.start()
    monitor(download_thread)



def monitor( download_thread):
    """ Monitor the download thread """
    if download_thread.is_alive():

        try:
            bar["value"]=queue.get(0)
            ld_window.after(10, lambda: monitor(download_thread))
        except Empty:
            pass



class VideoDownload(Thread):

    def __init__(self, url):
        super().__init__()

        self.url = url

    def run(self):
        """ download video"""

        # save the picture to a file
        block_size = 1024  # 1kB
        r = get(self.url, stream=True)
        total_size = int(r.headers.get("content-length"))

        with open('video.mp4', 'wb') as file:
            totaldata=0;
            for data in r.iter_content(block_size):
                totaldata+=len(data)
                per_downloaded=totaldata*100/total_size
                queue.put(per_downloaded)
                bar['value'] = per_downloaded
                file.write(data)
                time.sleep(0.01)
            file.close()    
            print("Download Finished")

        print("Download Complete !!!")
        Status["text"] = "Finished!!"
        Status["fg"] = "green"



#start download
def start_downloading():
   bar["value"]=0;

# GUI

ld_window=tk.Tk()
ld_window.title("Facebook Video Downloader")
ld_window.geometry("400x300")

# Label for URL Input
input_label= tk.Label(ld_window,text="Enter Facebook Video URL:")
input_label.pack()

# Input of URL
Url_Val = tk.StringVar()
Url_Input = tk.Entry(ld_window, textvariable=Url_Val, font=("Calibri", 9))
Url_Input.place( x=25,y=50, width=350)

# Button for Download
Download_button = tk.Button(ld_window, text="Download", font=("Calibri", 9), command=Download_vid)
Download_button.place(x=100, y=100, width=200)

# Progress Bar
bar = Progressbar(ld_window, length=350, style='grey.Horizontal.TProgressbar',mode='determinate')
bar.place(y=200,width=350,x=25)

queue=queue.Queue()
# Text for Status of Downloading
Status = tk.Label(ld_window, text="Hello!! :D", fg="blue", font=("Calibri", 9), bd=1, relief=tk.SUNKEN, anchor=tk.W, padx=3)
Status.pack(side=tk.BOTTOM, fill=tk.X)

ld_window.mainloop()

Output👇👇

Facebook Video Downloader

conclusion

Hurray! You have successfully Build a Facebook Video Downloader project using the Python. developing a Facebook Video Downloader using Python can be a useful tool for those who want to download videos from Facebook for offline viewing or sharing.

Overall, developing a Facebook Video Downloader using Python can be a great learning experience for those interested in web scraping and automation. Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝



Leave a Reply