I've written a MOVE TO MUSIC LIBRARY script..

Come up with a useful post-processing script? Share it here!
thewump
Newbie
Newbie
Posts: 6
Joined: January 24th, 2011, 10:20 pm

I've written a MOVE TO MUSIC LIBRARY script..

Post by thewump »

Hi All,

I've written a bash script that does a really a good job of taking any MP3 files from anywhere and sorting them into [library]/artist/album/XX - title.mp3 and am using it in my own sabnzb install, but wanted to know:

1) Is there interest in this?
2) In your opinion should it COPY the tracks to the music directory and leave the originals in the sabnzbd download directory or MOVE the files and delete the download directory?

It's pretty much ready to go, but would like to know what the preference is for this so I can make it great.

K
qwerty2k9
Newbie
Newbie
Posts: 2
Joined: June 22nd, 2009, 3:52 pm

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by qwerty2k9 »

I'd appreciate a script like that.. how about giving the user the option of a copy or move.. and then when they're happy that its doing everything properly they can switch to the move
thewump
Newbie
Newbie
Posts: 6
Joined: January 24th, 2011, 10:20 pm

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by thewump »

OK.. here it is. I use Linux and I think this should work fine on Mac. Windows you are probably SOL.

It can Move or Copy and assumes that music is in /home/you/Music

Save this as mp3organizer in your Post Processing Directory
chmod +x mp3ogranizer


Good luck peeps.

Code: Select all

#!/bin/bash


# Where is your music?
music_collection="$HOME/Music" 


# SABNZBD usage
# 1) Edit music_collection setting above if your music isn't in the "normal" place
# 2) Set this up as an SABNZBD Post Processing script

# GENERAL usage
# 1) Edit music_collection setting above if your music isn't in the "normal" place
# 2) run in a terminal as: ./mp3organizer /path_to/where_the_new_music_files/are 
# Note that the path can't have any spaces.

# NOTES AND ADVANCED USAGE
# File with bad tags are stored to /[Your_music_collection]/unknown

# See the Advanced Options settings if you want to:
# a) Make backup copies
# b) Move instead of copy file from the new music location

# =============================================================================================
# ADVANCED / OPTIONAL SETTINGS
# Want all the files copied somewhere else too? 
#backup_dir="$HOME/mp3organizer_backups"

# Set to "copy" or "move" to make the script either copy files to your music collection and leave
# the originals where they were or move them to your music collection.
mode="copy"

# If you want the unknown files to be saved somewhere else, or to change where this script
# keeps temporary files, change these:
unknown_dir="$music_collection/unknown" 
working_dir="$HOME/.mp3organizer_temporary_files"


# =============================================================================================
# EVERYTHING BELOW HERE IS AS IT SHOULD BE. DONT MESS WITH IT

# Make sure that a folder has been passed to the script
if [ -z "$1" ];then
  echo "Doh!  You have to pass a directory where the music is you want to organize!"
  exit
else	
  echo "Working from supplied directory"
  echo "$1"
	new_music="$1"
fi

# Make sure if a folder has been passed that it actuall exists
if [ ! -d "$new_music" ];then    
  echo "Can't find the search directory $new_music"
  echo "For SABNZBD, in the admin go to Settings > Switches and set REPLACE SPACES WITH UNDERSCORSES"
  exit
fi

# Check other folders exist and make them if necessary
if [ ! -d "$working_dir" ];then    
  echo "Creating $working_dir"
  mkdir $working_dir
fi

if [ ! -d "$music_collection" ];then    
  echo "Creating $music_collection"
  mkdir $music_collection
fi

if [ ! -d "$unknown_dir" ];then    
  echo "Creating $unknown_dir"
  mkdir $unknown_dir
fi

if [ ! -d "$backup_dir" ];then    
  echo "Creating $backup_dir"
  mkdir $backup_dir
fi

# Copy or Move the new music files to our temporary working area to be processed
if [ "$mode" = 'move' ]; then
  find $new_music -iname "*.mp3" -exec mv {} $working_dir \;
else
  find $new_music -iname "*.mp3" -exec cp {} $working_dir \;
fi

cd $working_dir

