8000 feat: add play_hls utility · gauravkrp/videodb-python@0944f0d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0944f0d

Browse files
committed
feat: add play_hls utility
1 parent 4f4b7fa commit 0944f0d

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

videodb/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55

66
from typing import Optional
7+
from videodb._utils.video import play_hls
78
from videodb._constants import VIDEO_DB_API
89
from videodb.client import Connection
910
from videodb.exceptions import (
@@ -23,6 +24,7 @@
2324
"AuthenticationError",
2425
"InvalidRequestError",
2526
"SearchError",
27+
"play_hls",
2628
]
2729

2830

videodb/_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
VIDEO_DB_API: str = "https://api.videodb.io"
5+
PLAYER_URL: str = "https://console.videodb.io/player"
56

67

78
class SearchType:

videodb/_utils/video.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import webbrowser as web
22

3+
from videodb._constants import PLAYER_URL
34

4-
def play_url(url: str) -> bool:
5-
opend = web.open(url)
5+
6+
def play_hls(url: str):
7+
"""Play a hls stream url in the browser/ notebook
8+
9+
:param str url: The url of the hls stream
10+
:return: The player url if the stream is opened in the browser or the iframe if the stream is opened in the notebook
11+
"""
12+
player = f"{PLAYER_URL}?url={url}"
13+
opend = web.open(player)
614
if not opend:
715
try:
816
from IPython.display import IFrame
917

1018
player_width = 800
1119
player_height = 400
12-
IFrame(url, player_width, player_height)
20+
return IFrame(player, player_width, player_height)
1321
except ImportError:
14-
return False
15-
return True
22+
return player
23+
return player

videodb/search.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from videodb._utils.video import play_url
2+
from videodb._utils.video import play_hls
33
from videodb._constants import (
44
SearchType,
55
ApiPath,
@@ -86,8 +86,7 @@ def play(self) -> str:
8686
:rtype: str
8787
"""
8888
self.compile()
89-
play_url(self.player_url)
90-
return self.player_url
89+
return play_hls(self.stream_url)
9190

9291

9392
class Search(ABC):

videodb/shot.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
from typing import Optional
5-
from videodb._utils.video import play_url
5+
from videodb._utils.video import play_hls
66
from videodb._constants import (
77
ApiPath,
88
)
@@ -78,5 +78,4 @@ def play(self) -> str:
7878
:rtype: str
7979
"""
8080
self.generate_stream()
81-
play_url(self.player_url)
82-
return self.player_url
81+
return play_hls(self.stream_url)

videodb/video.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Optional
2-
from videodb._utils.video import play_url
2+
from videodb._utils.video import play_hls
33
from videodb._constants import (
44
ApiPath,
55
SearchType,
@@ -84,9 +84,7 @@ def generate_stream(self, timeline: Optional[list[tuple[int, int]]] = None) -> s
8484
"length": self.length,
8585
},
8686
)
87-
self.stream_url = stream_data.get("stream_url")
88-
self.player_url = stream_data.get("player_url")
89-
return self.stream_url
87+
return stream_data.get("stream_url", None)
9088

9189
def generate_thumbnail(self):
9290
if self.thumbnail_url:
@@ -137,9 +135,7 @@ def add_subtitle(self) -> str:
137135
"type": Workflows.add_subtitles,
138136
},
139137
)
140-
self.stream_url = subtitle_data.get("stream_url")
141-
self.player_url = subtitle_data.get("player_url")
142-
return self.stream_url
138+
return subtitle_data.get("stream_url", None)
143139

144140
def insert_video(self, video, timestamp: float) -> str:
145141
"""Insert a video into another video
@@ -178,15 +174,12 @@ def insert_video(self, video, timestamp: float) -> str:
178174
for shot in all_shots
179175
],
180176
)
181-
self.stream_url = compile_data.get("stream_url")
182-
self.player_url = compile_data.get("player_url")
183-
return self.stream_url
177+
return compile_data.get("stream_url", None)
184178

185179
def play(self) -> str:
186180
"""Open the player url in the browser/iframe and return the stream url
187181
188182
:return: The stream url
189183
:rtype: str
190184
"""
191-
play_url(self.player_url)
192-
return self.player_url
185+
return play_hls(self.player_url)

0 commit comments

Comments
 (0)
0