Skip to content

Commit

Permalink
Switch from pafy to streamlink for youtube support (#45)
Browse files Browse the repository at this point in the history
pafy has fallen way out of date and no longer works. streamlink can also be used to fetch HLS (HTTP Live Streaming) URLs for youtube videos. Seems to work fine on streams that pafy did not work with.
  • Loading branch information
tyler-romero authored Nov 12, 2024
1 parent 41d0258 commit c4ea1a9
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 25 deletions.
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "stream"
version = "0.5.0"
version = "0.5.1"
description = "Groundlight Stream Processor - Container for analyzing video using RTSP etc"
readme = "README.md"
requires-python = ">=3.10"
Expand All @@ -11,8 +11,7 @@ dependencies = [
"numpy<2.0.0",
"opencv-python-headless>=4.10.0.84",
"PyYAML==6.0.2",
"youtube-dl==2021.12.17",
"pafy>=0.5.5"
"streamlink==7.0.0",
]

[project.optional-dependencies]
Expand Down
16 changes: 11 additions & 5 deletions src/stream/grabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import cv2
import numpy as np
import pafy
import streamlink

logger = logging.getLogger("groundlight.stream")

Expand Down Expand Up @@ -194,17 +194,23 @@ class YouTubeFrameGrabber(FrameGrabber):

def __init__(self, stream=None):
self.stream = stream
self.video = pafy.new(self.stream)
self.best_video = self.video.getbest(preftype="mp4")
streams = streamlink.streams(self.stream)
if "best" not in streams:
raise ValueError("No available HLS stream for this live video.")
self.best_video = streams["best"]

self.capture = cv2.VideoCapture(self.best_video.url)
logger.debug(f"initialized video capture with backend={self.capture.getBackendName()}")
if not self.capture.isOpened():
raise ValueError(f"could not initially open {self.stream=}")
self.capture.release()

def reset_stream(self):
self.video = pafy.new(self.stream)
self.best_video = self.video.getbest(preftype="mp4")
streams = streamlink.streams(self.stream)
if "best" not in streams:
raise ValueError("No available HLS stream for this live video.")
self.best_video = streams["best"]

self.capture = cv2.VideoCapture(self.best_video.url)
logger.debug(f"initialized video capture with backend={self.capture.getBackendName()}")
if not self.capture.isOpened():
Expand Down
Loading

0 comments on commit c4ea1a9

Please sign in to comment.