# Work on each file one at a time
for F in ./*
do
	if [ -f "$F" ];then				
    # use mminfo to get the track info
    # LOTS of stuff going on here. Have to remove leading space, special chars and pad track to 0x etc.
    # Someone smart could do this a lot more elegantly than this!
		artist=`mminfo "$F"|grep artist|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
		title=`mminfo "$F"|grep title|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
		album=`mminfo "$F"|grep album|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g'`
		trackno=`mminfo "$F"|grep trackno|awk -F: '{print $2}'|sed 's/^ *//g'| awk '{printf "%02d\n", $1;}'`

	  echo "============================"
		echo "artist:" $artist
		echo "title:" $title
		echo "album:" $album
		echo "trackno:" $trackno

      # If we have an artist, album and title then we know where to nicely put this file
			if [  -n "$artist"  ] && [ -n "$album" ] && [  -n "$title"  ];then
          # This is what we are going to call the file
  				filename="$trackno - $title.mp3"; 
			  	if [ -n  "$backup_dir" ];then
			          cp "$F" "$backup_dir/"
				  fi
  				# If the right directory structure exists - awesome. Move the file.
					if [ -d "$music_collection/$artist/$album" ];then    
						mv "$F" "$music_collection/$artist/$album/$filename"  
					else
					  # If not, build the whole file structure. Won't do any harm to try and create an artist directory that
					  # is already there - so not even checking for that.
						mkdir "$music_collection/$artist"
						mkdir "$music_collection/$artist/$album"
						mv "$F" "$music_collection/$artist/$album/$filename"
					fi
			else
        # Don't know who it's by, what album, or what track, so moving it to UNKNOWN
  			mv "$F" "$unknown_dir/"
				echo -e "UNKNOWN: $F \n"
			fi
	fi
done
Last edited by thewump on January 29th, 2011, 8:25 pm, edited 1 time in total.
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by Mar2zz »

I like this script. Are you fine with it if I use it and expand it? (also with replaygaining things and stuff)
thewump
Newbie
Newbie
Posts: 6
Joined: January 24th, 2011, 10:20 pm

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by thewump »

Go for it! 

Best

K
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by Mar2zz »

Thx. Hope it will work. Improving namingscheme and stuff. so far this. working on check for empty tags now. Too bad mminfo uses a deprecated md5 thing, it spams the terminal with warnings (but it still works). Also a bummer it doesn't recognize the albumartist tag, so it alway's has to go to Various Artists.

Code: Select all

PATHSAB=$1

cp_Workingdir () {
WORKDIR=/tmp/tagwork
mkdir $WORKDIR
find $PATHSAB \( -name *.mp3 -o -name *.ogg -o -name *.flac \) -exec cp {} $WORKDIR \;
}

