【发布时间】:2022-08-10 09:18:08
【问题描述】:
我正在使用 Fast API 从 googlevideo.com 返回视频响应。这是我正在使用的代码:
@app.get(params.api_video_route)
async def get_api_video(url=None):
def iter():
req = urllib.request.Request(url)
with urllib.request.urlopen(req) as resp:
yield from io.BytesIO(resp.read())
return StreamingResponse(iter(), media_type=\"video/mp4\")
但这不起作用
我希望将此 Nodejs 转换为 python FAST API:
app.get(\"/download-video\", function(req, res) {
http.get(decodeURIComponent(req.query.url), function(response) {
res.setHeader(\"Content-Length\", response.headers[\"content-length\"]);
if (response.statusCode >= 400)
res.status(500).send(\"Error\");
response.on(\"data\", function(chunk) { res.write(chunk); });
response.on(\"end\", function() { res.end(); }); }); });
-
什么不工作?你期望会发生什么?您收到任何错误消息吗?你得到了什么样的回应?如果您在调试器中观看该请求是否会返回任何数据(甚至是
print响应?) -
@MatsLindh 它没有返回任何响应,API 一直在加载
-
app.get(\"/download-video\", function(req, res) { http.get(decodeURIComponent(req.query.url), function(response) { res.setHeader(\"Content-Length\", response.headers[\"content-length\"]); if (response.statusCode >= 400) res.status(500).send(\"Error\"); response.on(\"data\", 函数(chunk) { res.write(chunk); }); response.on(\"end\", function() { res.end(); }); }); });这是我在 python fastapi 中转换的 nodejs 代码
-
您是否检查过您对
resp.read()的调用是否获得任何数据?它会被调用吗?urlopen成功了吗? -
@MatsLindh 是的,它正在返回字节,但我想要 mp4/video 格式,这需要很多时间
标签: python video-streaming urllib fastapi streamingresponsebody