How to access metadata of a file in Python using tinytag
Hello friends, in this tutorial, I will tell you how to access the metadata of a file in Python.
Access the metadata of a file in Python using tinytag
I have used the tinytag package, which is used for reading music metadata of most common audio files. The supported formats are :
- MP3/MP2/MP1 (ID3 v1, v1.1, v2.2, v2.3+)
- Wave/RIFF
- OGG
- OPUS
- FLAC
- WMA
- MP4/M4A/M4B/M4R/M4V/ALAC/AAX/AAXC
- AIFF/AIFF-C
For mp3 file you can also use: How to access MP3 metadata in Python
To install the package, open the command prompt in Windows or Terminal in macOS/ Linux and execute the command.
pip install tinytag
Once installed, import the TinyTag module from the tinytag
package.
from tinytag import TinyTag
I have used the get()
function. This function takes the file’s name as an argument. I have then stored the result in a temporary variable, audio
. If I print the variable, I will get the metadata for the audio file.
Code :
audio = TinyTag.get("C:\Perfect(Mr-Jatt1.com).mp3") print(audio)
Output :
{"album": "Perfect - Ed Sheeran - Mr-Jatt1.com", "albumartist": null, "artist": "Mr-Jatt1.com", "audio_offset": 72704, "bitdepth": null, "bitrate": 160.0, "channels": 2, "comment": "Downloaded from http://mr-jatt1.com", "composer": "www.mr-jatt1.com", "disc": null, "disc_total": null, "duration": 261.2082, "extra": {}, "filesize": 5296996, "genre": "Club", "samplerate": 48000, "title": "Perfect(Mr-Jatt1.com)", "track": null, "track_total": null, "year": "2022"}
You can also retrieve specific values, by calling the attributes.
Code :
print(audio.album) print(audio.genre) print(audio.artist) print(audio.duration)
Output :
Perfect - Ed Sheeran - Mr-Jatt1.com Club Mr-Jatt1.com 261.2082
Thus you now know how to access metadata of a file in Python.
Leave a Reply