Rename Music Files with Missing File Extensions
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.
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
where type/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.
#!/bin/bash
# usage:
# fixmime ./{**/,}*
file --mime-type "$@" |
while read -r line; do
file=${line%:*}
type=${line##* }
case $type in
#Audio
audio/x-flac) ext=flac;;
audio/mpeg) ext=mp3;;
application/ogg) ext=ogg;;
#Video
video/mp4) ext=mp4;;
video/x-flv) ext=flv;;
application/octet-stream) ext=webm;;
application/x-shockwave-flash) ext=swf;;
#Images
image/png) ext=png;;
image/jpeg) ext=jpg;;
image/gif) ext=gif;;
image/x-ico) ext=ico;;
#Text
text/plain) ext=md;; #markup your notes, even when they are plain
text/html) ext=html;;
text/x-pascal) ext=py;;
text/x-c++) ext=js;; #conflicts with .cpp, so use prefered
text/x-c) ext=scss;; #usualy it is .c
text/x-shellscript) ext=sh;;
application/pdf) ext=pdf;;
#Fonts
application/x-font-ttf) ext=ttf;;
application/vnd.ms-opentype) ext=otf;;
#Archive
application/x-gzip) ext=tar.gz;;
application/x-bzip2) ext=tar.bz;;
*) continue;;
esac
[[ $file = *.$ext ]] || mv -i -- "$file" "$file.$ext"
done
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
~/bin/rename-based-on-content-type /path/to/music/directory/{**/,}*
This bash script could do the trick:
#! /bin/bash
find ~/Music -type f | (while read path; do
case `file -i "$path"` in
*audio/mp3*|*audio/mpeg*) ext='.mp3' ;;
*application/ogg*) ext='.ogg' ;;
*flac*) ext='.flac' ;;
*) continue;; # ignore unknown files
esac
newname="$(dirname "$path")/$(basename "$path" "$ext").$ext"
if [ "$path" != "$newname" ]; then
mv -v "$path" "$newname"
fi
done)
The script uses file -i
to read the MIME type of a file; I have no
FLAC files here, so you might want to check what the MIME type for
FLAC is by running file -i a_file.flac
(and possibly adjusting the script source).
You can add an option -i
to mv
if you want to be asked
confirmation before any file rename.
It might be a good idea to replace mv -v
by echo mv -v
and doing a
test run before trying the actual renames -- the echo mv
will
print out the mv
commands that would be executed by the script.