Bhoj
Bhoj

Reputation: 41

Does python-magic package works to check mime-type of video file of format .mp4?

I am trying to use python-magic package to identify the uploaded file type(using mime-type), I have used it to identify image and audio file it works fine but for the video file, it throws an error.

For video file I have

class VideoFileUploadForm(forms.ModelForm):
    def clean(self):
        cleaned_data = super(VideoFileUploadForm, self).clean()

        upload_file = cleaned_data['upload']

        try:

            if upload_file:

                supported_types = ['video/mp4', 'video/x-matroska',
                                   'video/ogg','video/quicktime', 'video/x-ms-wmv',
                                   'video/webm']

                mimetype_of_file_uploaded = magic.from_buffer(upload_file.file.getvalue(), mime=True)

                val = 0

                for item1 in supported_types:

                    if item1 == mimetype_of_file_uploaded:
                        val = 1
                        break

                if val == 0:
                    raise ValidationError(u'Error! File can only be .mp4, .mkv,.ogg,.mov ,.wmv and .webm(video)  format')
        except (RuntimeError, TypeError, NameError,AttributeError) as e:
            print(e)
            raise ValidationError("Error! Something is wrong.File should be .mp4,"
                                  " .mkv,.ogg,.mov ,.wmv and .webm(video) format!")

    class Meta:
        model = VideoFileUpload
        fields = (
            'file_name',
            'upload',
        )

    def __init__(self, *args, **kwargs):
        super(VideoFileUploadForm, self).__init__(*args, **kwargs)

        self.fields['upload'].widget.attrs = {
            'class': 'btn  btn-block',
            'name': 'myCustomName',
            'placeholder': 'Upload file',
            'required': 'true'
        }

This code works fine if I supply image mime types and audio mime types in the place of "supported_types" but now for video, it does not support what could be the reason

Error is like this: '_io.BufferedRandom' object has no attribute 'getvalue'

File I tried to upload is of .mp4 format which plays perfectly in media player

Or, Is there any other way to validate .mp4(video) file format in python/django before uploading which is better than this ?

Upvotes: 0

Views: 1689

Answers (1)

Bhoj
Bhoj

Reputation: 41

I have solved it using django, instead of using python-magic , I used django built in mime type checker .Django provides mime in the forms of content type To find content type i did

 if upload_file:

            # supported format pdf, msword,mobi,txt ,ott,epub
            # mp4, mkv(x-maroska),ogg,.mov(quicktime),.wmv,webm
            supported_types = ['video/mp4', 'video/x-matroska',
                               'video/ogg','video/quicktime', 'video/x-ms-wmv',
                               'video/webm']



            mimetype_of_file_uploaded = upload_file.content_type

The content_type give the mime type in django.

Upvotes: 1

Related Questions