list_Files () {
TAGFILES=$(find $WORKDIR \( -name *.mp3 -o -name *.ogg -name *.flac \) -type f)
for track in $TAGFILES
do
genre=$(mminfo $track|grep genre|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g')
date=$(mminfo $track|grep date|awk -F: '{print $2}'|sed 's/^ *//g'| awk '{printf "%04d\n", $1;}')
album=$(mminfo $track|grep album|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g')
trackno=$(mminfo $track|grep trackno|awk -F: '{print $2}'|sed 's/^ *//g'| awk '{printf "%02d\n", $1;}')
artist=$(mminfo $track|grep artist|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g')
title=$(mminfo $track|grep title|awk -F: '{print $2}'|sed 's/^ *//g'|sed 's/[^a-zA-Z0-9\ \-\_]//g')
ext=$(find $track \( -name *.mp3 -o -name *.ogg -o -name *.flac \) -type f | sed 's/.*\.//g')
echo $artist >> $WORKDIR/artist.txt
done
}

set_Renaming (){
NORMAL="$artist/\($date\) $album/$trackno. $artist - $title.$ext"
SOUNDTRACK="Soundtrack/$album \($date\)/$trackno. $artist - $title.$ext"
ALBUMARTIST="Various Artists/\($date\) $album/$trackno. $artist - $title.$ext"
}

check_Albumartist () {
filecount=$(find $WORKDIR \( -name *.mp3 -o -name *.ogg -name *.flac \) -type f | wc -l)
artistcount=$(grep -m $filecount "$artist" $PATHSAB/artist.txt | wc -l)
}

check_Renaming () {
if [ $genre = Soundtrack -o $genre = soundtrack ]
    then
    SCHEME=$SOUNDTRACK
elif [ $filecount -eq $artistcount ]
    SCHEME=$NORMAL
else
    SCHEME=$ALBUMARTIST
fi
}
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by Mar2zz »

I gave up on this way of moving music. That deprecated message is annoying me.
Found an easier way with arename (on ubuntu sudo apt-get install arename).
When it doesn't find tags you can easy replace them with other values (like empty year tag with 0000). It will not move files if one of the tags in the template is missing and no other default value is specified. So it's also safe to use...

Make a file called .arenamerc in your homedirectory and fill it like this:

Code: Select all

  # switch on verbosity
  verbose

  # canonicalize file names before working with them
  canonicalize

  # the author is crazy! use a sane template by default. :-)
  template        &artist/(&year) &album/&tracknumber. &artist - &tracktitle
  comp_template   Various Artists/&album (&year)/&tracknumber. &artist - &tracktitle
  
  #set default values for tags if empty
  #default_artist
  #default_album
  #default_compilation
  #default_genre
  #default_tracknumber
  #default_tracktitle
  default_year 0000
  


  # activate the 'music' profile below /mnt/audio/music/.
  #profile test /mnt/audio/music/

  # force files from /foo/bar/ to stay below that directory
  prefix /home/user/Music
Create a postprocessingscript, like moveMusic.sh and make it executable (sudo chmod +x moveMusic.sh) and paste the following inside:

Code: Select all

#!/usr/bin/env bash
# Author:  Mar2zz
# blogs: mar2zz.tweakblogs.net
# License: GNU GPL v3
# 
# Using arename for this. (sudo apt-get install arename)
# http://ft.bewatermyfriend.org/comp/arename/arename.html
#
# Don't forget to use /home/user/.arenamerc if you want custom naming templates and other options.
#


$DIR=$1

$ARENAME="arename -c" #### remove -c if you want to delete sourcefiles
find $DIR \( -name *.mp3 -o -name *.ogg -o -name *.flac \) -type f -exec $ARENAME {} '+'
thewump
Newbie
Newbie
Posts: 6
Joined: January 24th, 2011, 10:20 pm

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by thewump »

ARENAME=NICE FIND.

Have you found a way for it to consider genre:soundtrack as a compilation though? Testing here shows OST files being treated just like normal files and stored under /artist

K
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by Mar2zz »

You should do a dryrun first (arename -d | grep -i "soundtrack\| ost \| movie score") or words like that and embed in an if-statement.

If it finds that word you can execute arename -rc ~/.arenamerc-ost and create a special .arenamerc-ost with the contents of the normal one, but with your own renaming template (e.g. Soundtracks/&album &year/$tracknumber. &artist - &track)

Think the best option for that is this by the way:
Maybe you could also edit your normal .arenamerc and name a template in there, like ost_template Soundtracks/&album &year/$tracknumber. &artist - &track
then run the template from commandline like arename -t ost_template.
Let me know if this works or if you need help writing the extra stuff in the script. If it works I will add to my default one.
thewump
Newbie
Newbie
Posts: 6
Joined: January 24th, 2011, 10:20 pm

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by thewump »

Here's some info from the writer of ARENAME.. friendly guy ;-)  I think I might stick with my script for now. I've changed it to dump mminfo once to a temp file and then grep it so that error message is under control.  I'm enjoying the ability to easily pre-process on the tags and make filename and path decisions around:

- genre = "Speech" or "Audiobook"
- genre = "Soundtrack" or "Sound Track" or "Soundtracks"
- TPE2 = "Various Artists" (compilation - exposed if you use mminfo -d 1)





keith Hunniford wrote:
> Hey,  This thing is great!

Hey there. Glad you like it. Also good to know, that the user base isn't
just me and the guy who packages it for debian. :)

May I ask where you got it from? Debian/Ubuntu? From github? Which
version are you running? If you're running off of github, make sure
you're using the `arename3' branch. I've started to work on `master'
again a week ago. So it has improved, but it's still fairly experimental
here and there.

> I have a question that I'm sure can be solved by hooks, but I don't
> get it.

Sure, there are different ways to do this. The `pre_template' and
`pre_expand_template' come to mind.

> Basically I would love to be able to specify effectively:
>
> if genre = "Soundtrack"
>    compilation = true
> end
>
> so effectively, soundtracks would have the compilation_template applied
> to them.

The `pre_template' hook is run before the template-type for the current
file is chosen. If you'd put anything into the `compilation' tag at that
point, the compilation-type template would be chosen.

Here's some code for that:

sub soundtrack_is_compilation {
    my ($n, $dat, $ext) = @_;

    return if ($dat->{genre} !~ m/^Soundtrack$/i);
    if (!defined $dat->{compilation} || $dat->{compilation} eq '') {
        $dat->{compilation} = 'force_compilation';
    }
}

register_hook('pre_template', \&soundtrack_is_compilation);

The problem with that is, that it may look crappy, it your compilation
template is using the "&compilation" tag.


The `pre_expand_template' is run *just* before the template is being
expanded. And it has access to the template string, so you could just
drop in the template string you'd like before it expands the one it
chose itself.

You could do it roughly like this:

sub soundtrack_is_compilation {
    my ($n, $tmpl, $dat) = @_;

    return if ($dat->{genre} !~ m/^Soundtrack$/i);
    $$tmpl = get_opt('comp_template');
}

register_hook('pre_expand_template', \&soundtrack_is_compilation);


I think, the latter solution is the one, I'd use.

Note, that I didn't test any of this. Use --dry-run before until it
actually works.

Regards, Frank

--
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by Mar2zz »

Lol, he whips out bash like it's cookies. I don't see what he is trying to do with that code (I ve learned bash 2 weeks ago, so big noobie here..). Didn't know he is on github, I think I have a simpler solution with the repo version. I am testing right now.

Here is my solution. it works  ;)
You can use whatever configfile you like, you just need to define it. I created one for soundtracks with my soundtrackstemplate and prefix /home/mars/Music/Soundtracks and called it .arenamerc-ost. (you can do the same for audiobooks ofcourse)
Here is working code:

Code: Select all

DIR=$1


#### first verbose dryrun (no filechanges happen, but outputs all tags it can find)
if find "$DIR" \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f -exec arename --dryrun --verbose {} '+' | grep -i "soundtrack\|movie score"
	then
	echo "Soundtrack found"
	find $DIR \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f -exec arename --rc $HOME/.arenamerc-ost {} '+'
elif find "$DIR" \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f -exec arename --dryrun --verbose {} '+' | grep -i "audiobook"
	then
	echo "Audiobook found"
	find $DIR \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f -exec arename --rc $HOME/.arenamerc-book {} '+'
else
	echo "Normal files found"
	find $DIR \( -name "*.mp3" -o -name "*.ogg" -o -name "*.flac" \) -type f -exec arename --rc $HOME/.arenamerc {} '+'
fi
Of course this can fail whenever it finds a song with soundtrack in the title but is not really a soundtrack... Though that wouldn't happen often I think.
(of course you can output the "genre-lines from the verbose log" with grep to a textfile (grep -i "genre:" > tempfile.log) and then grep that file for soundtrack or audiobook and put that in an ifstatement to choose an configfile)
Last edited by Mar2zz on February 7th, 2011, 1:56 pm, edited 1 time in total.
User avatar
john3voltas
Release Testers
Release Testers
Posts: 115
Joined: January 17th, 2008, 5:35 pm
Location: Lisbon/Portugal

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by john3voltas »

Mar2zz wrote: Lol, he whips out bash like it's cookies. I don't see what he is trying to do with that code (I ve learned bash 2 weeks ago, so big noobie here..).
Biiiig noobie here too :).
So sorry to go off topic, but where did you get bash info in these past 2 weeks?
What did you read to get to grips with bash?
TIA
Cheers
SABnzbd 0.6.0Alpha11 on Fedora 14-64bit laptop.
Usenet-News, TeraNews, newszilla6.xs4all.nl and reader.ipv6.xsnews.nl.
IPv6 connections powered by Hurricane Electric.
Can pull 30Mbit nntp on a 30Mbit FTTH link.
User avatar
Mar2zz
Jr. Member
Jr. Member
Posts: 85
Joined: February 4th, 2011, 8:30 am
Contact:

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by Mar2zz »

[offtopic]
http://mywiki.wooledge.org/EnglishFrontPage
http://www.faqs.org/docs/Linux-HOWTO/Ba ... HOWTO.html
and a lot of google, with terms like "bash list only moviefiles in folder" for example.

21st of januari I posted my first installscript for Sickbeard (very simple commands in a file, no ifstatements and functions and so on) on my dutch blog (mar2zz.tweakblogs.net) and asked people to comment on it and give feedback. That was very helpfull and I learned to use functions from that. I also opened two topics on tweakers.net to get help with some things and learned a lot from that (case-statements and while loops for example). Basically it's like having an idea and google like hell to find alle commands needed to bash it  ;)[/offtopic]
User avatar
john3voltas
Release Testers
Release Testers
Posts: 115
Joined: January 17th, 2008, 5:35 pm
Location: Lisbon/Portugal

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by john3voltas »

Thanks a bunch, man. ;)
SABnzbd 0.6.0Alpha11 on Fedora 14-64bit laptop.
Usenet-News, TeraNews, newszilla6.xs4all.nl and reader.ipv6.xsnews.nl.
IPv6 connections powered by Hurricane Electric.
Can pull 30Mbit nntp on a 30Mbit FTTH link.
jgourd
Newbie
Newbie
Posts: 18
Joined: April 21st, 2011, 8:43 am

Re: I've written a MOVE TO MUSIC LIBRARY script..

Post by jgourd »

I just can't make any of these work.

Code: Select all

Normal files found
find: `/media/sdc1/Downloads/complete/Music/Lee': No such file or directory
find: `Mead': No such file or directory
find: `-': No such file or directory
find: `Nothing': No such file or directory
find: `Else': No such file or directory
find: `Matters': No such file or directory
find: `(2009)': No such file or directory
It seems to not like spaces in the folder name when passed from SabNZB
Post Reply