I've been trying out a few different music players recently and noticed that some (a lot) of my music would be missing from the library. It turns out that at some point, a plethora of my music files lost their .mp3/.ogg/.flac file extensions. I'm honestly not sure how this happened, but I'm confident it was something I did mistakenly (or maybe I thought it wouldn't matter since file extensions are mostly cosmetic anyway).
So I need to get these file extensions back. I've looked into pyrenamer, but I can't figure out how to match it to files without an extension and then tell it to add the correct file extension depending on the actual type of the file.
I've also looked at EasyTag. However, it also recognizes music files based entirely on their file extensions. So the music I want to fix doesn't even show up. Brilliant.
Any thoughts on how to do this? I certainly don't mind some command line, I'm just not sure which tools would be best and I also suck at regex.
Best Answer
Here's a bash script that renames the files passed to it based on their guessed format. It calls
file
to figure out the format by looking at characteristic patterns in the first few bytes.file -i
prints lines like/path/to/file: type/subtype
wheretype/subtype
is a MIME type. The script then associates extensions to known types and renames the file to have the extension. Files that already have the extension are left alone. Files that have an unrecognized type are left alone. The script will prompt before overwriting a target file.Save the script as
~/bin/rename-based-on-content-type
(or whatever you like) and make it executable (chmod +x ~/bin/rename-based-on-content-type
). Pass the names of the files you want to rename to the script on the command line. If you have a directory tree/path/to/music/directory
that you want to traverse recursively, invoke the script as