
import yt_dlp


def get_video_duration(video_url):
    """
    Fetches the duration of a YouTube video in seconds using yt-dlp.

    Args:
        video_url (str): The URL of the YouTube video.

    Returns:
        int: The duration of the video in seconds, or None if there's an error.
    """
    try:
        # Set up the options for yt-dlp
        ydl_opts = {
            'quiet': True,  # Suppress unnecessary output
            'extract_flat': True,  # Only fetch metadata
        }

        # Initialize yt-dlp with the options
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            # Extract video info from the URL
            info_dict = ydl.extract_info(video_url, download=False)
            # Get the duration from the video info
            duration = info_dict.get('duration', None)  # Duration is in seconds
            return duration
    except Exception as e:
        print(f"Error fetching video duration: {e}")
        return None
# print(get_video_duration("https://www.youtube.com/watch?v=61vSzkQqL7Y"))