diff --git a/.gitignore b/.gitignore index 3bd00a3..a34fa23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ -psp_at3tool.exe +psp_at3tool +libatrac.so.1.2.0 diff --git a/Dockerfile b/Dockerfile index 0931c8a..d4b84b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,15 +9,16 @@ RUN mv ffmpeg-build/artifacts/ffmpeg-*-linux-gnu/bin/ffmpeg . FROM python:3.11-slim -ENV WINEPREFIX="/wine32" -ENV WINEARCH=win32 -ENV LOG_LEVEL= -RUN dpkg --add-architecture i386 -RUN apt-get update && apt-get install -y wine32 wine:i386 --no-install-recommends -RUN apt-get clean -RUN /usr/bin/wine wineboot | true COPY --from=builder /root/ffmpeg /usr/bin/ffmpeg -COPY psp_at3tool.exe . + +# Necessary to run at3tool +RUN apt-get update && apt-get install -y gcc-multilib +COPY libatrac.so.1.2.0 /usr/local/lib +RUN ldconfig +RUN ln -s /usr/local/lib/libatrac.so.1 /usr/local/lib/libatrac.so +ENV LD_LIBRARY_PATH /usr/local/lib +COPY psp_at3tool /usr/bin/at3tool + COPY requirements.txt . RUN pip install -r requirements.txt RUN mkdir /uploads diff --git a/main.py b/main.py index 77eac5f..043468d 100644 --- a/main.py +++ b/main.py @@ -5,13 +5,15 @@ from pathlib import Path from fastapi.middleware.cors import CORSMiddleware from tempfile import gettempdir, NamedTemporaryFile -from utils import * +from utils import atracTypes, remove_file, do_encode from typing import Union api = FastAPI( title="ATRAC API" ) logger = logging.getLogger("uvicorn.info") + + @api.on_event("startup") async def startup_event(): api.add_middleware( @@ -21,13 +23,13 @@ async def startup_event(): allow_methods=["*"], allow_headers=["*"], ) - subprocess.run(['/usr/bin/wineserver', '-p']) @api.get("/") async def root(): return RedirectResponse("/docs") + @api.post('/encode') def encode_atrac(type: atracTypes, background_tasks: BackgroundTasks, file: UploadFile = File()): global logger @@ -39,6 +41,7 @@ def encode_atrac(type: atracTypes, background_tasks: BackgroundTasks, file: Uplo background_tasks.add_task(remove_file, output, logger) return FileResponse(path=output, filename=Path(filename).stem + '.at3', media_type='audio/wav') + @api.post('/transcode') def transcode_atrac(type: atracTypes, background_tasks: BackgroundTasks, applyReplaygain: bool = False, loudnessTarget: Union[float, None] = Query(default=None, ge=-70, le=-5), file: UploadFile = File()): global logger @@ -64,13 +67,14 @@ def transcode_atrac(type: atracTypes, background_tasks: BackgroundTasks, applyRe *transcoderCommands, intermediary], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) logger.info(transcoder.stdout.decode('utf-8', errors='ignore')) - + logger.info("Starting at3tool...") output = do_encode(intermediary, type, logger) background_tasks.add_task(remove_file, output, logger) background_tasks.add_task(remove_file, intermediary, logger) return FileResponse(path=output, filename=Path(filename).stem + '.at3', media_type='audio/wav') + @api.post('/decode') def decode_atrac(background_tasks: BackgroundTasks, file: UploadFile = File()): global logger @@ -79,8 +83,9 @@ def decode_atrac(background_tasks: BackgroundTasks, file: UploadFile = File()): output = Path(gettempdir(), str(uuid4())).absolute() with NamedTemporaryFile() as input: shutil.copyfileobj(file.file, input) - encoder = subprocess.run(['/usr/bin/wine', 'psp_at3tool.exe', '-d', - Path(input.name), + encoder = subprocess.run([ + '/usr/bin/at3tool', '-d', + Path(input.name), output]) background_tasks.add_task(remove_file, output, logger) return FileResponse(path=output, filename=Path(filename).stem + '.wav', media_type='audio/wav') diff --git a/utils.py b/utils.py index 03ca2a3..ddf88c7 100644 --- a/utils.py +++ b/utils.py @@ -19,6 +19,7 @@ class atracTypes(str, Enum): PLUS320 = 'PLUS320' PLUS352 = 'PLUS352' + bitrates = { 'LP2': 132, 'LP4': 66, @@ -34,6 +35,7 @@ class atracTypes(str, Enum): 'PLUS352': 352 } + def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ['wav', 'at3'] @@ -46,7 +48,7 @@ def remove_file(filename, logger): def do_encode(input, type, logger): output = Path(gettempdir(), str(uuid4())).absolute() - subprocess.run(['/usr/bin/wine', 'psp_at3tool.exe', '-e', '-br', str(bitrates[type]), - input, + subprocess.run(['/usr/bin/at3tool', '-e', '-br', str(bitrates[type]), + input, output]) return output