Denis
Denis

Reputation: 669

How to find broken mp3 files with python 3?

I have my personal mp3 collection which partially damaged after my HDD broken. I need find names of damaged mp3s inside quite a lot (aprox. >5k files) in folders and sub-folders.

Can you please give me a tip about python 3 libraries which can open mp3 file, read it and find bit rate issues in it.

Upvotes: 2

Views: 839

Answers (2)

Denis
Denis

Reputation: 669

For my purposes ideally fits mutagen here is solution with comments. Everything is pretty simple.

Upvotes: 0

a_guest
a_guest

Reputation: 36309

For the various steps, I can give you the following hints.

File names

For obtaining mp3 file names the glob module is your friend: glob.iglob('*.mp3', recursive=True).

Dealing with mp3

For dealing with mp3 files you can use practically any command line utility that serves your needs. A few examples:

You can run these tools from within python via the subprocess module. For example:

subprocess.check_output(['avprobe', 'path/to/file'])

Then you can parse the output as appropriate; how to detect if the file is broken needs to be explored though.

Dive into mp3

If you're feeling adventurous then you can also scan the mp3 files directly. Wikipedia gives a hint about the file structure. So in order to get the bit rate the following should do:

with open('path/to/file', 'rb') as fp:
    fp.read(2)  # Skip the first two bytes.
    bit_rate = fp.read(1) & 0xf0

Upvotes: 2

Related Questions