]> littlesong.place Git - littlesongplace.git/commitdiff
Add support for non-mp3 audio/video formats
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 26 Jan 2025 17:13:59 +0000 (12:13 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 26 Jan 2025 17:13:59 +0000 (12:13 -0500)
main.py
templates/edit-song.html
test/sample-4s.mp4 [new file with mode: 0644]
test/test_offline.py

diff --git a/main.py b/main.py
index 8dab553dd69b108aee83aa51b20c553b55c74a55..08ccbbd58d0fe9211efa6a89b735d302a88a6e71 100644 (file)
--- 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/<int:songid>")
index 63e02b7b93a38ba76e5ef8d3f6083223b8f23454..94e49f9769394814c991462ce59e9e61f7ba6c04 100644 (file)
@@ -4,6 +4,12 @@
 
 {% 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>
@@ -12,8 +18,8 @@
     <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>
diff --git a/test/sample-4s.mp4 b/test/sample-4s.mp4
new file mode 100644 (file)
index 0000000..16742b9
Binary files /dev/null and b/test/sample-4s.mp4 differ
index 8b990f43b5baa16ab26949e425511126be20fefe..3e78f9d73393039f6d629fb1a2cef8b1dad29e24 100644 (file)
@@ -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 &#39;song title&#39;", 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)