LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-28-2008, 04:47 PM   #1
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 58

Rep: Reputation: 15
Need assistance with shell script to pull MP3 file


I have found two great sites to get exposure to new music, every few days they publish a few demo tracks for people to evaluate; it's worked really well for me except I am having to hit the site every week to pull the files down by hand and I just want this to happen automatically. If the files get pulled down automatically, my MP3 player can wirelessly synch them up, so it's 100% hands free!

The sites I am interested in pulling down are:
http://www.thesoundculture.com/
http://iodacast.libsyn.com/

Here is the approach I have been aiming for, however if you find a better way to do this please let me know.

I can pull down all the files on the site using the following command:

wget -O - http://iodacast.libsyn.com/ | grep -o 'http://redirect2.iodalliance.com/download_track.php[^"]*' | xargs -- wget --

However the downloads have 2 problems
  • The files have random names (F274DC803EBB10CCF4DB9E5D82FF19CCC5769FB48F02482ED1BC5AFA3D6C9B73), and the actual track name is stored in the files "Title" property.
  • The files could be MP3, AVI or other formats (I only care about the MP3s)

I know I can use the file command to identify the file type, but I have no idea how to have the script add the extension for me.
file FD4C82CE040B6EF56CEC7ED2FD9142ECA848DBF7D2A6D6BF9C01AE3CDDFF18AB
FD4C82CE040B6EF56CEC7ED2FD9142ECA848DBF7D2A6D6BF9C01AE3CDDFF18AB: Audio file with ID3 version 23.0 tag, MP3 encoding


Lastly, I would also like to get the actual file name for the file from the "Title" attribute of the file (EXIF?), and rename the file something more logical.

If anyone can help with any particular part I would appreciate it!

Thanks!
cpare
 
Old 06-28-2008, 05:42 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
To add the extension you have to concatenate the string ".mp3" to the file name. Suppose in your script you have the file name stored in the variable FILE. You simply have to
Code:
mv $FILE ${FILE}.mp3
For the title part, I'd suggest to try eyeD3. You can rename the file to the Title read from the id3 tag using a single command
Code:
eyeD3 --rename=%t $FILE
where %t stats for the Title. Other format specifications are available, as %A (artist), %a (album), %n (track number), and %N (the total track count). In this way you can build the file name using the tag info, provided that they are correctly written into the mp3 file.
 
Old 06-28-2008, 05:56 PM   #3
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 58

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
To add the extension you have to concatenate the string ".mp3" to the file name. Suppose in your script you have the file name stored in the variable FILE. You simply have to
Code:
mv $FILE ${FILE}.mp3
For the title part, I'd suggest to try eyeD3. You can rename the file to the Title read from the id3 tag using a single command
Code:
eyeD3 --rename=%t $FILE
where %t stats for the Title. Other format specifications are available, as %A (artist), %a (album), %n (track number), and %N (the total track count). In this way you can build the file name using the tag info, provided that they are correctly written into the mp3 file.

Thanks for the concat piece, however I will still have to use the file command to determine if the file is an MP3. Any suggestion on how I can cycle through each of these files checking each one, and renaming the MP3 ones?
 
Old 06-28-2008, 06:31 PM   #4
FranDango
Member
 
Registered: Jun 2008
Posts: 101

Rep: Reputation: 15
This can be done with a shell script if you are using other tools like awk etc. But I'm more comfortable with PHP, and PHP scripts can also run on the command-line.

My approach with PHP would be to call wget or curl via a system call, analyzing the downloaded file by piping the "file" command and extracting the resulting string with split, substr or other PHP calls. Then rename the file and adding an extension. Have a look at the documentation at php.net.

Renaming the file or adding an extension to it just a matter of "mv" on the command-line:

mv FD4C82CE040B6EF56CEC7ED2FD9142ECA848DBF7D2A6D6BF9C01AE3CDDFF18AB my.mp3

Linux Archive

Last edited by FranDango; 09-20-2008 at 04:47 AM.
 
Old 06-28-2008, 07:29 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
In BASH, suppose you have a directory where you've downloaded the files and a directory where you want to temporarily store the mp3 files. You can use a while loop over the list of downloaded files (the list coming from the find command) and for each file check the result of the file command and act accordingly. For example:
Code:
#!/bin/bash
download_dir=/path/to/download/dir
archive_dir=/path/to/archive/dir
while read file
do
  if file "$file" | grep -q "Audio file with ID3 version 23.0 tag, MP3 encoding"
  then
    mv "$file" "$archive_dir/$(basename "$file").mp3"
    eyeD3 --rename "%A - %t" "$archive_dir/$(basename "$file").mp3" > /dev/null
  fi
done < <(find $download_dir -type f)
 
Old 06-28-2008, 08:53 PM   #6
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks for the help everyone, I just added the code colucix posted to my line that fetched all the files, and things seem to be working.

Code:
#!/bin/bash
download_dir=.
archive_dir=/home/cpare/Music/One\ Off\ Tracks/

# This fetches all the tracks at this site
wget -O - http://iodacast.libsyn.com/ | grep -o 'http://redirect2.iodalliance.com/download_track.php[^"]*' | xargs -- wget --

# Now we need to convert the bogus names we just downloaded to something meaningful
while read file
do
  if file "$file" | grep -q "Audio file with ID3 version 23.0 tag, MP3 encoding"
  then
    mv "$file" "$archive_dir/$(basename "$file").mp3"
    eyeD3 --rename "%A - %t" "$archive_dir/$(basename "$file").mp3" > /dev/null
  fi
done < <(find $download_dir -type f)
If anyone finds this useful, or improves this script, please post your changes or reply to this thread.

Thanks again!
 
  


Reply

Tags
mp3, script, sh, shell


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Shell script assistance required.. TheEngineer Linux - Newbie 4 10-26-2006 06:42 AM
How to access mp3 bitrate info in a shell script? ppk007 Linux - Software 3 08-04-2006 05:07 PM
Assistance with 'find -exec cp' shell script dick.swift Linux - Software 6 01-23-2006 10:00 AM
What Linux program is available to pull cd music off a cd and make it into a mp3? BajaNick Linux - Software 11 02-02-2005 10:09 PM


All times are GMT -5. The time now is 12:53 AM.

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 Google+: linuxquestions
Open Source Consulting | Domain Registration