2

I have a rest API built with Django rest framework, one of its serializers is to accept Base64file which is our audio file, now what I want is simply check and validate the decoded file so I can know if this a valid mp4 or any audio type in general or not.

the problem is, sometimes the audio file after upload and save is corrupted and can't be played, so doing this validation is essential to make sure that the file is sent correctly or it was sent corrupted at first place.

I have been digging on google and the Internet searching for anything can do this simple task but all I found was how to play audio or manipulate it, I didn't even find something that may raise an exception if the file is not valid when trying to open it.

for more info. I'm using django-extra-fields, I use Base64FileField to implement my Audio file field, they provided an example to do so for like PDF's, I'm trying to do this similar way for audio but what is holding me is doing the check for audio.

The example of PDF:

class PDFBase64File(Base64FileField):
    ALLOWED_TYPES = ['pdf']

    def get_file_extension(self, filename, decoded_file):
        try:
            PyPDF2.PdfFileReader(io.BytesIO(decoded_file))
        except PyPDF2.utils.PdfReadError as e:
            logger.warning(e)
        else:
            return 'pdf'

What is done so far:

class AudioBase64File(Base64FileField):
    ALLOWED_TYPES = (
        'amr',
        'ogg',
        'm4a',
        '3gp',
        'aac',
        'mp4',
        'mp3',
        'flac'
    )
    INVALID_FILE_MESSAGE = ("Please upload a valid audio.")
    INVALID_TYPE_MESSAGE = ("The type of the audio couldn't be determined.")

    def get_file_extension(self, filename, decoded_file):
        # missing validation
        return 'mp4'
Mahmoud Adel
  • 1,262
  • 2
  • 13
  • 23
  • Linux has program `file` which used `file filename` can check what is inside this file - result i.e `filename: Audio file with ID3 version 2.4.0, contains:MPEG ADTS, layer III, v1, 64 kbps, 44.1 kHz, Stereo` – furas Jan 07 '20 at 12:35
  • `ffmpeg` has program `ffprobe` which used `ffprobe filename` can get many information and one line has ie. `Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s`. There is also `ffmpeg-python` module - so maybe it also can do the same. – furas Jan 07 '20 at 12:37
  • good rule: first uses Google. Using Google "python test file type" it shows many links. One of them [How to check type of files without extensions in python?](https://stackoverflow.com/questions/10937350/how-to-check-type-of-files-without-extensions-in-python/24433682) And there is even link to [python-magic](https://github.com/ahupp/python-magic) – furas Jan 07 '20 at 12:40
  • @furas thank you for your replies and these helpful info you shared with me, but I'm afraid it's not answering what the question is about, there is no physical file to check and I'm not looking to get the type of file, I'm looking to know if this decoded file is a valid or not, means if this file is not corrupted after the process of encoding the file at client side and decide on the server, so I can accept it or reject the submission with error, please take a look again on the code example for PDF – Mahmoud Adel Jan 09 '20 at 12:16
  • You can save data in file on disk and check it - so instead of saving on disk after checking you save it before checking and later keep good file or delete wrong file. You can also check if some functions can use file-like object and then you can use `io.BytesIO` to create file in memory - like in PDF example. – furas Jan 09 '20 at 13:09
  • @furas good ideas, I appreciate you help, thanks – Mahmoud Adel Jan 16 '20 at 07:21

1 Answers1

2

You can use ffmpeg.

You can read the file and see if there is any error or not. ffmpeg will report any error while reading the file.

You can also skip some parts of the video just to make it faster but reading a file without doing anything is pretty fast and should be good enough.

ffmpeg -v error -i file.mp4 -f null - 2>error.log

How can I check the integrity of a video file (avi, mpeg, mp4…)?

ffmpeg

Navid Zarepak
  • 4,148
  • 1
  • 12
  • 26
  • this helpful if there is a physical file for sure, but I want to do this for the decoded file, it would be greate if I can do it in a similar way of the PDF code example in question – Mahmoud Adel Jan 09 '20 at 12:18
  • 1
    I couldn't find anything that can validate a video like that. Or at least nothing stand-alone. And if there is any, it probably does the decode/encode itself all over again. So it might be a good idea to stick with something simple while looking for a more optimized way. Good luck. – Navid Zarepak Jan 09 '20 at 12:37
  • Thank you for your help, I appreciate it – Mahmoud Adel Jan 16 '20 at 07:19