I have videos on my Google Cloud Storage. I'm using "Google Transcoder API" to generate HLS (.mpd) and DASH (.mpd) formats for each video.
Now I want to publish these videos on my website (written in React.js) using react-player. But as I figured it out, Google Pre-Signed URLs (just like AWS) are public. And my video content should not be downloadable or available through a URL (event with a expiry).
I want to write a FastAPI API as a proxy server to get request from react-player and stream videos from Google Cloud Storage.
I want to know:
1- How can I download HLS .m3u8 or DASH .mpd files from Google Cloud Storage, in Python.
2- How can I return the downloaded content, from FastAPI to Front-End.
Where both of these operations, follow the streaming standards and do not download the full video at first.
My videos are not live and are pre-recorded and transcoded.
I found this snippet but I don't know if it's OK or not:
from fastapi import FastAPI, Request
from starlette.responses import StreamingResponse
import httpx
app = FastAPI()
CLOUD_STORAGE_BASE_URL = "https://storage.googleapis.com/YOUR_BUCKET_NAME"
async def proxy_video(request: Request):
path = request.url.path
video_url = f"{CLOUD_STORAGE_BASE_URL}{path}"
async with httpx.AsyncClient() as client:
response = await client.get(video_url, stream=True)
response.raise_for_status() # Raise an exception for non-200 responses
return StreamingResponse(
response.aiter_raw(),
status_code=response.status_code,
headers=response.headers,
media_type=response.headers.get("Content-Type"),
)
@app.get("/{path:path}")
async def serve_video(path: str):
return await proxy_video(request)