Source code to download video python GUI

The above code is a Python GUI program that allows the user to input a URL, find videos on the webpage, display the videos with checkboxes on the GUI, and download the selected videos. The program uses the Tkinter library for the GUI, the requests library to send HTTP requests to the URL, and the BeautifulSoup library to parse the HTML content of the webpage. The program has a main window with a label for the URL input field, an input field for the URL, a “Find Videos” button, and a “Download” button. When the user enters a URL and clicks the “Find Videos” button, the program sends a GET request to the URL and parses the HTML content to find all the video tags on the webpage. It then adds the video URLs to a list and creates a checkbox for each video. The program displays the checkboxes on the GUI and stores the checkbox variables in a separate list. When the user clicks the “Download” button, the program iterates through the list of checkbox variables and checks if each checkbox is checked. If it is checked, it downloads the corresponding video from the list of video URLs using the urllib library. The downloaded video is saved as “video.mp4”  
import tkinter as tk
import requests
from bs4 import BeautifulSoup
import urllib.request
# Set the size of the GUI window
window_size = “800×800”
 
# Create the main window
root = tk.Tk()
root.geometry(window_size)
root.title(“Video Downloader”)
 
# Create the label for the URL input field
url_label = tk.Label(root, text=”Enter URL:”)
url_label.pack()
 
# Create the URL input field
url_entry = tk.Entry(root)
url_entry.pack()
 
# Create the list to store the video URLs and checkbox variables
video_list = []
checkbox_vars = []
 
# Create the “Find Videos” button
def find_videos():
    # Get the URL from the input field
    url = url_entry.get()
 
    # Send a GET request to the URL
    r = requests.get(url)
 
    # Parse the HTML content
    soup = BeautifulSoup(r.content, “html.parser”)
 
    # Find all the video tags
    videos = soup.find_all(“video”)
 
    # Iterate through the videos and add them to the list
    for video in videos:
        src = video[“src”]
        video_list.append(src)
 
        # Create a checkbox variable
        var = tk.IntVar()
 
        # Create a checkbox for the video
        cb = tk.Checkbutton(root, text=src, variable=var)
        cb.pack()
 
        # Add the checkbox variable to the list
        checkbox_vars.append(var)
 
find_videos_button = tk.Button(root, text=”Find Videos”, command=find_videos)
find_videos_button.pack()
 
# Create the “Download Videos” button
def download_videos():
    # Iterate through the list of checkbox variables
    for i, var in enumerate(checkbox_vars):
        # If the checkbox is checked
        if var.get() == 1:
            # Download the corresponding video
            urllib.request.urlretrieve(video_list[i], “video.mp4”)
 
download_button = tk.Button(root, text=”Download”, command=download_videos)
download_button.pack()
 
root.mainloop()
  This method is a part of the urllib library in Python, which provides functions for working with URLs. The urllib library is a built-in library in Python, so you don’t need to install it separately. The urllib.request.urlretrieve() method is used to download a file from the specified URL and save it to the local filesystem. It takes two arguments:
  • url: The URL of the file to be downloaded.
  • filename: The name of the file to be saved.
The method returns a tuple containing the local filename and the headers. For example, the following code downloads a file from the specified URL and saves it as “file.txt”:
urllib.request.urlretrieve(“http://www.example.com/file.txt”, “file.txt”)
To download a video from a URL, you can use the urllib.request.urlretrieve() method, as mentioned in the previous answer. This method downloads the file from the specified URL and saves it to the local filesystem. For example, the following code downloads a video from the specified URL and saves it as “video.mp4”:
urllib.request.urlretrieve(“http://www.example.com/video.mp4”, “video.mp4”) Alternatively, you can use the urllib.request.urlopen() method to download the video file and write it to the local filesystem using the write() method of the io library. For example:
import urllib.request import io # Download the video file response = urllib.request.urlopen(“http://www.example.com/video.mp4”) # Open a local file for writing with open(“video.mp4”, “wb”) as f: # Write the video file to the local file f.write(response.read())
Both of these methods can be used to download a video from a URL and save it to the local filesystem.
 

Recommended Posts