Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- import subprocess
- import os
- from django.db.models.signals import post_save
- from django.dispatch import receiver
- from django.core.files import File
- from pathlib import Path
- from decouple import config
- from .models import Lecture
- AWS_STORAGE_BUCKET_NAME = config("AWS_STORAGE_BUCKET_NAME")
- s3 = boto3.resource("s3")
- bucket = s3.Bucket(AWS_STORAGE_BUCKET_NAME)
- @receiver(post_save, sender=Lecture)
- def handle_video_upload(sender, instance, created, **kwargs):
- """
- Variables names:
- - file_absolute_path: absolute path of the saved instance
- - file_suffix: file suffix to check if the video is a new
- .mkv video being uploaded, to check if the conversion
- is needed
- - file_absolute_path_m3u8: a string with absolute path to save
- the new .m3u8 file
- - file_name_m3u8: the name of the file with .m3u8 suffix
- """
- file_absolute_path = Path(instance.file.path)
- file_suffix = file_absolute_path.suffix
- if not file_suffix == '.m3u8' and instance.file_type == "V":
- file_absolute_path_m3u8 = file_absolute_path.with_suffix(".m3u8")
- file_name_m3u8 = file_absolute_path_m3u8.name
- # use shell commands to create HLS .m3u8 file
- # and delete the original video file
- subprocess.run([
- "ffmpeg",
- "-i",
- file_absolute_path,
- "-f",
- "hls",
- file_absolute_path_m3u8
- ])
- subprocess.run(["rm", file_absolute_path])
- # update the file with the new .m3u8 file
- with open(file_absolute_path_m3u8, "r") as file_object:
- file_m3u8 = File(name=file_absolute_path_m3u8, file=file_object)
- subprocess.run(["rm", file_absolute_path_m3u8])
- instance.file.save(file_name_m3u8, file_m3u8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement