mp3 metadata: Python

Python learning progress is going fine. I have been following many tutorials. I found one awesome reference – “Hacking Secret Ciphers with Python”, it has helped me a lot with more coding and fun approach. My friend asked me if I could write a python script that will give the information of all mp3 files residing in a folder: names and its play duration. Another request was to output all details to json on a text file.

I got the reference of one media library: Mutagen, from stackoverflow. I downloaded and installed it. I found Mutagen very effective to get the Metadata of mp3 files so far; I haven’t done much so far.

import os
import json
from mutagen.mp3 import MP3

class paddy_audio:
    def __init__(self, name, duration):
        self.name = name
        self.duration = duration

data_dict = []

for dirname, dirnames, filenames in os.walk('songs'):
    for filename in filenames:
		audio = MP3("songs/"+filename)
		name = audio.info.length
		obj = paddy_audio(filename, name)
		data_dict.insert(0, obj)

with open('mp3_details.txt', 'w') as outfile:
	outfile.write(json.dumps(data_dict, default=lambda o: o.__dict__))

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>