From 58dc4d62b05128c4e3f31712d273830bdb4cebe4 Mon Sep 17 00:00:00 2001 From: Matthias Portzel Date: Sat, 7 May 2022 16:46:16 -0400 Subject: [PATCH] Move to mp3 as the default audio type * Also adds aac as an allowed audio type, fixing a bug where I could edit those posts * To convert old audio to mp3, the following commands were used: ls *.aac | xargs -I {} bash -c 'ffmpeg -y -i {} -acodec mp3 $(echo {} | sed s/aac/mp3/)' for t in Thought.objects.filter(media__endswith=".aac"): t.file.name = f"{t.uuid}.mp3" t.save() And ditto with m4a. --- thoughts/models.py | 3 ++- thoughts/views.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/thoughts/models.py b/thoughts/models.py index 830549d..e2fd36f 100644 --- a/thoughts/models.py +++ b/thoughts/models.py @@ -55,7 +55,8 @@ ALLOWED_MEDIA_TYPES = { "image/png": "png", "image/jpeg": "jpeg", "audio/x-m4a": "m4a", - "audio/mp3": "mp3", + "audio/mpegaudio/mpeg": "mp3", + "audio/x-hx-aac-adts": "aac", "video/mp4": "mp4", "video/quicktime": "mov", } diff --git a/thoughts/views.py b/thoughts/views.py index 5f1de0f..507f4c6 100644 --- a/thoughts/views.py +++ b/thoughts/views.py @@ -134,7 +134,7 @@ def post(request): thought.media.name = f"{thought.uuid}.{ALLOWED_MEDIA_TYPES[media_type]}" - if media_type == "audio/x-m4a": + if media_type == "audio/x-m4a" or media_type == "audio/x-hx-aac-adts": # This is a hack-fix because I want to be able to upload audio # In the future, this should be refactored to convert file types # using ffmpeg.js on the client side (so there are 0 security concerns) @@ -142,11 +142,11 @@ def post(request): thought.save() # Save so that we have a file to work with subprocess.run(["ffmpeg", "-i", thought.media.path, - "-codec:a", "aac", "-vn", - os.path.join(settings.MEDIA_ROOT, f"{thought.uuid}.aac") + "-codec:a", "mp3", "-vn", + os.path.join(settings.MEDIA_ROOT, f"{thought.uuid}.mp3") ], check=True) - os.remove(os.path.join(settings.MEDIA_ROOT, f"{thought.uuid}.m4a")) # Remove the original file - thought.media.name = f"{thought.uuid}.aac" # Update the file in the DB + os.remove(os.path.join(settings.MEDIA_ROOT, thought.media.name)) # Remove the original file + thought.media.name = f"{thought.uuid}.mp3" # Update the file in the DB # We need to make sure that if we remove an image, the alt text is removed with it if not thought.media: