How to play videos with the Python IO library?

1

I would like to play a video that I get from an ftp server, at this moment the file is being saved on the server but I do not want it to be saved but to be played immediately.

They told me that with the library io it can be done but I would like to know how.

My view code:

@csrf_exempt
def MostrarVideo(request):  

    nombre = request.POST['nombre']
    apellido = request.POST['apellido']
    filename = nombre + apellido + ".mp4"

    import ftplib

    ftp = ftplib.FTP('dirser', 'user', 'pass')
    ftp.dir()

    try:
       # Aquí es donde abro el archivo en mi servidor para que se guarde
        with open("videos/"+filename, 'wb') as f:
        # Aquí escribo los datos desde el servidor ftp hasta la carpeta videos en el Django
            ftp.retrbinary("RETR "+filename ,f.write)
    except Exception as e:
        print "Error " + str(e)
        return HttpResponse(0)
    print("Enviando")
    return HttpResponse(filename)
    
asked by A. Castro 04.04.2017 в 23:11
source

1 answer

1

Well, if you want to play your video, that is another question, which does not have much to do with django or python. Besides that there are many ways to do it. Basically what you want is to make a kind of proxy, in which your server makes the request to another ftp server to get the files. In this case an mp3. I will return it as an attachment. But maybe you could do a stream or I do not know.

import io
import ftplib
from django.http import HttpResponseServerError


@csrf_exempt
def mostrar_video(request):
    nombre = request.POST.get('nombre', '').strip()
    apellido = request.POST.get('apellido', '').strip()

    filename = '{}{}.mp4'.format(nombre, apellido)

    ftp = ftplib.FTP('dirser', 'user', 'pass')
    ftp.dir()
    response = HttpResponse()

    try:
        buffer = io.BytesIO()
        ftp.retrbinary('RETR {}'format(filename), buffer.write)
        response.content_type = 'video/mp4'
        response['Content-Disposition'] = 'attachment; filename={}'.format(filename)
        response.write(buffer.getvalue())
        buffer.close()
    except Exception as error:
        response = HttpResponseServerError(error.__str__())
    return response

Tell me how you are doing with this snippet of code, given that I could not prove it.

    
answered by 06.04.2017 в 01:13