Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. import subprocess
  2. import os
  3. from django.db.models.signals import post_save
  4. from django.dispatch import receiver
  5. from django.core.files import File
  6. from pathlib import Path
  7. from .models import Lecture
  8.  
  9.  
  10. @receiver(post_save, sender=Lecture)
  11. def handle_video_upload(sender, instance, created, **kwargs):
  12.     """
  13.    Variables:
  14.        - file_relative_path:
  15.             path relative to the project directory
  16.             ex: media/lectures/first_video_2/2020-04-04_16-11-20.mkv
  17.  
  18.         - file_suffix:
  19.             suffix | file extension
  20.             ex: .mkv
  21.  
  22.         - file_relative_dir:
  23.             project-related directory, without the file name included
  24.             ex: media/lectures/first_video_2
  25.  
  26.         - file_name_m3u8:
  27.             name of the file containing the .m3u8 extension
  28.             ex: 2020-04-04_16-11-20.m3u8
  29.  
  30.         - file_tmp_local_dir:
  31.             temporary directory to store files
  32.             ex: /tmp/media/lectures/first_video_2
  33.  
  34.         - file_tmp_local_output:
  35.             temporary path of the .m3u8 file, where also
  36.             will have the .ts files
  37.             ex: /tmp/media/lectures/first_video_2/2020-04-04_16-11-20.m3u8
  38.  
  39.         - file_cloudfront_url:
  40.             url of the file uploaded to the cloudfront
  41.             ex: https://d4fmj82j2soe4.cloudfront.net/media/lectures/first_video_2/2020-04-04_16-11-20.mkv
  42.    """
  43.  
  44.     file_relative_path = Path(instance.file.name)
  45.     file_suffix = file_relative_path.suffix
  46.  
  47.     if not file_suffix == '.m3u8' and instance.file_type == "V":
  48.         file_relative_dir = os.path.dirname(instance.file.name)
  49.  
  50.         file_relative_path_m3u8 = file_relative_path.with_suffix(".m3u8")
  51.         file_name_m3u8 = file_relative_path_m3u8.name
  52.  
  53.         file_tmp_local_dir = f"/tmp/{file_relative_dir}"
  54.         file_tmp_local_output = f"{file_tmp_local_dir}/{file_name_m3u8}"
  55.         file_cloudfront_url = instance.file.url
  56.  
  57.         # create the local dir for the files
  58.         subprocess.run(['mkdir', '-p', file_tmp_local_dir])
  59.  
  60.         # convert a video in HLS (.m3u8 and .ts files)
  61.         subprocess.run([
  62.             "ffmpeg",
  63.             "-i",
  64.             file_cloudfront_url,
  65.             "-f",
  66.             "hls",
  67.             file_tmp_local_output
  68.         ])
  69.  
  70.         # update the instance file field with the new .m3u8 file
  71.         with open(file_tmp_local_output, "r") as file_object:
  72.             print(file_tmp_local_output)
  73.             print(file_name_m3u8)
  74.             file_m3u8 = File(
  75.                 name=file_relative_path_m3u8,
  76.                 file=file_object)
  77.             instance.file.save(file_name_m3u8, file_m3u8)
  78.             subprocess.run(["rm", "-r", file_tmp_local_dir])
  79.  
  80.         print("file_relative_path: ", file_relative_path)
  81.  
  82.         print("file_suffix: ", file_suffix)
  83.         print("file_relative_dir: ", file_relative_dir)
  84.         print("file_name_m3u8: ", file_name_m3u8)
  85.         print("file_relative_path_m3u8: ", file_relative_path_m3u8)
  86.  
  87.         print("file_tmp_local_dir:", file_tmp_local_dir)
  88.         print("file_tmp_local_output", file_tmp_local_output)
  89.  
  90.         print("file_cloudfront_url: ", file_cloudfront_url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement