LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2011, 01:52 PM   #1
Fred_mike
Member
 
Registered: Oct 2010
Posts: 95

Rep: Reputation: Disabled
Bash beginner, please help.


I need to convert some .flac files into .mp3.

I found this script:


#!/bin/bash
FLAC=$1
MP3="${FLAC%.flac}.mp3"
[ -r "$FLAC" ] || { echo can not read file \"$FLAC\" >&1 ; exit 1 ; } ;
metaflac --export-tags-to=- "$FLAC" | sed 's/=\(.*\)/="\1"/' >tmp.tmp
cat tmp.tmp
. ./tmp.tmp
rm tmp.tmp
flac -dc "$FLAC" | lame -b 192 -h --tt "$Title" \
--tn "$Tracknumber" \
--tg "$Genre" \
--ty "$Date" \
--ta "$Artist" \
--tl "$Album" \
--add-id3v2 \
- "$MP3" </code>

And I need it to run it from outside of a directory for several directories.

I need to run it from /dir for /dir/dire*

Please help.

Thanks
 
Old 06-20-2011, 02:15 PM   #2
cpuobsessed
Member
 
Registered: Jan 2006
Distribution: Fedora, Ubuntu, Slackware
Posts: 56

Rep: Reputation: 15
Just make sure that wherever you put the script is in your $PATH or just use the full path name and make sure it is marked executable:
Code:
chmod +x flactomp3.sh
and
Code:
 /path/to/script/flactomp3.sh
 
Old 06-20-2011, 02:21 PM   #3
Fred_mike
Member
 
Registered: Oct 2010
Posts: 95

Original Poster
Rep: Reputation: Disabled
done it, thanks anyway.
 
Old 06-20-2011, 02:28 PM   #4
Fred_mike
Member
 
Registered: Oct 2010
Posts: 95

Original Poster
Rep: Reputation: Disabled
I ran

for dir in DIRECTORY; do cd "$dir"; for file in *.flac; do sh /home/name/mp3.sh "$file"; done; cd ..; done

Last edited by Fred_mike; 06-20-2011 at 02:53 PM.
 
Old 06-21-2011, 09:38 PM   #5
mehorter
LQ Newbie
 
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18

Rep: Reputation: 3
Hey now,

I wrote this script to convert flac/shn to mp3's and rename them according to the setlist often given in the accompanying txt file. I know this can be improved but it works for me. I also wrote scripts to check the md5 and/or ffp files, another one to convert flac/shn to wav, rename as above, copy over in alpha-numeric-timestamp order to a usb stick so they'll play in my car radio in the correct order. Oh and one to convert 24bit flac to 16bit... Any way here is my mp3 script, give it a whirl and let me know how it works for you.

Code:
#!/bin/bash
#f2mp3
 
# The intention of this script is to decode flac/shn files to a
# alpha-numericly renamed and tagged mp3 file as indicated by 
# the setlist txt file that often is included in such file 
# packages.

# Your original lossless directory and contents are left unchanged and will
# continue to pass md5sum.

# Srcipt assumes you are in root dir of the flac/shn files to be processed.

while getopts ":y:" opt; do
    case $opt in
        y ) year=$OPTARG
            ;;
        esac
done



echo -e "\nPick the file that contains the setlist \n "
IFS=$'\n'
select file in $(find . -size -100k -type f)
do
    txtfile=$(echo "$file"|sed 's/ /\./g') #substitute spaces for '.'
   break
done
IFS=" "

# Define a regular expression that only describes the song titles in your 
# txtfile the default, for instance, ^d[1-3]t[0-9]{2}, means every line that starts d1t01, 
# d1t02, ....d3t09.
echo
read -e -p "Enter your RegEx. Default - Enter to accept or edit : " -i "^d[1-3]t[0-9]{2} " regex

read -e -p "Enter the Artist tag. Default -Enter to accept or edit : " -i "Grateful Dead" artist

#read -e -p "Enter the 'year of issue' tag :"  year


#############################################################

#cleanup previous attempts
rm *wav 2>/dev/null
rm *mp3 -fr 2>/dev/null
mkdir mp3

#############################################################

a=1   #counter to choose txt file line for use in renaming
b=101 #track number
    
for i in *{shn,flac}
do
    #if *shn or *flac doesn't exist goto next file in *{shn,flac}
    [ -e "$i" ] || continue
    
    #pick one line from txt file, call it "new" and use to rename wav file
    new="$(sed -nr "/$regex/p" $txtfile |sed -e's/ - / /' -e's/\[.*\]//' \
                    -e's/[ ]*$//' -e's/\//-/g' -e's/:/-/' -e's/->//'\
                    -e's/[*#%-\r]//g'  -e's/[ \t]*$//'|sed -n "$a"p)"
    echo 
    #strip begining of line to first space then insert "$b" (track#) to front
    new="$b$(echo $new | awk '{ $1 = "";print }')"
    
####################################################    
# declare some tags for the mp3
    tt=${new##$b } #titletag
    ta=$artist   #artist tag
    tl=$(album=$(basename $PWD); echo "${album%.*}") #album tag
    [ -e $year ] && ty=$year     #year of issue tag
    tn=$b   #track number tag
    tg=17        #genre tag - defaulted to "rock"                              
    
# convert flac or shn file to wav
    shntool conv  "$i" 
    echo
    
# convert wav file to mp3
    lame --vbr-new --preset standard --tt "$tt" --ta "$ta" \
        --tl "$tl" --ty "$ty" --tn "$tn" --tg "$tg" --add-id3v2 \
        *wav "$new.mp3" 
    rm *wav
    mv -v "$new.mp3" mp3/"$new.mp3"
    
    ((a++))  #get next txt file line number
    ((b++))  #get next track number
done

cp $txtfile mp3/

exit 0
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading a value from a textfile using bash script (beginner problem) AndrewJS Linux - Newbie 1 06-10-2011 07:16 PM
Beginner question, Bash-script - date kickarzt Programming 15 03-10-2010 12:18 PM
Bash substitution -- Fairly beginner stuff! ipguru99 Programming 4 01-01-2008 04:31 PM
What is the best book for a beginner to learn his way 'round BASH??? Fabyfakid Linux - Newbie 8 10-15-2004 11:04 PM
beginner archangel Linux - Security 11 09-03-2001 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration