Home > Mobile >  In Django how to process the file inside request.FILES?
In Django how to process the file inside request.FILES?

Time:01-12

for file in request.FILES:
        print("")
        video_input_path = file
        img_output_path = 'output.jpg'
        subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])
        print("")

I'm trying to generate a thumbnail for the File (Video) uploaded via Form, this is pure python solution for generating the thumbnail from any video (works for pictures too but there is a better solution with PIL)

video_input_path = file

How do I access the file itself here? As far as I know my only options are

file = request.FILES['filename']
file.name           # Gives name
file.content_type   # Gives Content type text/html etc
file.size           # Gives file's size in byte
file.read()         # Reads file

Edit: This works, the thumbnails are generated in the /CACHE/... folder, but this can still be improved (see best answer down below)

CodePudding user response:

You probably need to manually handle the uploaded file (https://docs.djangoproject.com/es/4.0/topics/http/file-uploads/#basic-file-uploads). In general Django uses InMemoryUploadedFile for small files and TemporaryUploadedFile for large files. If you are sure that the file is large enough to force the use ofTemporaryUploadedFile , you can access its path via file.temporary_file_path() .

On the other hand, I recommend that you use the PyAV package instead of a subprocess to call ffmpeg.

  •  Tags:  
  • Related