From: Chris Fulljames Date: Sun, 26 Jan 2025 17:13:59 +0000 (-0500) Subject: Add support for non-mp3 audio/video formats X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=922d876e9767cca7c97c331e1d84f7f163740ec4;p=littlesongplace.git Add support for non-mp3 audio/video formats --- diff --git a/main.py b/main.py index 8dab553..08ccbbd 100644 --- a/main.py +++ b/main.py @@ -317,7 +317,6 @@ def update_song(): 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: @@ -352,7 +351,6 @@ def create_song(): passed = convert_song(tmp_file, file) if not passed: - flash_and_log("Invalid mp3 file", "error") return True else: # Create song @@ -392,6 +390,20 @@ def convert_song(tmp_file, request_file): # 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/") diff --git a/templates/edit-song.html b/templates/edit-song.html index 63e02b7..94e49f9 100644 --- a/templates/edit-song.html +++ b/templates/edit-song.html @@ -4,6 +4,12 @@ {% block body %} +

+Handy Tip: +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. +

+ {% if song %}

Edit Song

@@ -12,8 +18,8 @@

Upload a New Song

{% endif %}
-
- +
+

diff --git a/test/sample-4s.mp4 b/test/sample-4s.mp4 new file mode 100644 index 0000000..16742b9 Binary files /dev/null and b/test/sample-4s.mp4 differ diff --git a/test/test_offline.py b/test/test_offline.py index 8b990f4..3e78f9d 100644 --- a/test/test_offline.py +++ b/test/test_offline.py @@ -233,10 +233,14 @@ def test_upload_song_collab_too_long(client): _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 @@ -303,7 +307,7 @@ def test_update_song_collab_too_long(client): 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)