Python youtube link to mp4

import tkinter as tk
import customtkinter as ctk
from pytube import YouTube

def Download(url_var):   
    try:
        ytlink = link.get(url_var)
        ytObject = YouTube(ytlink)
        video = ytObject.streams.get_highest_resolution(url_var)
        video.download(video) 
    except:
        print("Invalid link") 
    print("Download Compleate")
ctk.set_appearance_mode("System")
ctk.set_default_color_theme("blue")

app = ctk.CTk()
app.geometry("720x480")
app.title("YouTube Download")

url_var = tk.StringVar()
title = ctk.CTkLabel(app, text="Insert a YouTube link")
title.pack(padx=10, pady=10)


link = ctk.CTkEntry(app, width=350, height=40, textvariable=url_var)
link.pack()

download = ctk.CTkButton(app, text="Download", command=Download)
download.pack()

app.mainloop()

It should work, but it gives me this error:
File “C:\Users\ХарисВМладенов\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\customtkinter\windows\widgets\ctk_button.py”, line 553, in _clicked
self._command()
TypeError: Download() missing 1 required positional argument: ‘url_var’

The error message suggests that the Download() function is missing a required argument. Looking at the function definition, it appears that the url_var parameter is not being passed to the function correctly.

In the line download = ctk.CTkButton(app, text="Download", command=Download), the Download function is being passed as the command argument to the ctk.CTkButton constructor. However, the function is being passed without any arguments, which means that the url_var parameter is not being passed to the function.

To fix this, you can modify the Download() function to take no arguments and to retrieve the value of url_var directly from the url_var object defined earlier. Here is the modified Download() function:

def Download():
    try:
        ytlink = url_var.get()
        ytObject = YouTube(ytlink)
        video = ytObject.streams.get_highest_resolution()
        video.download()
    except:
        print("Invalid link")
    print("Download Complete")

In this modified version of Download(), the url_var.get() method is used to retrieve the value of url_var directly. The get_highest_resolution() and download() methods are called on the video object without any arguments, which means that the highest-resolution video will be downloaded to the current working directory.

After making this change, the Download() function should be able to retrieve the value of url_var correctly and the TypeError should be resolved. Let me know if that works.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.