summaryrefslogtreecommitdiff
path: root/python/ytdl/live.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/ytdl/live.py')
-rw-r--r--python/ytdl/live.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/python/ytdl/live.py b/python/ytdl/live.py
new file mode 100644
index 0000000..eaa78cf
--- /dev/null
+++ b/python/ytdl/live.py
@@ -0,0 +1,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)