Skip to content

Commit

Permalink
feat(render): Add fps and quality options to replay rendering
Browse files Browse the repository at this point in the history
- Added `fps` option to control the frames per second for replay processing.
- Added `quality` option to specify the quality of the output video.
- Updated temporary work directory path for replay processing.
  • Loading branch information
SakuraIsayeki committed Jul 21, 2023
1 parent 52ded6b commit 8b0bef4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
4 changes: 3 additions & 1 deletion wowskarma_api_minimap/src/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ echo = false

[default.replay]
max_file_size = 5242880 # Max replay file size in bytes. Default is 5MB
temp_workdir = "/tmp/wowskarma/minimap" # Temporary directory for replay processing
temp_workdir = "/tmp/wows-karma/minimap" # Temporary directory for replay processing
fps = 15 # Frames per second for replay processing
quality = 5 # Quality of the output video. 1 is the lowest quality, 9 is the highest
39 changes: 25 additions & 14 deletions wowskarma_api_minimap/src/routes/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import os

import pathlib
from asyncio.log import logger

from fastapi import APIRouter, HTTPException, status, UploadFile, BackgroundTasks
from fastapi.logger import logger
from fastapi.responses import FileResponse
from renderer.data import ReplayData
from renderer.render import Renderer
Expand All @@ -18,11 +18,13 @@

router = APIRouter()


def on_job_finished(job_id: str, filepath):
logger.info(f"Render job {job_id} finished. Filesize: {os.stat(filepath).st_size / 1024 / 1024:.2f} MiB")
os.remove(filepath)


@router.post("/render", dependencies=[AuthenticatedUser])
async def render_replay(
def render_replay(
file: UploadFile,
background_tasks: BackgroundTasks,
replay_id: str | None = None,
Expand All @@ -43,28 +45,37 @@ async def render_replay(
)

# Log the new job.
job_id = binascii.b2a_hex(os.urandom(7))
job_id = str(binascii.b2a_hex(os.urandom(7))).split("'")[1]
logger.info(f"Started new render job: Job ID: {job_id}, Replay ID: {replay_id or 'None'}, "
f"Filename: {file.filename}.")

# Ensure the work folder exists, if not, create it.
os.makedirs(settings.replay.temp_workdir, exist_ok=True)

# Render time!
filepath = pathlib.Path(settings.replay.temp_workdir, f"{job_id}.mp4")
replay_info = ReplayParser(file.file, True).get_info()
replay_data: ReplayData = replay_info["hidden"]["replay_data"]

# Concat filepaths to get the full path to the replay file.


filepath = pathlib.Path(settings.replay.temp_workdir, f"{job_id}.mp4")
renderer = Renderer(
replay_data,
replay_data=replay_data,
enable_chat=True,
team_tracers=True,
target_player_id=target_player_id
target_player_id=target_player_id,
)

renderer.start(
path=str(filepath),
quality=settings.replay.quality,
fps=settings.replay.fps
)
background_tasks.add_task(on_job_finished, job_id, filepath)

# Log the finished job.
logger.info(f"Render job {job_id} finished. Filesize: {os.stat(filepath).st_size / 1024 / 1024:.2f} MiB")

return FileResponse(
path=filepath,
media_type="video/mp4",
filename=f"{replay_id or 'replay'}.mp4"
)
renderer.start(str(filepath), quality=9, fps=30)

background_tasks.add_task(os.remove, filepath)
return FileResponse(filepath, media_type="video/mp4", filename=f"{replay_id or 'replay'}.mp4")

0 comments on commit 8b0bef4

Please sign in to comment.