2018-02-26 18:27:41 +01:00
|
|
|
import mimetypes
|
2017-06-23 23:00:42 +02:00
|
|
|
|
2018-06-10 10:55:16 +02:00
|
|
|
import magic
|
|
|
|
|
import mutagen
|
2018-10-24 19:14:51 +02:00
|
|
|
import pydub
|
2017-06-23 23:00:42 +02:00
|
|
|
|
2018-07-03 08:13:13 +02:00
|
|
|
from funkwhale_api.common.search import normalize_query, get_query # noqa
|
2018-02-18 23:46:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def guess_mimetype(f):
|
2018-06-03 18:07:59 +02:00
|
|
|
b = min(1000000, f.size)
|
2018-02-26 18:27:41 +01:00
|
|
|
t = magic.from_buffer(f.read(b), mime=True)
|
2018-06-09 15:36:16 +02:00
|
|
|
if not t.startswith("audio/"):
|
2018-02-26 18:27:41 +01:00
|
|
|
# failure, we try guessing by extension
|
2019-06-13 11:05:28 +02:00
|
|
|
mt, _ = mimetypes.guess_type(f.name)
|
2018-02-26 18:27:41 +01:00
|
|
|
if mt:
|
|
|
|
|
t = mt
|
|
|
|
|
return t
|
2018-02-21 00:02:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def compute_status(jobs):
|
2018-06-09 15:36:16 +02:00
|
|
|
statuses = jobs.order_by().values_list("status", flat=True).distinct()
|
|
|
|
|
errored = any([status == "errored" for status in statuses])
|
2018-02-21 00:02:09 +01:00
|
|
|
if errored:
|
2018-06-09 15:36:16 +02:00
|
|
|
return "errored"
|
|
|
|
|
pending = any([status == "pending" for status in statuses])
|
2018-02-21 00:02:09 +01:00
|
|
|
if pending:
|
2018-06-09 15:36:16 +02:00
|
|
|
return "pending"
|
|
|
|
|
return "finished"
|
2018-04-07 15:34:35 +02:00
|
|
|
|
|
|
|
|
|
2018-04-27 21:10:02 +02:00
|
|
|
AUDIO_EXTENSIONS_AND_MIMETYPE = [
|
2019-07-25 15:38:44 +02:00
|
|
|
("ogg", "video/ogg"),
|
2018-06-09 15:36:16 +02:00
|
|
|
("ogg", "audio/ogg"),
|
2019-06-27 06:05:22 +02:00
|
|
|
("opus", "audio/opus"),
|
2018-06-09 15:36:16 +02:00
|
|
|
("mp3", "audio/mpeg"),
|
|
|
|
|
("flac", "audio/x-flac"),
|
2018-11-19 22:20:09 +01:00
|
|
|
("flac", "audio/flac"),
|
2018-04-27 21:10:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
EXTENSION_TO_MIMETYPE = {ext: mt for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}
|
|
|
|
|
MIMETYPE_TO_EXTENSION = {mt: ext for ext, mt in AUDIO_EXTENSIONS_AND_MIMETYPE}
|
|
|
|
|
|
2019-05-02 13:15:33 +02:00
|
|
|
SUPPORTED_EXTENSIONS = list(
|
|
|
|
|
sorted(set([ext for ext, _ in AUDIO_EXTENSIONS_AND_MIMETYPE]))
|
|
|
|
|
)
|
|
|
|
|
|
2018-04-27 21:10:02 +02:00
|
|
|
|
2018-04-07 15:34:35 +02:00
|
|
|
def get_ext_from_type(mimetype):
|
2018-04-27 21:10:02 +02:00
|
|
|
return MIMETYPE_TO_EXTENSION.get(mimetype)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_type_from_ext(extension):
|
2018-06-09 15:36:16 +02:00
|
|
|
if extension.startswith("."):
|
2018-04-27 21:10:02 +02:00
|
|
|
# we remove leading dot
|
|
|
|
|
extension = extension[1:]
|
|
|
|
|
return EXTENSION_TO_MIMETYPE.get(extension)
|
2018-05-15 21:59:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_audio_file_data(f):
|
|
|
|
|
data = mutagen.File(f)
|
2018-05-16 18:55:09 +02:00
|
|
|
if not data:
|
|
|
|
|
return
|
2018-05-15 21:59:29 +02:00
|
|
|
d = {}
|
2018-08-29 16:57:05 +02:00
|
|
|
d["bitrate"] = getattr(data.info, "bitrate", 0)
|
2018-06-09 15:36:16 +02:00
|
|
|
d["length"] = data.info.length
|
2018-05-15 21:59:29 +02:00
|
|
|
|
|
|
|
|
return d
|
2018-09-06 18:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_actor_from_request(request):
|
|
|
|
|
actor = None
|
|
|
|
|
if hasattr(request, "actor"):
|
|
|
|
|
actor = request.actor
|
|
|
|
|
elif request.user.is_authenticated:
|
|
|
|
|
actor = request.user.actor
|
|
|
|
|
|
|
|
|
|
return actor
|
2018-10-24 19:14:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def transcode_file(input, output, input_format, output_format, **kwargs):
|
|
|
|
|
with input.open("rb"):
|
|
|
|
|
audio = pydub.AudioSegment.from_file(input, format=input_format)
|
2019-01-29 09:32:35 +01:00
|
|
|
return transcode_audio(audio, output, output_format, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def transcode_audio(audio, output, output_format, **kwargs):
|
2018-10-24 19:14:51 +02:00
|
|
|
with output.open("wb"):
|
|
|
|
|
return audio.export(output, format=output_format, **kwargs)
|