3

I want to check if a audio file to see if its MP3 or FLAC the checks only have to be basic but I want to go beyond simply checking the file extensions

os.path.splitext

Works okay but no good if the file doesn't have an extensions written in or someone passes a file with a fake extension

I've tried but it just returns None

sndhdr.what(file)

I've also tried using magic but it returns 'application/octet-stream' which isn't much use.

magic.from_file(file, mime=True)

I've read Mutagen could be good for this but so far failed to find any function that outputs the audio encoding as MP3 or FLAC

twigg
  • 3,753
  • 13
  • 54
  • 96

4 Answers4

5

To find the File format including audio, video, txt and more, you can use fleep python library to check the audio file format:

First you need to install the library:

pip install fleep

Here is a test code:

import fleep

with open("vida.flac", "rb") as file:
    info = fleep.get(file.read(128))

print(info.type)
print(info.extension)
print(info.mime) 

Here is the results:

['audio']
['flac']
['audio/flac']

Even if the file doesn't have an extension or someone passes a file with a fake extension, it will detect and return.

Here I have copied the vida.wav file to dar.txt and also removed the extension

import fleep

with open("dar", "rb") as file:
    info = fleep.get(file.read(128))

print(info.type)
print(info.extension) 
print(info.mime) 

The result is still the same.

['audio']
['flac']
['audio/flac']

Credit to the author of the library https://github.com/floyernick/fleep-py

Stryker
  • 5,732
  • 1
  • 57
  • 70
2

This might help you getting started

21.9. sndhdr — Determine type of sound file

https://docs.python.org/2/library/sndhdr.html

jester112358
  • 465
  • 3
  • 17
  • 1
    I did take a look at this earlier but when passing it my test mp3 file sndhdr.what(file) it returns none – twigg May 19 '15 at 10:06
  • 1
    sndhdr only works with certain file formats: `The value for type indicates the data type and will be one of the strings 'aifc', 'aiff', 'au', 'hcom', 'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul'. ` src: [https://docs.python.org/3.5/library/sndhdr.html] – Roopesh90 Oct 23 '15 at 16:39
1

Try the library filetype. filetype on pypi. filetype on github.

(Another answer mentioned fleep. I tried that. But unfortunately, it looks like it isn't maintained anymore. It doesn't recognise all the types of mp3, for example. filetype is actively maintained and recognises all the mp3 formats)

Install filetype

pip install filetype

Use filetype to recognise your file. It uses file signatures, a.k.a magic bytes at the start of the file.

import filetype

kind = filetype.guess('path/to/sample.mp3')
if kind is None:
    print('Cannot guess file type!')
else:
    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime)

NOTE, there is an issue

It seems like the filetype repo while actively maintained is slow to make releases. I raised an issue on GitHub about the slow releases here.

This means to get the new bits of the library (like more file signatures to match with), you might want to install directly from the git repo.

To do this with pip:

pip install -e git+https://github.com/h2non/filetype.py

To do this with pipenv:

pipenv install -e git+https://github.com/h2non/filetype.py#egg=filetype

(-e means install in editable mode. That comes as a recommendation from here.)

Donal
  • 8,430
  • 3
  • 16
  • 21
0

You can read file specifications of mp3 and flac and then can implement a checker that reads and parses mp3 as a binary file.A modifiable example with a similar goal is here

woryzower
  • 956
  • 3
  • 15
  • 22