summaryrefslogtreecommitdiff
path: root/python/ytdl/live.py
blob: eaa78cf07f250c551d4987986a05c47a857c8ff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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)