import yt_dlp
import subprocess

def stream_video(url):
    ydl_opts = {
        'format': 'bestvideo+bestaudio/best',
        'quiet': True,
        'no_warnings': True,
        'noplaylist': True,
        'forceurl': True,
        'skip_download': True,
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        try:
            info = ydl.extract_info(url, download=False)
            if 'url' in info:
                video_url = info['url']
            else:
                # For some videos (like YouTube), it's nested in 'formats'
                formats = info.get('formats', [])
                if formats:
                    video_url = formats[-1]['url']
                else:
                    print("Could not extract video URL.")
                    return

            print("Streaming to mpv...")
            subprocess.run(['mpv', video_url])
        except Exception as e:
            print(f"An error occurred: {e}")

# Example usage
video_url = input("Video URL: ")
stream_video(video_url)
