Ubuntu – Rename Music Files with Missing File Extensions

filesystemmusic

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 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/{**/,}*
Related Question