filepath = get_user_path(session["userid"]) / (str(song_data["songid"]) + ".mp3")
shutil.move(tmp_file.name, filepath)
else:
- flash_and_log("Invalid mp3 file", "error")
error = True
if not error:
passed = convert_song(tmp_file, file)
if not passed:
- flash_and_log("Invalid mp3 file", "error")
return True
else:
# Create song
# Uploaded valid mp3 file
return True
+ # Not a valid mp3, try to convert with ffmpeg
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as out_file:
+ out_file.close()
+ os.remove(out_file.name)
+ result = subprocess.run(["ffmpeg", "-i", tmp_file.name, out_file.name], stdout=subprocess.PIPE)
+ if result.returncode == 0:
+ # Successfully converted file, overwrite original file
+ os.replace(out_file.name, tmp_file.name)
+ return True
+
+ if os.path.exists(out_file.name):
+ os.remove(out_file.name)
+
+ flash_and_log("Invalid audio file", "error")
return False
@app.get("/delete-song/<int:songid>")
{% block body %}
+<p>
+<em>Handy Tip:</em>
+If you upload a video (e.g. a PS4/PS5 capture file), the audio will be extracted automatically!
+Most standard audio/video formats are supported - .wav, .mp3, .ogg, .mp4, etc.
+</p>
+
{% if song %}
<form action="/upload-song?songid={{ song.songid }}" method="post" enctype="multipart/form-data">
<h2>Edit Song</h2>
<h2>Upload a New Song</h2>
{% endif %}
<div class="upload-form">
- <label for="song">{% if song %}Replace {% endif %}mp3 File</label><br>
- <input type="file" name="song" accept=".mp3" id="file-select" {% if not song %}required{% endif %}>
+ <label for="song">{% if song %}Replace {% endif %}Audio File</label><br>
+ <input type="file" name="song" id="file-select" {% if not song %}required{% endif %}>
</div>
<div class="upload-form">
<label for="title">Title</label><br>
_create_user(client, "user", "password", login=True)
_test_upload_song(client, b"not a valid collaborator name", error=True, collabs="a"*32)
-def test_upload_song_invalid_mp3(client):
+def test_upload_song_invalid_audio(client):
_create_user(client, "user", "password", login=True)
- song_file = open(__file__, "rb")
- _test_upload_song(client, b"Invalid mp3 file", error=True, song=song_file)
+ # Use this script file as the "audio" file
+ _test_upload_song(client, b"Invalid audio file", error=True, filename=__file__)
+
+def test_upload_song_from_mp4(client):
+ _create_user(client, "user", "password", login=True)
+ _test_upload_song(client, b"Successfully uploaded 'song title'", filename="sample-4s.mp4")
################################################################################
# Edit Song
def test_update_song_invalid_mp3(client):
_create_user_and_song(client)
song_file = open(__file__, "rb")
- _test_upload_song(client, b"Invalid mp3 file", error=True, songid=1, song=song_file)
+ _test_upload_song(client, b"Invalid audio file", error=True, songid=1, song=song_file)
def test_update_song_invalid_song(client):
_create_user_and_song(client)