Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__
psp_at3tool.exe
psp_at3tool
libatrac.so.1.2.0
17 changes: 9 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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')
6 changes: 4 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class atracTypes(str, Enum):
PLUS320 = 'PLUS320'
PLUS352 = 'PLUS352'


bitrates = {
'LP2': 132,
'LP4': 66,
Expand All @@ -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']
Expand All @@ -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