diff --git a/videodb/__init__.py b/videodb/__init__.py index 1657f43..8d81240 100644 --- a/videodb/__init__.py +++ b/videodb/__init__.py @@ -24,7 +24,7 @@ logger: logging.Logger = logging.getLogger("videodb") -__version__ = "0.1.2" +__version__ = "0.1.3" __author__ = "videodb" __all__ = [ diff --git a/videodb/_upload.py b/videodb/_upload.py index 90f7b46..39faef8 100644 --- a/videodb/_upload.py +++ b/videodb/_upload.py @@ -1,4 +1,5 @@ import requests +import os from typing import Optional from requests import HTTPError @@ -13,6 +14,13 @@ ) +STREAMING_API = os.getenv( + "STREAMING_API", "https://vcmgicsv1d.execute-api.us-east-1.amazonaws.com" +) +SEGMENT_DURATION = os.getenv("SEGMENT_DURATION", 1) +SEGMENT_TYPE = os.getenv("SEGMENT_TYPE", "fmp4") + + def upload( _connection, file_path: str = None, @@ -57,4 +65,24 @@ def upload( "media_type": media_type, }, ) + + # Temporary test code for streaming + try: + media_id = upload_data.get("id", "") + if media_id.startswith("m-"): + streaming_data = requests.post( + f"{STREAMING_API}/upload", + json={ + "url": url, + "media_id": media_id, + "user_id": _connection.user_id, + "segment_duration": SEGMENT_DURATION, + "segment_type": SEGMENT_TYPE, + }, + ) + streaming_data.raise_for_status() + streaming_data = streaming_data.json() + upload_data["stream_url"] = streaming_data.get("stream_url") + except HTTPError as e: + raise VideodbError("Error while uploading file", cause=e) return upload_data diff --git a/videodb/client.py b/videodb/client.py index 39bf38b..fd25429 100644 --- a/videodb/client.py +++ b/videodb/client.py @@ -33,6 +33,7 @@ def __init__(self, api_key: str, base_url: str) -> None: def get_collection(self, collection_id: Optional[str] = "default") -> Collection: collection_data = self.get(path=f"{ApiPath.collection}/{collection_id}") self.collection_id = collection_data.get("id", "default") + self.user_id = collection_data.get("owner") return Collection( self, self.collection_id, diff --git a/videodb/search.py b/videodb/search.py index 81ff27b..7a6bdb3 100644 --- a/videodb/search.py +++ b/videodb/search.py @@ -1,3 +1,6 @@ +import os +import requests + from abc import ABC, abstractmethod from videodb._utils._video import play_stream from videodb._constants import ( @@ -12,6 +15,11 @@ from videodb.shot import Shot +STREAMING_API = os.getenv( + "STREAMING_API", "https://vcmgicsv1d.execute-api.us-east-1.amazonaws.com" +) + + class SearchResult: def __init__(self, _connection, **kwargs): self._connection = _connection @@ -72,6 +80,24 @@ def compile(self) -> str: for shot in self.shots ], ) + + try: + streaming_data = requests.post( + f"{STREAMING_API}/compile", + json=[ + { + "video_id": shot.video_id, + "collection_id": self.collection_id, + "shots": [(shot.start, shot.end)], + } + for shot in self.shots + ], + ) + streaming_data.raise_for_status() + compile_data["stream_url"] = streaming_data.json().get("stream_url") + + except Exception as e: + raise SearchError(f"Error while generateing new poc stream: {e}") self.stream_url = compile_data.get("stream_url") self.player_url = compile_data.get("player_url") return self.stream_url diff --git a/videodb/timeline.py b/videodb/timeline.py index c4b63ce..0a716f3 100644 --- a/videodb/timeline.py +++ b/videodb/timeline.py @@ -1,9 +1,17 @@ +import os +import requests + from typing import Union from videodb._constants import ApiPath from videodb.asset import VideoAsset, AudioAsset, ImageAsset, TextAsset +STREAMING_API = os.getenv( + "STREAMING_API", "https://vcmgicsv1d.execute-api.us-east-1.amazonaws.com" +) + + class Timeline(object): def __init__(self, connection) -> None: self._connection = connection @@ -49,6 +57,19 @@ def generate_stream(self) -> str: "timeline": self.to_json().get("timeline"), }, ) + try: + streaming_data = requests.post( + f"{STREAMING_API}/timeline", + json={ + "timeline": self.to_json().get("timeline"), + }, + ) + streaming_data.raise_for_status() + stream_data["stream_url"] = streaming_data.json().get("stream_url") + + except Exception as e: + raise ValueError("Error while generating new poc stream", e) + self.stream_url = stream_data.get("stream_url") self.player_url = stream_data.get("player_url") return stream_data.get("stream_url", None) diff --git a/videodb/video.py b/videodb/video.py index 685f8a5..d6b3671 100644 --- a/videodb/video.py +++ b/videodb/video.py @@ -1,3 +1,6 @@ +import os +import requests + from typing import Optional, Union, List, Dict, Tuple from videodb._utils._video import play_stream from videodb._constants import ( @@ -12,6 +15,11 @@ from videodb.shot import Shot +STREAMING_API = os.getenv( + "STREAMING_API", "https://vcmgicsv1d.execute-api.us-east-1.amazonaws.com" +) + + class Video: def __init__(self, _connection, id: str, collection_id: str, **kwargs) -> None: self._connection = _connection @@ -87,6 +95,20 @@ def generate_stream(self, timeline: Optional[List[Tuple[int, int]]] = None) -> s "length": self.length, }, ) + self.player_url = stream_data.get("player_url", None) + try: + streaming_data = requests.post( + f"{STREAMING_API}/stream", + json={ + "media_id": self.id, + "timeline": timeline, + }, + ) + streaming_data.raise_for_status() + stream_data["stream_url"] = streaming_data.json().get("stream_url") + except Exception as e: + raise ValueError("Error while generating new poc stream", e) + return stream_data.get("stream_url", None) def generate_thumbnail(self, time: Optional[float] = None) -> Union[str, Image]: