Re-encode audio

This commit is contained in:
MatthiasSaihttam 2021-04-29 01:50:37 -04:00
parent 32c8cc5476
commit f7ab61cbee
2 changed files with 18 additions and 1 deletions

View File

@ -20,7 +20,7 @@
{% with file_type=thought.get_media_type %}
{% if file_type == "png" or file_type == "jpeg" %}
<img src="{{ thought.media.url }}" class="extended-media">
{% elif file_type == "m4a" or file_type == "mp3" %}
{% elif file_type == "m4a" or file_type == "mp3" or file_type == "aac" %}
<audio controls src="{{ thought.media.url }}" class="extended-media"></audio>
{% elif file_type == "mov" or file_type == "mp4" %}
<video src="{{ thought.media.url }}" class="extended-media"></video>

View File

@ -1,4 +1,6 @@
import os.path
import uuid
import subprocess
import magic
@ -6,6 +8,7 @@ from django.http import HttpResponse
from django.shortcuts import render
from django.utils import timezone
from whispermaphone import settings
from .models import Thought
# A dict mapping allowed mime types to file extensions
@ -87,6 +90,20 @@ def post(request):
thought.media = request.FILES["media"]
if media_type == "audio/x-m4a":
# 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)
# and then the backend just has to check a 3 item whitelist
thought.save() # Save so that we have a file to work with
subprocess.run(["ffmpeg",
"-i", thought.media.path,
"-codec:a", "aac",
os.path.join(settings.MEDIA_ROOT, f"{thought.uuid}.aac")
])
os.remove(f"{thought.uuid}.m4a") # Remove the original file
thought.media.name = f"{thought.uuid}.aac" # Update the file in the DB
thought.save()
return render(request, "whispermaphone/post.html", {})