Advertisement
a_igin

Untitled

Nov 4th, 2020
1,761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import mimetypes
  2. import subprocess
  3. import tempfile
  4.  
  5. from django.utils.crypto import get_random_string
  6.  
  7. from core import consts
  8.  
  9.  
  10. class FileInfo:
  11.  
  12.     @staticmethod
  13.     def get_data(file):
  14.         data = {'type': '', 'sub_type': '', 'duration': None}
  15.         mime, encoding = mimetypes.guess_type(file.path)
  16.  
  17.         if mime:
  18.             data['type'], data['sub_type'] = mime.split('/')
  19.         else:
  20.             data['type'] = consts.OTHER
  21.  
  22.         if data['type'] in [consts.VIDEO, consts.AUDIO]:
  23.             fh, temp_path = tempfile.mkstemp()
  24.             with open(temp_path, "wb") as temp_file:
  25.                 temp_file.write(file.read())
  26.  
  27.             result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
  28.                                      "format=duration", "-of",
  29.                                      "default=noprint_wrappers=1:nokey=1", temp_path],
  30.                                     stdout=subprocess.PIPE,
  31.                                     stderr=subprocess.STDOUT)
  32.             data['duration'] = float(result.stdout)
  33.  
  34.         return data
  35.  
  36.  
  37. def generate_promo_code():
  38.     return get_random_string(length=7).upper()
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement