Error using yt-dlp to get the length of a YouTube video

What I’m trying to do:
I just made a simple program to modify a YouTube url, where I can give the start and stop time of a YT video. It works fine, but I wanted to check if the asked start or stop time is not exceeding the video length, before applying this modification. I tested this return of the video length (by importing the library yt-dlp) on Colab and it works fine. I wanted to do the same on the Anil server side, but this gives me the following error : An error occurred: ERROR: [youtube] Or7aNnrLF6s: Sign in to confirm you are not a bot. This helps protect our community. Learn more

Colab program :

#!pip install yt-dlp
import yt_dlp

def get_video_length(video_url):
    ydl = yt_dlp.YoutubeDL({'quiet': True})  # Suppress output
    try:
        info_dict = ydl.extract_info(video_url, download=False)
        return info_dict.get('duration')
    except yt_dlp.utils.DownloadError as e:
        print(f"An error occurred: {e}")
        return None

video_url = "https://youtu.be/Or7aNnrLF6s?si=C2Lm29vUi9RKJM65"  
video_length = get_video_length(video_url)

if video_length:
    print(f"The video length is: {video_length} seconds")

response : The video length is: 598 seconds
This seems correct and works
Here my Anvil server code

@anvil.server.callable
def get_video_length(base_url):
    ydl = yt_dlp.YoutubeDL({'quiet': True})  # Suppress output
    try:
        info_dict = ydl.extract_info(base_url, download=False)
        print(info_dict)
        return info_dict.get('duration')
    except yt_dlp.utils.DownloadError as e:
        print(f"An error occurred: {e}")
        return None

So can somebody help me with this issue?
Or is there a better way to define the length of a YouTube video?

Has somebody already build a YouTube downloader with Anvil ?
I think I continuously have permissions problems, because it works on Colab, but on Anvil I get always the same error, doesn’t matter what library I use.
An error occurred: ERROR: [youtube] lN7LbxK2ZtU: Sign in to confirm you’re not a bot. This helps protect our community. Learn more
Can somebody help

It looks like YouTube is better at checking that you are not a bot than your bot at pretending not to be one. :slight_smile:

Sorry, I’ve never tried. Perhaps scraping a page with some info about that video?

Thank you for your response, but I tried it with several YouTube url’s and I got always the same error message. Would it help if I login at Anvil as a Google user, or do you think I need a real YouTube API, to pass this error. All my scripts work at Google Colab.

I don’t think you are allowed to download Youtube videos willy-nilly and it might be violating their rules…

But, just to embed videos, you can set start and end times without worrying about the duration. If the beginning/end values are past the end of the video it will just end when the video ends:

Ok thank you, that part adding start and stop time is working correctly.
But this is a bit small function for my YouTube anvil app and I thought, I can add more functionality to this app by adding info and download the video’s that I want to keep, although I pay for YouTube premium and I can download video’s offline, but not on my disk. That is the reason.