Skip to content

Commit 1fb3399

Browse files
author
kevin.zhang
committed
chore: add stream support for video
1 parent 740960b commit 1fb3399

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ __pycache__/
2121
.svn/
2222

2323
storage/
24+
config.toml

app/controllers/v1/video.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44

55
from fastapi import Request, Depends, Path, BackgroundTasks, UploadFile
6+
from fastapi.responses import FileResponse
67
from fastapi.params import File
78
from loguru import logger
89

@@ -78,7 +79,7 @@ def file_to_uri(file):
7879

7980

8081
@router.delete("/tasks/{task_id}", response_model=TaskDeletionResponse, summary="Delete a generated short video task")
81-
def create_video(request: Request, task_id: str = Path(..., description="Task ID")):
82+
def delete_video(request: Request, task_id: str = Path(..., description="Task ID")):
8283
request_id = base.get_task_id(request)
8384
task = sm.state.get_task(task_id)
8485
if task:
@@ -130,3 +131,13 @@ def upload_bgm_file(request: Request, file: UploadFile = File(...)):
130131
return utils.get_response(200, response)
131132

132133
raise HttpException('', status_code=400, message=f"{request_id}: Only *.mp3 files can be uploaded")
134+
135+
136+
@router.get("/stream/{file_path:path}")
137+
async def stream_video(request: Request, file_path: str):
138+
tasks_dir = utils.task_dir()
139+
video_path = os.path.join(tasks_dir, file_path)
140+
if os.path.isfile(video_path):
141+
return FileResponse(video_path, media_type="video/mp4", filename=file_path)
142+
else:
143+
return {"message": "File not found."}

app/services/state.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def delete_task(self, task_id: str):
4444
# Redis state management
4545
class RedisState(BaseState):
4646

47-
def __init__(self, host='localhost', port=6379, db=0):
47+
def __init__(self, host='localhost', port=6379, db=0, password=None):
4848
import redis
49-
self._redis = redis.StrictRedis(host=host, port=port, db=db)
49+
self._redis = redis.StrictRedis(host=host, port=port, db=db, password=password)
5050

5151
def update_task(self, task_id: str, state: int = const.TASK_STATE_PROCESSING, progress: int = 0, **kwargs):
5252
progress = int(progress)
@@ -98,5 +98,6 @@ def _convert_to_original_type(value):
9898
_redis_host = config.app.get("redis_host", "localhost")
9999
_redis_port = config.app.get("redis_port", 6379)
100100
_redis_db = config.app.get("redis_db", 0)
101+
_redis_password = config.app.get("redis_password", None)
101102

102-
state = RedisState(host=_redis_host, port=_redis_port, db=_redis_db) if _enable_redis else MemoryState()
103+
state = RedisState(host=_redis_host, port=_redis_port, db=_redis_db, password=_redis_password) if _enable_redis else MemoryState()

0 commit comments

Comments
 (0)