-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (77 loc) · 3.22 KB
/
main.py
File metadata and controls
86 lines (77 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import subprocess, logging, shutil
from uuid import uuid4
from fastapi import FastAPI, File, UploadFile, BackgroundTasks, HTTPException, Query
from fastapi.responses import FileResponse, RedirectResponse
from pathlib import Path
from fastapi.middleware.cors import CORSMiddleware
from tempfile import gettempdir, NamedTemporaryFile
from utils import *
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(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
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
filename = file.filename
logger.info(f"Beginning encode for {filename}")
with NamedTemporaryFile() as input:
shutil.copyfileobj(file.file, input)
output = do_encode(input.name, type, logger)
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
filename = file.filename
logger.info(f"Beginning encode for {filename}")
transcoderCommands = []
if loudnessTarget is not None:
transcoderCommands.append(f'-filter_complex')
transcoderCommands.append(f'-loudnorm=I={loudnessTarget}')
elif applyReplaygain:
transcoderCommands.append('-af')
transcoderCommands.append('volume=replaygain=track')
transcoderCommands += ['-ac', '2', '-ar', '44100', '-f', 'wav']
intermediary = Path(gettempdir(), str(uuid4())).absolute()
with NamedTemporaryFile() as input:
shutil.copyfileobj(file.file, input)
logger.info("Starting ffmpeg...")
transcoder = subprocess.run([
'/usr/bin/ffmpeg', '-i',
Path(input.name),
*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
filename = file.filename
logger.info(f"Beginning decode for {filename}")
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),
output])
background_tasks.add_task(remove_file, output, logger)
return FileResponse(path=output, filename=Path(filename).stem + '.wav', media_type='audio/wav')