LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 06-06-2004, 12:51 AM   #1
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Rep: Reputation: 15
Piping to ttoogg in bash script


I have, at the moment, a rather huge mp3 collection that I would like to convert to ogg. I looked around sourceforge and came up with ttoogg http://ttoogg.sourceforge.net/ . It is a perl script that takes in mp3s, calls up a bunch of programs, and spits out an ogg. It works nicely, save for the fact that I can't specify an entire root directory for it to do it's thing with. As it is now I would have to ttoogg each directory containing mp3s individually.

I have 4000 or so directories to do and I'm not going to do them individually.

The obvious solution is to then write a nice bash script to do the work for me. That's where my problem is. What I want to do is search through every folder recursively until an mp3 is found, convert the mp3 upon finding it, and then delete the original mp3 upon conversion. My idea was to find the mp3s was to use this in my script.

Code:
#encode all mp3s to ogg
find / -name '*.mp3' | ttoogg
The only problem is that I cannot get anything piped to ttoogg. I'm pretty sure got it to work earlier, but then walked off, did something else, and forgot how I got it to work. Now coming back to the problem I've gotten stuck in one of those infinite loop things. "Well, what I just did didn't work a few seconds ago, but what about now?"

Could someone explain to me why I can't get anything piped out?

I do realize that there is the -exec option for find, but ttoogg always places the oggs in the current working directory, which does me no good.

On a side note I'm still learning bash scripting, so anything I churn out probably isn't going to be the most elegant. If you can think up some better way, by all means tell me. I'll be very happy to get different solutions in. (Which I'm sure sounds like, "Do this for me!" )
 
Old 06-06-2004, 02:24 AM   #2
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,127

Rep: Reputation: 75
Why not:
find . -name '*.mp3' -exec ttoog {} \;
 
Old 06-06-2004, 02:38 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Piping find won't do what you want. Essentially what your doing is shoving straight text into the ttoogg program.

Another problem would come up if any of your files have real spaces in their names. Screwing with word lists in a bash script trying to access files with spaces has always given me a headache.

I would suggest combining the script and the find command. Instead of:
find / -name "*.mp3" -exec ttoogg {} \;
^^^^^^ I know that's not what you want because of the current directory thing

You could try this:
find / -name "*.mp3" -exec /usr/local/scripts/cd_and_convert.sh {} \;

The contents of the script might look something like this:
Code:
#!/bin/bash

# Get the filename to convert from the command line
filename=$1

#Pull the directory from the filename
absolute_path=???

#Go to the directory
cd ${absolute_path}

#Execute the conversion
ttoogg ${filename}

#Check to see if the conversion was a success
???

#Remove the original mp3 file
rm -f ${filename}

exit 0
I leave the ??? for you to fill out because it might need something special you would have to put in (or I can't think of something right off-hand).
 
Old 06-06-2004, 02:55 AM   #4
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,127

Rep: Reputation: 75
Quote:
Originally posted by Dark_Helmet
Piping find won't do what you want. Essentially what your doing is shoving straight text into the ttoogg program.
woops' I thought it would take full path as an argument. But I see what you mean now about the files all being in the same dir.

Just to add a little to this script:

#Pull the directory from the filename
absolute_path=dirname $filename;
.........

Last edited by micxz; 06-06-2004 at 02:57 AM.
 
Old 06-06-2004, 03:34 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Hehe... to be honest, I don't know how the ttoogg program works It very well might take a full path, but the "goofy" said that it would convert the mp3 and place the ogg in the current directory. My immediate reaction when reading the post originally was to offer the -exec option too, and then after some thought, realized that it would dump all those ogg's into the directory he ran find from. I'm guessing he ran into that problem first-hand
 
Old 06-06-2004, 12:55 PM   #6
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
I have followed Dark_Helmets suggestion heavily and my command looks like so
Code:
blt@qwerty: find / -name '*.mp3' -exec /home/blt/goose.sh {} \;
with the script being
Code:
#!/bin/bash

#read input from stdout
file_name=$1

#rip out file name to give its location
absolute_path=`dirname ${file_name}`

cd ${absolute_path}

#start converting
ttoogg ${filename}

#if ttoogg exited without errors then delete mp3
if [ $? -eq 0]
then 
    rm *.mp3
else
    echo "woops"
fi

exit 0
It works fine, unless the files have whitespace in them. If there's whitespace then dirname chops off the file name after the whitespace, rather than at the start of the file name.

Anybody have any suggestions as to how I could A) work around the above problem or B) replace all whitespaces with an underscore, which would work just fine.
 
Old 06-06-2004, 01:09 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Ok, for the directory names with spaces, change this:
absolute_path=`dirname ${filename}`

to this:
absolute_path=`dirname "${filename}"`

In fact, it would probably be a good idea to surround every use of a variable with double quotes.

Now, something to mention about the rest of the script. The mp3 files will be processed one at a time, not all in one command. So the "rm *.mp3" is a bad idea unless each mp3 is held in its own directory. The script would convert one mp3, and then delete everything else in the dir; probably not what you want.

You might also consider commenting out the mv command and replacing it with an "echo" so you can visually verify what the script would execute. And when you're satisfied, uncomment the mv command. The "safe" version:
Code:
then
  echo rm "${filename}"
  #rm ${filename}
else
The "go" version:
Code:
then
  #echo rm "${filename}"
  rm ${filename}
else
<edit>
NOTE: for the safe version, you would want to comment out and echo the ttoogg command as well.
</edit>
 
Old 06-06-2004, 01:34 PM   #8
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,127

Rep: Reputation: 75
for the spaces:

nospaces = `sed -e 's/ /-/g'`
 
Old 06-06-2004, 05:44 PM   #9
goofyheadedpunk
Member
 
Registered: Aug 2003
Distribution: Arch Linux
Posts: 140

Original Poster
Rep: Reputation: 15
Thanks for all the help, especially Dark_Helmet's. I'm currently in the process of saving about 10 gigs on a 40 gig by converting my mp3 collection to an ogg collection.

I did, however, keep rm '*.mp3' in. Each mp3 is not held in its own directory, but if I do rm ${filename} then I get caught in a loop. All the mp3s in the directory that's being processed are not deleted until the last file is encoded, so rm '*.mp3' works fine. I suppose it has something to do with the way ttoogg exits, but I'm just guessing.

It matters not though, I do have backups on my 80 gig... just in case.

Thanks again for all the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple bash piping problem nadroj Linux - General 3 09-10-2005 03:12 PM
piping to and 'error' window from termalan (shell script) dna3e8 Linux - Software 2 03-19-2005 11:08 AM
Exim - piping mail sent to a particular address to a script Khang Linux - Software 2 01-31-2005 11:04 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


All times are GMT -5. The time now is 11:35 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration