P
P
Pavel Dyukarev2019-01-04 14:09:42
Django
Pavel Dyukarev, 2019-01-04 14:09:42

How to stream MP3 using Django?

Guys, if anyone came across - share your experience.
I can't figure out how to send a file to a template for processing using the Django StreamingHttpResponse class.
Let's say there is such a view:

import os
import mimetypes
from django.http import StreamingHttpResponse
from django.core.servers.basehttp import FileWrapper


def mp3(request):
   the_file = '/some/file/song.mp3'
   filename = os.path.basename(the_file)
   chunk_size = 8192
   response = StreamingHttpResponse(FileWrapper(open(the_file, 'rb'), chunk_size),
                           content_type=mimetypes.guess_type(the_file)[0])
   response['Content-Length'] = os.path.getsize(the_file)    
   response['Content-Disposition'] = "attachment; filename=%s" % filename
   return response

How is this "retrieved" on the django side of the template?
How to put several files in response?
Is it possible to iterate these files on the side of the template?
Perhaps there is another way?
Share your experience!
-------------------
I understand that using Django for streaming is not the best option - but I have no other options - you need to do it on Django.
-------------------
Do you have any working example?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-01-04
@PavelDuk

what's with janga?
well, give you a django player with the necessary playlist (the playlist can be generated by dzhanga), and then generally stream the server directly.
why try to cross a mare with a cart?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question