diff --git a/.github/workflows/django.yaml b/.github/workflows/django.yaml index fa57f71..d2d821d 100644 --- a/.github/workflows/django.yaml +++ b/.github/workflows/django.yaml @@ -1,10 +1,10 @@ name: Django CI -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] +# on: +# push: +# branches: [ "main" ] +# pull_request: +# branches: [ "main" ] jobs: build: diff --git a/manage.py b/manage.py index 2060d42..f2a662c 100644 --- a/manage.py +++ b/manage.py @@ -6,7 +6,6 @@ def main(): """Run administrative tasks.""" - os.environ.setdefault('C_FORCE_ROOT', 'true') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') try: from django.core.management import execute_from_command_line diff --git a/video/helper.py b/video/helper.py index 9bfcb86..d9c82e2 100644 --- a/video/helper.py +++ b/video/helper.py @@ -3,8 +3,8 @@ def new_paths(video_path): - name_video = os.path.basename(video_path)[:-4] + "_edit.mp4" - full_path = video_path[:-4] + "_edit.mp4" + name_video = os.path.basename(video_path) + full_path = video_path[:-4] + "_temp.mp4" return full_path, name_video def encode_image(frame): diff --git a/video/serializers.py b/video/serializers.py index d4464f2..0e7687b 100644 --- a/video/serializers.py +++ b/video/serializers.py @@ -4,4 +4,5 @@ class VideoSerializer(serializers.ModelSerializer): class Meta: model = Video + read_only_fields = ("id") fields = ["id", "video", "slug"] \ No newline at end of file diff --git a/video/tasks.py b/video/tasks.py index f4e7f6c..63b032e 100644 --- a/video/tasks.py +++ b/video/tasks.py @@ -38,18 +38,18 @@ def process_clip(clip, text, x, y, fontsize): processed_frames.ready() processed_frames.successful() + # for frame in clip.iter_frames(): # processed_frame = process_frame.delay(frame, text, x, y, fontsize).get() # processed_frames.append(processed_frame) - print(processed_frames[0]) emoji_clip = ImageSequenceClip(processed_frames.join(), fps=clip.fps) processed_video = CompositeVideoClip([clip, emoji_clip]) return processed_video # @shared_task -def edit_video(video_slug, video_path, text_clip, x, y, timestep, duration, fontsize): +def edit_video(video_path, text_clip, x, y, timestep, duration, fontsize): clip = VideoFileClip(video_path) @@ -62,15 +62,7 @@ def edit_video(video_slug, video_path, text_clip, x, y, timestep, duration, font final_clip = concatenate_videoclips([clip1, final_clip2, clip3]) - full_path, name_video = new_paths(video_path) - - new_video = Video() - final_clip.write_videofile(full_path) - content = open(full_path, 'rb').read() - new_video.video.save(name_video, ContentFile(content)) - new_video.slug = video_slug - # final_clip.delete() - new_video.save() + # full_path, name_video = new_paths(video_path) - # return final_clip + return final_clip diff --git a/video/views.py b/video/views.py index ca9a2de..314adb2 100644 --- a/video/views.py +++ b/video/views.py @@ -1,3 +1,4 @@ +import os from django.shortcuts import render from rest_framework import viewsets from rest_framework.decorators import action @@ -8,8 +9,10 @@ from rest_framework.exceptions import APIException import time from .models import Video +from django.core.files.base import ContentFile from .serializers import VideoSerializer from .tasks import edit_video +from .helper import new_paths @@ -22,36 +25,35 @@ class VideoViewSet(viewsets.ModelViewSet): def add_text(self, request, pk=None): video_orginal = self.get_object() - - # try: - # with transaction.atomic(): - - start_time = time.time() - edit_video( - video_orginal.slug, - video_orginal.video.path, - request.data['text'], - int(request.data['x']), - int(request.data['y']), - int(request.data['t']), - int(request.data['d']), - int(request.data['s']) - ) - print(time.time() - start_time) - # edit_video.delay( - # video_orginal.slug, - # video_orginal.video.path, - # request.data['text'], - # int(request.data['x']), - # int(request.data['y']), - # int(request.data['t']), - # int(request.data['d']), - # int(request.data['s']) - # ) - - # transaction.on_commit(lambda: task_execute.delay(job_params)) - - # except Exception as e: - # raise APIException(str(e)) + video_path = video_orginal.video.path + video_slug = video_orginal.slug + video_obj = video_orginal.video + + try: + with transaction.atomic(): + final_clip = edit_video( + video_path, + request.data['text'], + int(request.data['x']), + int(request.data['y']), + int(request.data['t']), + int(request.data['d']), + int(request.data['s']) + ) + + full_path, name_video = new_paths(video_path) + + final_clip.write_videofile(full_path, threads=4, audio = False, progress_bar = False) + content = open(full_path, 'rb').read() + + new_video = Video(slug = video_slug + "edit") + new_video.video.save(name_video, ContentFile(content)) + new_video.save() + + os.remove(full_path) + + except Exception as e: + transaction.rollback() + raise APIException(str(e)) - return Response({"edit":True}, status=status.HTTP_200_OK) + return Response({"edit":True}, status=status.HTTP_200_OK) \ No newline at end of file