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?