This week we will talk about Audio-Renamer project.
This project is a solution to the problem, every music lover would have faced in their computer. Unknown music files! Files named like "1.mp3", "file2.mp3"... It is really a headache. Even if you play them, you might not figure out the title or artist of the song. Definitely not a way to enjoy the music!
If you have an unknown music file, one of the applications using AcoustID will very likely be able to tell you what song it is, who wrote it, and a lot more. Python module pyacoustid is used to connect with AcoustID web API. One of the purposes of this project is to get familiar with web API.
A Guide to the code
Before diving in deep, let's walk through the code. In essence, the following happens inside the code:
- Gets a list of mp3 files from the given directory
- Makes fingerprints of mp3 files
- Checks the fingerprint in the database and get details
- Asks user suggestion to rename
- Renames the mp3file based on user input
Let's start at the very beginning.
The crucial input of this project is a directory that contains mp3 files which need to be renamed. In order to do access mp3 files from directory 'os' python module used. This module provides a portable way of using operating system dependent functionality.
def mp3files(path): "Returns mp3 files from the given directory." return [f for f in os.listdir(path) if ".mp3" in f]
os.listdir is used to get the files from the path and if it contains ".mp3" it will be added to list. I am really thrilled by these list comprehensions. They shorten the code remarkably. This will definitely excite you, if you are coming across it for the first time.
Another method in 'os', os.rename() is later used to rename the files.
Now mp3 files have to be changed into acoustic fingerprints. An acoustic fingerprint is a condensed digital summary, that can be used to identify an audio sample or quickly locate similar items in an audio database.
Acoustid python module is used for fingerprinting the mp3file:
acoustid.fingerprint_file(path): Using either the Chromaprint dynamic library or the fpcalc command-line tool, fingerprint an audio file. Returns a pair consisting of the file's duration and its fingerprint.
The returned duration and fingerprint with the API-key is used to look up details of the song in acoustid database.
lookup(apikey, fingerprint, duration): Make a request to the Acoustid API to look up the fingerprint returned by the previous function. An API key is required, as is the length, in seconds, of the source audio. Returns a parsed JSON response.
def process_file(file_name): "process a file and returns information about the audio." (duration, fingerprint) = acoustid.fingerprint_file(file_name) return acoustid.lookup('EXyveY7Q4B', fingerprint, duration)
It is easy to extract and use required, once the JSON response is returned. To make the application more productive, it interacts with the user. With user inputs file renames as the user expects. Application renames the original file in order to avoid unnecessary files. All the information is printed onto the terminal for better understanding