Feat/auto generate edited files #2
No files matched your search
+19
-19
@@ -1,21 +1,21 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"name": "Umsonst gelebt",
|
"name": "Umsonst gelebt",
|
||||||
"artist": "Jugend Chor",
|
"artist": "Jugend Chor",
|
||||||
"id": "002",
|
"id": "002",
|
||||||
"original_name": "-",
|
"original_name": "-",
|
||||||
"original": {
|
"original": {
|
||||||
"filename": "m003.mp3",
|
"filename": "m003.mp3",
|
||||||
"sampling_rate": 44.1,
|
"sampling_rate": 44100,
|
||||||
"bitrate": 256
|
"bitrate": 256000
|
||||||
},
|
},
|
||||||
"edits": [
|
"edits": [
|
||||||
{
|
{
|
||||||
"filename": "m004.mp3",
|
"filename": "m004.mp3",
|
||||||
"sampling_rate": 22.1,
|
"sampling_rate": 22100,
|
||||||
"bitrate": 63,
|
"bitrate": 63000,
|
||||||
"id": "1"
|
"id": "1"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -15,6 +15,7 @@ from db import init_db, get_db, close_db, create_participant, now
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
import threading
|
import threading
|
||||||
from mutagen import File
|
from mutagen import File
|
||||||
|
import subprocess
|
||||||
|
|
||||||
bp = Blueprint("main", __name__)
|
bp = Blueprint("main", __name__)
|
||||||
|
|
||||||
@@ -369,3 +370,77 @@ def api_admin_add():
|
|||||||
})
|
})
|
||||||
|
|
||||||
return jsonify({"saved": True, "filename": filename_new})
|
return jsonify({"saved": True, "filename": filename_new})
|
||||||
|
|
||||||
|
VALID_SAMPLING_RATES = {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000}
|
||||||
|
|
||||||
|
@bp.route("/api/admin/add/<songId>", methods=["POST"])
|
||||||
|
@admin_required
|
||||||
|
def api_admin_add_task(songId):
|
||||||
|
mode = request.form.get("mode", "cbr")
|
||||||
|
sampling_rate = request.form.get("sampling_rate", type=int)
|
||||||
|
|
||||||
|
if mode not in ("cbr", "vbr"):
|
||||||
|
return jsonify({"saved": False, "error": "Ungültiger Modus"}), 400
|
||||||
|
if sampling_rate not in VALID_SAMPLING_RATES:
|
||||||
|
return jsonify({"saved": False, "error": "Ungültige Abtastrate"}), 400
|
||||||
|
|
||||||
|
songs = load_songs_list()
|
||||||
|
song = next((s for s in songs if str(s["id"]) == songId), None)
|
||||||
|
if song is None:
|
||||||
|
return jsonify({"saved": False, "error": "Song nicht gefunden"}), 404
|
||||||
|
|
||||||
|
original = song["original"]
|
||||||
|
if sampling_rate > original["sampling_rate"]:
|
||||||
|
return jsonify({"saved": False, "error": "Abtastrate höher als Original"}), 400
|
||||||
|
|
||||||
|
if mode == "vbr":
|
||||||
|
quality = request.form.get("quality", type=int)
|
||||||
|
if quality is None or not 0 <= quality <= 9:
|
||||||
|
return jsonify({"saved": False, "error": "Ungültige VBR-Qualität"}), 400
|
||||||
|
quality_args = ["-q:a", str(quality)]
|
||||||
|
else:
|
||||||
|
bitrate_kbps = request.form.get("bitrate", type=int)
|
||||||
|
if not bitrate_kbps or bitrate_kbps <= 0:
|
||||||
|
return jsonify({"saved": False, "error": "Bitrate fehlt"}), 400
|
||||||
|
if bitrate_kbps * 1000 > original["bitrate"] + 1000:
|
||||||
|
return jsonify({"saved": False, "error": "Bitrate höher als Original"}), 400
|
||||||
|
quality_args = ["-b:a", f"{bitrate_kbps}k"]
|
||||||
|
|
||||||
|
src = BASE_MUSIC_DIR / original["filename"]
|
||||||
|
if not src.is_file():
|
||||||
|
return jsonify({"saved": False, "error": "Originaldatei fehlt"}), 404
|
||||||
|
|
||||||
|
edit_id = uuid.uuid4().hex
|
||||||
|
dst = BASE_MUSIC_DIR / (edit_id + ".mp3") # immer MP3, siehe Hinweis unten
|
||||||
|
|
||||||
|
try:
|
||||||
|
subprocess.run(
|
||||||
|
["ffmpeg", "-y", "-i", str(src), "-vn",
|
||||||
|
"-c:a", "libmp3lame", *quality_args,
|
||||||
|
"-ar", str(sampling_rate), str(dst)],
|
||||||
|
check=True, capture_output=True, timeout=300,
|
||||||
|
)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
dst.unlink(missing_ok=True)
|
||||||
|
current_app.logger.error("ffmpeg: %s", e.stderr.decode(errors="replace")[-1000:])
|
||||||
|
return jsonify({"saved": False, "error": "Konvertierung fehlgeschlagen"}), 500
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
dst.unlink(missing_ok=True)
|
||||||
|
return jsonify({"saved": False, "error": "Zeitüberschreitung"}), 500
|
||||||
|
|
||||||
|
# tatsächliche Werte aus der fertigen Datei lesen
|
||||||
|
out = File(dst)
|
||||||
|
actual_bitrate = out.info.bitrate if out else None
|
||||||
|
actual_rate = out.info.sample_rate if out else sampling_rate
|
||||||
|
|
||||||
|
song.setdefault("edits", []).append({
|
||||||
|
"id": edit_id,
|
||||||
|
"filename": dst.name,
|
||||||
|
"mode": mode,
|
||||||
|
"quality": quality if mode == "vbr" else None,
|
||||||
|
"bitrate": actual_bitrate,
|
||||||
|
"sampling_rate": actual_rate,
|
||||||
|
})
|
||||||
|
save_songs_list(songs)
|
||||||
|
|
||||||
|
return jsonify({"saved": True, "id": edit_id})
|
||||||
@@ -3,13 +3,40 @@
|
|||||||
{% block admin_title %}Kopie erstellen{% endblock %}
|
{% block admin_title %}Kopie erstellen{% endblock %}
|
||||||
|
|
||||||
{% block admin_content %}
|
{% block admin_content %}
|
||||||
{{ song }}
|
|
||||||
<form class="form" id="form">
|
<form class="form" id="form">
|
||||||
<label for="titel">Titel</label>
|
<label for="mode">Modus</label>
|
||||||
<input id="title" name="title" type="text" autocomplete="off" required autofocus>
|
<select id="mode" name="mode">
|
||||||
|
<option value="cbr">Konstant (CBR)</option>
|
||||||
|
<option value="vbr">Variabel (VBR)</option>
|
||||||
|
</select>
|
||||||
|
|
||||||
<label for="artist">Artist</label>
|
<div id="cbr-field">
|
||||||
<input id="artist" name="artist" type="text" autocomplete="off">
|
<label for="bitrate">Bitrate (kbit/s)</label>
|
||||||
|
<select id="bitrate" name="bitrate">
|
||||||
|
{% for b in [32, 48, 64, 96, 128, 160, 192, 256, 320] if b * 1000 <= song.original.bitrate %}
|
||||||
|
<option value="{{ b }}">{{ b }} kbit/s</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="vbr-field" hidden>
|
||||||
|
<label for="quality">VBR-Qualität</label>
|
||||||
|
<select id="quality" name="quality">
|
||||||
|
<option value="0">V0 (beste, ca. 220–260 kbit/s)</option>
|
||||||
|
<option value="2">V2 (ca. 170–210 kbit/s)</option>
|
||||||
|
<option value="4">V4 (ca. 140–185 kbit/s)</option>
|
||||||
|
<option value="6">V6 (ca. 100–130 kbit/s)</option>
|
||||||
|
<option value="8">V8 (ca. 70–105 kbit/s)</option>
|
||||||
|
<option value="9">V9 (schlechteste, ca. 65–85 kbit/s)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label for="sampling_rate">Abtastrate (Hz)</label>
|
||||||
|
<select id="sampling_rate" name="sampling_rate" required>
|
||||||
|
{% for r in [8000, 11025, 16000, 22050, 32000, 44100, 48000] if r <= song.original.sampling_rate %}
|
||||||
|
<option value="{{ r }}" {% if r == song.original.sampling_rate %}selected{% endif %}>{{ r }} Hz</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
|
||||||
<button type="submit">Erstellen</button>
|
<button type="submit">Erstellen</button>
|
||||||
</form>
|
</form>
|
||||||
@@ -18,8 +45,8 @@
|
|||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
<th>Bitrate</th>
|
<th>Bitrate (bit/s)</th>
|
||||||
<th>Abtastrate</th>
|
<th>Abtastrate (Hz)</th>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -31,7 +58,7 @@
|
|||||||
{% for e in song.edits %}
|
{% for e in song.edits %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ e.id }}</td>
|
<td>{{ e.id }}</td>
|
||||||
<td>{{ e.bitrate }} ({{ (e.bitrate / song.original.bitrate * 100) | round(1) }}%)</td>
|
<td>{{ e.bitrate }} ({{ (e.bitrate / song.original.bitrate * 100) | round(1) }}%) {{ e.get("mode", "cbr") | upper }}</td>
|
||||||
<td>{{ e.sampling_rate }} ({{ (e.sampling_rate / song.original.sampling_rate * 100) | round(1) }}%)</td>
|
<td>{{ e.sampling_rate }} ({{ (e.sampling_rate / song.original.sampling_rate * 100) | round(1) }}%)</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
@@ -44,20 +71,25 @@
|
|||||||
const form = document.getElementById("form");
|
const form = document.getElementById("form");
|
||||||
form.addEventListener("submit", async (event) => {
|
form.addEventListener("submit", async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const res = await fetch("/api/admin/add", {
|
const button = form.querySelector("button");
|
||||||
method: "POST",
|
button.disabled = true; // Doppelklick verhindern
|
||||||
body: new FormData(form),
|
try {
|
||||||
});
|
const res = await fetch("/api/admin/add/{{ song.id }}", {
|
||||||
const result = await res.json();
|
method: "POST",
|
||||||
console.log(result);
|
body: new FormData(form),
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error("Fehler " + res.status);
|
||||||
|
location.reload(); // Tabelle aktualisieren
|
||||||
|
} catch (err) {
|
||||||
|
alert("Erstellen fehlgeschlagen: " + err.message);
|
||||||
|
button.disabled = false;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const fileInput = document.getElementById("file");
|
const mode = document.getElementById("mode");
|
||||||
const fileName = document.getElementById("file-name");
|
mode.addEventListener("change", () => {
|
||||||
fileInput.addEventListener("change", () => {
|
document.getElementById("cbr-field").hidden = mode.value !== "cbr";
|
||||||
fileName.textContent = fileInput.files.length
|
document.getElementById("vbr-field").hidden = mode.value !== "vbr";
|
||||||
? fileInput.files[0].name
|
|
||||||
: "Keine Datei ausgewählt";
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,8 @@
|
|||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Artist</th>
|
<th>Artist</th>
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
<th>Bitrate</th>
|
<th>Bitrate (bit/s)</th>
|
||||||
<th>Abtastrate</th>
|
<th>Abtastrate (Hz)</th>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for s in songs %}
|
{% for s in songs %}
|
||||||
|
|||||||
Reference in new issue
Block a user