app: add convert_to_wav

This commit is contained in:
bretello 2024-06-14 19:29:12 +02:00
parent 17c7400d3e
commit d3709bd779
Signed by: brethil
GPG Key ID: 876AAC6290170FE7

22
app.py
View File

@ -1,4 +1,5 @@
import os
from subprocess import check_call, CalledProcessError
from flask import Flask
from flask import request, render_template
@ -12,6 +13,21 @@ app = Flask(
os.makedirs("uploads", exist_ok=True)
def convert_to_wav(file_name: str):
assert file_name.endswith(".wav")
output, extension = file_name.rsplit(".", maxsplit=1)
assert extension != "wav"
print(f"{extension=}")
command = f"tape2wav {file_name} ${output}.wav"
try:
check_call(command.split())
except CalledProcessError as exc:
print(f"tape2wav failed: {exc.output=}\n{exc.stderr=}")
raise
@app.route("/")
def upload_form():
return render_template(
@ -40,6 +56,12 @@ def upload_file():
except Exception as exc:
return f"fail: {exc=}"
name, extension = file.filename.rsplit(".", maxsplit=1)
if extension != "wav":
thread = Thread(target=convert_to_wav, args=(file.filename,))
thread.start()
print(f"thread started for {file.filename=}")
return "File uploaded successfully"