Get the duration of a video on django

4

Hi, I would like to know how I get the duration of a video on django. I will show you what I have so they know what I have tried

I've tried using these examples: link

but you can not because those codes receive the name of the file as a string and if the file is in another directory you should also pass it all together: ../ example / hello.mp4

as it seems the files before being saved are saved temporarily I have the path and the name of the file but nothing happens, I do not know what to try

  def clean_archivo_video(self):
    file_f = self.cleaned_data['archivo_video']
    mime = magic.from_buffer(file_f.read(), mime=True)
    if mime != 'video/mp4':
    raise forms.ValidationError('Sube un archivo de MP4.')

    ff = UploadedFile(file_f)
    yy = ff._get_name()
    fff = TemporaryUploadedFile(ff._get_name(), ff.content_type, ff.size, ff.charset)
    uu = fff.temporary_file_path()
    www = str(uu+'/'+yy)
    print www
    video_file_path = www
    print duration(video_file_path)
    return file_f

with the above code I get the name of the file (not the one that I will save in the database but with the one that is saved in the temporary directory) and the place where the file is located but nothing happens.

Try the other solutions for the other answers but none works on django, except django works perfectly. Out of django I pass the name of the file as a string as a parameter and it works well the problem is that it does not work inside django.

    
asked by Emanuel Mamani 21.08.2016 в 22:44
source

1 answer

1

I think there are no Python modules for that. The translation of the related question in SO 1 says:

You may have to use an external program. fprobe can give you the information you need.

import subprocess

def getLength(filename):
    result = subprocess.Popen(
        ["ffprobe", filename], stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
    )
    return [x for x in result.stdout.readlines() if "Duration" in x]

1 The credits are for the user @SingleNegationElimination , Link to question here

    
answered by 24.02.2017 в 19:56