I want to rename mp3 files on my mac using python before importing them to iTunes. So I need to change the "Title" of the file, not the file's name. As in, I want to change "Al-Fatihah" in the picture below to "new_title".
Most online resources and question that I found suggest using either external libraries or using os.stat()
which only gives info about modification and creation of the file (second picture below), unless I'm misunderstanding something. I was wondering if there is a way to do so without having to download extra libraries as I'm not always sure which libraries are safe.
Thanks!
If you don't use a library, you're gonna have to go in and manually edit the bytes yourself. The 'title' you're referring to is an ID3 tag, which is a standard defining which parts of the mp3 file contain data about the track.
In the case of ID3v1, the last 128 bytes of the file are reserved for metadata, and bytes 4 to 34 are reserved for the title.
Manually writing bytes in python is an absolute pain, so I strongly, strongly recommend using a library for this menial task. eyeD3 is a library that can do this for you. If you are not "sure which libraries are safe", why don't you have a look at the source code for these libraries to check that they're safe yourself?
If you really, must absolutely edit them using only python, you'd have to go about it like this. I'm pasting this answer from another question about manipulating bytes here. This is not an exact solution, more of a guideline of what manually editing the bytes would entail:
with open("filename.mp3", "r+b") as f:
fourbytes = [ord(b) for b in f.read(4)]
fourbytes[0] = fourbytes[1] # whatever, manipulate your bytes here
f.seek(0)
f.write("".join(chr(b) for b in fourbytes))