Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code not working ('>' not supported between instances of 'NoneType' and 'int') #774

Open
Fideier264 opened this issue Jul 14, 2024 · 2 comments

Comments

@Fideier264
Copy link

image

This is the issue that comes. Please can somebody help, saw the code here on github and thought it is cool and wanted to try it.

Full code: `import yt_dlp

def download_youtube_video(video_url):
"""Downloads a YouTube video using yt-dlp."""

ydl_opts = {
    "outtmpl": "%(title)s.%(ext)s",  # Output filename template
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(video_url, download=False)
    formats = info_dict.get("formats", None)

    # Print all the available formats and ask the user to select
    for f in formats:
        print(f"{f['format_id']}:\t{f['ext']} ({f.get('format_note', None)}p)")

    resolution_choice = input(
        "Do you want to select from the available options? (y/n): "
    )

    if resolution_choice.lower() == "y":
        format_id = input("Enter the format id of the video: ")
        ydl_opts["format"] = format_id
    else:
        # Select the highest resolution format
        highest_resolution = max(formats, key = lambda x: x.get("height", 0))
        format_id = highest_resolution["format_id"]
        ydl_opts["format"] = format_id

    # Download the video
    ydl.download([video_url])
    print("Download complete using yt-dlp!")

if name == "main":
video_url = input("Enter the URL: ")
download_youtube_video(video_url)`

@vishnuvarshan007
Copy link

You can use this Mate !

import yt_dlp

def download_youtube_video(video_url):
"""Downloads a YouTube video using yt-dlp."""

ydl_opts = {
    "outtmpl": "%(title)s.%(ext)s",  # Output filename template
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(video_url, download=False)
    formats = info_dict.get("formats", None)

    # Print all the available formats and ask the user to select
    for f in formats:
        print(f"{f['format_id']}:\t{f['ext']} ({f.get('format_note', None)}p)")

    resolution_choice = input(
        "Do you want to select from the available options? (y/n): "
    )

    if resolution_choice.lower() == "y":
        format_id = input("Enter the format id of the video: ")
        ydl_opts["format"] = format_id
    else:
        # Select the highest resolution format
        highest_resolution = max(formats, key=lambda x: x.get("height", 0))
        format_id = highest_resolution["format_id"]
        ydl_opts["format"] = format_id

    # Download the video
    ydl.download([video_url])
    print("Download complete using yt-dlp!")

if name == "main":
video_url = input("Enter the URL: ")
download_youtube_video(video_url)

@IsraelCodeMaster
Copy link

Olá, percebi que você estava tendo problemas com o código. Fiz algumas melhorias que devem resolver o problema de seleção de alta resolução e a atualização das opções ydl_opts. Aqui está o código atualizado:

import yt_dlp

def download_youtube_video(video_url):
    """Downloads a YouTube video using yt-dlp."""
    ydl_opts = {
        "outtmpl": "%(title)s.%(ext)s",  # Output filename template
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info_dict = ydl.extract_info(video_url, download=False)
        formats = info_dict.get("formats", None)
        # Print all the available formats and ask the user to select
        for f in formats:
            print(f"{f['format_id']}:\t{f['ext']} ({f.get('format_note', None)}p)")
        resolution_choice = input("Do you want to select from the available options? (y/n): ")
        if resolution_choice.lower() == "y":
            format_id = input("Enter the format id of the video: ")
            ydl_opts["format"] = format_id
        else:
            # Select the highest resolution format
            highest_resolution = max(formats, key=lambda x: (x.get("height") or 0))
            format_id = highest_resolution["format_id"]
            ydl_opts["format"] = format_id

        # Update options with selected format
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([video_url])
        print("Download complete using yt-dlp!")

if __name__ == "__main__":
    video_url = input("Enter the URL: ")
    download_youtube_video(video_url)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants