0

I'm getting this error every time I'm trying to upload video file, and I think the problem is come from the moviepy library that I'm using to cut every video that are higher than 10 minute.

the error: AttributeError at /CreateVideo 'TemporaryUploadedFile' object has no attribute 'get'

views:

from moviepy.video.io.VideoFileClip import VideoFileClip
from django.core.exceptions import ValidationError

def create_video(request):
    if request.method == 'POST':
        title = request.POST['title']
        video = request.FILES['video']
        banner = request.FILES['banner']

        video_file = video
        video_clip = VideoFileClip(video_file.temporary_file_path())
        duration = video_clip.duration
        video_clip.close()
        if duration > 600: # 10 minute in seconds
            raise ValidationError('the video cannot be longer than 10 minute')
        return video_file

        new_video = Video.objects.create(
            user=request.user,
            title=title,
            video=video,
            banner=banner
        )
        new_video.save()
        return redirect('Video')

    return render(request, 'video/create_video.html')

models:

class Video(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,  on_delete=models.CASCADE)
    title = models.CharField(max_length=70)
    video = models.FileField(upload_to='videos')
    created_on = models.DateTimeField(auto_now_add=True)
    banner = models.ImageField(upload_to='banner')
    slug = models.SlugField(max_length=100, unique=True)

1 Answers1

0

For Future User:

The error message indicates that the TemporaryUploadedFile object does not have a get() method. This is likely because the temporary_file_path() method is returning a temporary file path string instead of an actual file object.

To fix this issue, change the video_clip initialization line from:

video_clip = VideoFileClip(video_file.temporary_file_path())

to:

video_clip = VideoFileClip(video_file)

But using that method would return an error:

'TemporaryUploadedFile' object has no attribute 'endswith'

To fix this issues you need to check if the file type of the video uploaded is supported by the library you are using (in this case, VideoFileClip from moviepy). You can also check if the file is being uploaded correctly by checking its file size or by printing out its contents like this:

from moviepy.video.io.VideoFileClip import VideoFileClip

def create_video(request):
    if request.method == 'POST':
        title = request.POST['title']
        video = request.FILES.get('video')
        banner = request.FILES.get('banner')

        if video:
            video_type = video.content_type.split('/')[0]
            if video_type != 'video':
                raise ValidationError('File is not a video')
            video_ext = video.name.split('.')[-1]
            if video_ext not in ['mp4', 'avi', 'mov']:
                raise ValidationError('Unsupported video format')

            video_clip = VideoFileClip(video.temporary_file_path())
            duration = video_clip.duration
            video_clip.close()
            if duration > 600: # 10 minute in seconds
                raise ValidationError('the video cannot be longer than 10 minute')

            new_video = Video.objects.create(
                user=request.user,
                title=title,
                video=video,
                banner=banner
            )
            new_video.save()
            return redirect('Video')
        else:
            raise ValidationError('No video file uploaded')

    return render(request, 'video/create_video.html')

moviepy Docs