I want to resize the video and picture files. so I looked for it and found FFmpeg
and now I'm getting errors while compressing. TemporaryUploadedFile
don't have a path when we upload. so how can I do that?
- Error
FileNotFoundError: [WinError 2] The system cannot find the file specified
- Code
import os
import subprocess
import ffmpeg
MEDIA_MAX_UPLOAD_SIZE = 5242880
def check_media_size(media):
media_size = media.size
if media_size < MEDIA_MAX_UPLOAD_SIZE:
return media_size
@api_view(["POST"])
def medias_upload(request):
if request.method == 'POST':
received_media=request.FILES.getlist('media')
allowed_mime_types = ['image/jpeg','image/png','image/gif','image/jpg','video/mp4','video/mkv','video/mov','video/avi']
upload_data = []
if received_media:
for media in received_media:
received_media_mime_type = mime_type_check(media)
received_media_size = check_media_size(media)
if received_media_mime_type in allowed_mime_types and received_media_size:
new_uuid = uuid.uuid4()
if str(received_media_mime_type).split('/',1)[0] == "image":
file_type = str(received_media_mime_type).split('/',1)[1]
converted_uuid = subprocess.run(f'ffmpeg -i {media} -acodec {file_type} temp_location/{new_uuid}')
upload_data.append(converted_uuid)
if str(received_media_mime_type).split('/',1)[0] == "video":
file_type = str(received_media_mime_type).split('/',1)[1]
converted_uuid = subprocess.run(f'ffmpeg -i {media} -acodec {file_type} temp_location/{new_uuid}')
upload_data.append(converted_uuid)
else:
return Response({"response": False, "return_code":"Failed", "result": {}, "message": "Wrong MIME-TYPE or MAXIMUM file size allowed 5MB"}, status=status.HTTP_404_NOT_FOUND)
return Response({"response": True, "return_code": "success", "result": {"media_uploaded_uuid":upload_data}, "message": success["success"]}, status=status.HTTP_200_OK)
return Response({"response": False, "return_code":"Failed", "result": {}, "message": "No Data Found"}, status=status.HTTP_404_NOT_FOUND)
- Q . Is there any better way to compress video and picture file size?