#!/bin/bash # This script will walk a folder recursively and: # - if it encounters a .mp3 file with metadata, the file will be copied to the target folder # - if it encounters a .ogg file with metadata, a .mp3 file will be created with metadata in the target folder # - files (.mp3 or .ogg) that are missing metadata are reported # # required packages: sox, mp3info, ogginfo # this script was based on info found at: # - http://www.linuxweblog.com/convert-ogg-to-mp3 # - http://ubuntuforums.org/showpost.php?p=9859875&postcount=8 # - http://www.fell.it/2009/11/28/convert-ogg-files-to-mp3/ # # author: dirk@onesparrow.com # source and target folders export OGG2MP3_SOURCE_FOLDER="/home/dirk/music" export OGG2MP3_TARGET_FOLDER="/home/dirk/music1" function handleFile (){ NEW_FILE=`echo $1 | sed "s|$OGG2MP3_SOURCE_FOLDER|$OGG2MP3_TARGET_FOLDER|g" | sed 's/.ogg/.mp3/g'` PARENT_FOLDER=`dirname "$NEW_FILE"` echo $1 "=>" $NEW_FILE mkdir -p "$PARENT_FOLDER" case $1 in *.mp3) # just copy to target if it is an mp3 # skip when metadata is missing (mp3info will output "does not have ..." to stderr) if [[ `mp3info "$1" 2>&1 |grep "does not have an ID3 1.x tag"|wc -l` -eq 1 ]] then echo " => NO METADATA, SKIPPING ..." else cp "$1" "$NEW_FILE" fi ;; *.ogg) # convert and add metadata # extract metadata from .ogg and add it to .mp3 OGGINFO=`ogginfo "$1"` OLD_IFS=$IFS IFS=" " TITLE="Unknown" ALBUM="Unknown" ARTIST="Unknown" TRACKNUMBER="Unknown" DATE="Unknown" for line in $OGGINFO do if [[ $line == *TITLE=* ]] then TITLE=${line#*=} elif [[ $line == *ALBUM=* ]] then ALBUM=${line#*=} elif [[ $line == *ARTIST=* ]] then ARTIST=${line#*=} elif [[ $line == *TRACKNUMBER=* ]] then TRACKNUMBER=${line#*=} elif [[ $line == *DATE=* ]] then DATE=${line#*=} fi done IFS=$OLD_IFS if [[ $TITLE == "Unknown" ]] then echo " => NO METADATA, SKIPPING ..." else sox "$1" "$NEW_FILE" mp3info -a "$ARTIST" -l "$ALBUM" -n "$TRACKNUMBER" -t "$TITLE" -y "$DATE" "$NEW_FILE" fi ;; *) echo " => UNEXPECTED FILE TYPE, SKIPPING";; esac } export -f handleFile # walk the tree find $OGG2MP3_SOURCE_FOLDER -type f -exec bash -c 'handleFile "{}"' \;