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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-28-2008, 04:47 PM
|
#1
|
Member
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 73
Rep:
|
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
|
|
|
06-28-2008, 05:42 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
06-28-2008, 05:56 PM
|
#3
|
Member
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 73
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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?
|
|
|
06-28-2008, 06:31 PM
|
#4
|
Member
Registered: Jun 2008
Posts: 101
Rep:
|
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.
|
|
|
06-28-2008, 07:29 PM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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)
|
|
|
06-28-2008, 08:53 PM
|
#6
|
Member
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 73
Original Poster
Rep:
|
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!
|
|
|
All times are GMT -5. The time now is 06:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|