Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: FLAC to MP3 Encoding Script

  1. #11
    Join Date
    Nov 2004
    Location
    Anywhere
    Beans
    Hidden!

    Re: FLAC to MP3 Encoding Script

    Thanks

    Great Work indeed.

  2. #12
    Join Date
    Aug 2007
    Location
    Torrington, Wyoming
    Beans
    372
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: FLAC to MP3 Encoding Script

    Is there any way you could set it up so i can select the song in nautilus and have it convert it right there. I know it is possible but I don't know enough about bash scripting to be able to make it work.
    すべてと、そして、すべてへのリナックスは自由を鳴らせました。
    Linux to all, and to all, let freedom ring.

  3. #13
    Join Date
    Jan 2008
    Beans
    41

    Re: FLAC to MP3 Encoding Script

    I must be doing something wrong with the flac to mp3 (new version) as it's giving me the error '19: Syntax error: "}" unexpected' after Lame does it's thing.

    UPDATE:

    Came downstairs this morning and it appears to have worked!

    http://ubuntuforums.org/showthread.php?t=499045
    explains what I was seeing...

    Many thanks.

    UPDATE 2:

    ... but Nitin Sawhney appears to have inherited Anthrax's summary data.
    Last edited by Fynn; September 17th, 2008 at 07:50 PM.

  4. #14
    Join Date
    Jun 2008
    Location
    France
    Beans
    34
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: FLAC to MP3 Encoding Script

    Hello all,

    Reviving this thread a bit. I updated the script from Tom Mann to include a kind of "MP3 folder cleanup".

    Basically anything that is recorded in the database and not present anymore in the flacdir will be removed.

    Code:
    #!/bin/bash
    #!/bin/sh
    
    # flactomp3.sh v0.01
    # converts all flac files recursively found at $flacdir
    # and creates MP3s of them at mp3dir, following any
    # existing directory structure.
    #
    # Requirements
    # lame, flac.
    
    flacdir="/media/Musique/Librairie/"
    mp3dir="/media/Musique/Baladeur/"
    dbfilename="$mp3dir.convert_db"
    logfile="$mp3dir.log"
    
    function createmp3 {
          flac -dc "$1" | lame -V 5 -h \
          --tt "$TITLE"\
          --tl "$ALBUM"\
          --tn "$TRACKNUMBER"\
          --ty "$DATE"\
          --ta "$ARTIST"\
          --ty "${DATE%%-*}"\
          --id3v2-only\
          --replaygain-accurate\
          --ignore-tag-errors\
          - "$2"
    	  chmod 775 "$2"
    	  chgrp users "$2"
    }
    
    function getmd5 {
      md5=`md5sum $1`
      flacmd5="${md5%% $1}"
      flacmd5="${flacmd5/[/}"
      flacmd5="${flacmd5/]/}"
    }
    
    function readmd5 {
      md5=`cat "$dbfilename" | grep $1`
      storedmd5="${md5%% $1}"
      storedmd5="${storedmd5/[/}"
      storedmd5="${storedmd5/]/}"
    }
    
    function storemd5 {
      if [ ! "x$storedmd5" == "x" ] # x added as "" is not a valid input for a test
      then
        sed -i 's|'"$storedmd5  $barefile"'||g' "$dbfilename"
        cat "$dbfilename" | awk '$0!~/^$/ {print $0}' "$dbfilename" > "$dbfilename".tmp
        mv "$dbfilename.tmp" "$dbfilename" # remove blank lines
        # TODO: the sed statement above can break if there is more than one of the same entry!
      fi
      echo "$flacmd5 $1" >> "$dbfilename"
    }
    
    function makedir {
      if [ ! -d "$1" ]
      then
        mkdir -p "$1"
    echo "Folder $1 created." >> "$logfile"
    	fi
    }
    
    # test to see if the flac md5 database exists: if not create it
    if [ ! -f "$dbfilename" ]
    then
      touch "$dbfilename"
    fi
    
    if [ ! -f "$logfile" ]
    then
    	touch "$dbfilename"
    else
    	rm "$logfile"
    	touch "$logfile"
    fi
    
    # test to see if the mp3 directory exists: if not create it
    makedir "$mp3dir"
    
    # main loop - we go into directories, make sure the mp3
    # equivalent directory exists, create it if not, then
    # convert each file across.
    
    export IFS=$'\n'
    
    #MP3 Database cleanup
    for dir in $(find "$mp3dir" -type d)
    do
    	if [ ! "$dir" == "$mp3dir" ]
    	then
    
    		#Remove any empty folder
    		rmdir --ignore-fail-on-non-empty "$dir"
    		
    		#Get the artist & artist/album pattern
    		dir="${dir#$mp3dir}"
    
    
    		#Select only the tracks matching the above pattern
    		for track in $(cat "$dbfilename" | grep "$dir/")
    		do
    			#Retrieve track from the database
    			track="${track#*  }"
    			
    			#Check if the folder still exists in $flacdir
    			if [ ! -d "$flacdir$dir" ]
    			then
    				
    				#Remove the entry if not
    				sed -i 's|'".*$track"'||g' "$dbfilename"
    				cat "$dbfilename" | awk '$0!~/^$/ {print $0}' "$dbfilename" > "$dbfilename".tmp
    				mv "$dbfilename.tmp" "$dbfilename" # remove blank lines
    
    				#Remove the folder if not removed before
    				if [ -d "$mp3dir$dir" ]
    				then
    echo "$dir has been removed from \$mp3dir" >> "$logfile"
    					rm -r "$mp3dir$dir"
    				fi
    			
    			#Folder still exists
    			else
    				#Check if file still exists
    				if [ ! -f "$flacdir$track.flac" ]
    				then
    					#Remove the entry if not
    					sed -i 's|'".*$track"'||g' "$dbfilename"
    					cat "$dbfilename" | awk '$0!~/^$/ {print $0}' "$dbfilename" > "$dbfilename".tmp
    					mv "$dbfilename.tmp" "$dbfilename" # remove blank lines
    					
    					#Remove the file if it still exists
    					if [ -f "$mp3dir$track.mp3" ]
    					then
    echo "$track has been removed from \$mp3dir" >> "$logfile"
    						rm "$mp3dir$track.mp3"
    					fi
    				fi
    			fi
    		done
    	fi
    done
    
    for flac in $(find "$flacdir" -type f)
    do
    	# check we're dealing with a .flac
    	if [ "${flac##/*.}" == "flac" ]
    	then
    		track="${flac#$flacdir}"
    		track="${track%.flac}"
    		
    		# check that the file does not have a matching entry in our md5 database
    		getmd5 "$flac"
    		readmd5 "$track"
    		if [ ! "$storedmd5" == "$flacmd5" ]
    		then
    
    			# replace flac directory structure with mp3 directory structure
    			mp3="$mp3dir$track.mp3"
    
    			# retrieve tags
    			metaflac --export-tags-to=- "$flac" | sed 's/=\(.*\)/="\1"/' > /tmp/flacmeta.tmp
    			. /tmp/flacmeta.tmp
    			rm /tmp/flacmeta.tmp
    echo "Tag values: $ARTIST/($DATE) $ALBUM/$TRACKNUMBER. $TITLE" >> "$logfile"
    			# check target directory exists, creating it if necessary
    			targetdir="${mp3%/*.mp3}"
    			makedir "$targetdir"
    
    			# convert flac to mp3, preserving tags
    			createmp3 "$flac" "$mp3"
    
    
    			# finally store the md5 in our database as we have
    			# created the mp3 successfully.
    			if [ $? == 0 ]
    			then
    echo "$flac correctly encoded to $mp3" >> "$logfile"
    				storemd5 "$track"
    			fi
    		fi
    	fi
    done
    export IFS=" "
    So far it works well. If it can be of any use to somebody...

    Ah, one thing, if anyone knows how to benefit from a multicore/hyperthreaded CPU, it would be nice ! (I have an Atom 330, 2 cores with HT)

    I was thinking of creating a separate script for the encoding and launching 2 or 3 cronjobs in the same time, but that's a bit, errhhh, wild, and I wonder how to handle possible conflicts.

    Well, we'll see !
    Cheers

  5. #15
    Join Date
    Jul 2008
    Location
    Banville :(
    Beans
    119

    Re: FLAC to MP3 Encoding Script

    Useful stuff. I'll see if I can be bothered to add in some getopts so people can set the "in" and "out" directories from the commandline, a bit like "mp3Demflacs -i /home/user/flacs/ -o /home/user/mp3s".

  6. #16
    Join Date
    Aug 2010
    Beans
    0

    Re: FLAC to MP3 Encoding Script

    Quote Originally Posted by monsterstack View Post
    Useful stuff. I'll see if I can be bothered to add in some getopts so people can set the "in" and "out" directories from the commandline, a bit like "mp3Demflacs -i /home/user/flacs/ -o /home/user/mp3s".
    try something like this

    Code:
    #!/bin/bash
    #!/bin/sh
    
    # flactomp3.sh v0.01
    # converts all flac files recursively found at $flacdir
    # and creates MP3s of them at mp3dir, following any
    # existing directory structure.
    #
    # Requirements
    # lame, flac.
    
    flacdir="/media/Musique/Librairie/"
    mp3dir="/media/Musique/Baladeur/"
    
    while [ ! -z "$1" ]; do
        case $1 in
            -i | --input_file )
                shift
                flacdir=$1
                ;;
            -o | --output_file )
                shift
                mp3dir=$1
                ;;
        esac
        shift
    done
    
    dbfilename="$mp3dir.convert_db"
    logfile="$mp3dir.log"
    
    [...]
    Last edited by DarkWan; August 20th, 2010 at 08:58 PM.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •