Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video compression task #1124

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions zubhub_backend/media/media/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
from rest_framework.response import Response
from rest_framework.decorators import api_view, throttle_classes
from django.utils.text import slugify
from django.http import HttpResponseBadRequest
from django.core.files.base import ContentFile
from tempfile import NamedTemporaryFile
from cloudinary.utils import api_sign_request
from math import floor
import uuid
import os
import subprocess
from time import time
from cloudinary import config

Expand Down Expand Up @@ -134,12 +139,38 @@ def UploadFileAPIView(request):
try:
key = request.data.get("key")
__, name = key.split("/")
file = request.data.get(name)
url = upload_file(file, key)
original_video_file = request.data.get(name)

if original_video_file.size > 20000000:
return Response({"error": "File size is too large"}, status=status.HTTP_400_BAD_REQUEST)

# Saves the original video to a temporary file
temp_input_file = NamedTemporaryFile(delete=False, suffix=".mp4")
original_video_file.seek(0)
temp_input_file.write(original_video_file.read())
temp_input_file.close()

ffmpeg_command = [
'ffmpeg', '-i', temp_input_file.name,
'-vcodec', 'libx265', '-crf', '32',
'-f', 'mp4', '-movflags', 'faststart',
'-y', # Overwrites output file if it exists
'-strict', 'experimental', 'output.mp4'
]

print("*************** COMPRESSING VIDEO *******************")
subprocess.run(ffmpeg_command, check=True)

with open('output.mp4', 'rb') as compressed_file:
compressed_video = ContentFile(compressed_file.read())

print("******************** UPLOADING COMPRESSED *****************")
url = upload_file(compressed_video, key)

return Response({"url": url}, status=status.HTTP_200_OK)

except Exception:
return Response({'result': 'failed to delete file from media server'}, status=status.HTTP_502_BAD_GATEWAY)
except Exception as e:
return Response({"result": f"Error processing video: {str(e)}"}, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
@authentication_required
Expand Down
4 changes: 2 additions & 2 deletions zubhub_frontend/zubhub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"html2pdf.js": "^0.10.1",
"i18next": "^23.6.0",
"i18next-browser-languagedetector": "^7.1.0",
"i18next-http-backend": "^2.2.2",
"intl-tel-input": "^18.2.1",
"i18next-http-backend": "^1.3.2",
"intl-tel-input": "^17.0.13",
"lodash": "^4.17.21",
"nanoid": "^3.3.1",
"pdfmake": "^0.2.7",
Expand Down
Loading