Solving Real World Problems with Python
Most of the projects on this blog so far have been fun, but not very useful in everyday life. I am actually currently working on another fun, but mostly useless project which is going to take some time, so in the meantime, lets take a look at a small project I made that really is useful. I mainly want to show in this post that programming isn't just for fun project ideas, but can also help you solve real problems in your daily life.
The problem that I encountered recently has to do with music, specifically mp3 files. Let's say you download a song in a perfectly legal way and try to import it into your music player of choice. Anyone who has done this knows that a lot of time, the way it is imported is a mess. The title is often wrong, as well as even the artists and album names. Most mp3 files have all this information at the beginning of the file stored in something called ID3 tags, but depending on where you downloaded it from, it won't always be correct. Fortunately, this is very easy to solve with a little bit of code. Python has a great module named eyeD3 which allows you to manipulate and correct the data. Below is a short script I wrote to do so.
import eyed3
import os
import sys
dir = sys.argv[1]
artist = input("Artist Name: ")
album = input("Album Name: ")
songs = []
def main():
for filename in os.listdir(dir):
if not filename.endswith("jpg"):
file_path = os.path.join(dir,filename)
mp3 = eyed3.load(file_path)
if mp3.tag:
if artist != '':
mp3.tag.artist = artist
mp3.tag.title = filename
if album != '':
mp3.tag.album = album
track_num = input("{} is track: ".format(filename))
mp3.tag.track_num = track_num
mp3.tag.save()
if __name__ == "__main__":
main()
This simple script allows me to import all my favorite albums into my music player in an organized manner. I give the script a folder full of mp3 files, I type in the artist name and album name for the songs in that folder, then it gives me all the the songs and I tell it which track number that song should be in the album. I also set the song title to be whatever the filename for that song is. When that is done, all the files have their metadata updated and can now be imported smoothly into my music player. This is an extremely quick and dirty script with no error handling and can break extremely easily, but just for my own personal use, it's fine. I just wanted to show how simple it can be to fix real world problems with just a few minutes of coding. Our next project will be much larger and will finally get into the CyberSecurity part of this blog so stay tuned.