LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 07-29-2005, 01:04 AM   #1
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Rep: Reputation: 30
perl script


Hi ,
I have a *very* newbie question :-)
Amarok has a problem displaying utf-8 id3v2 tags properly and I found the solution on the amarok web site. However, they give a perl script saying that it solves the problem but I never used perl script before and I have no idea how to do it and what I am supposed to do with this line of code:
Code:
#!/usr/bin/perl
die "File $ARGV[0] doesn't exist" unless -f $ARGV[0];
use MP3::Mplib;
my $mp3 = MP3::Mplib->new($ARGV[0]);
my $v2tag = $mp3->get_v2tag;
print "Error writing tags of $ARGV[0]\n" unless $mp3->set_v2tag($v2tag,&UTF8);
Anyone can help? :-)
Thanks in advance,
A.

PS: the ref can be found there:
http://amarok.kde.org/wiki/index.php...in_my_files.21
 
Old 07-29-2005, 02:54 AM   #2
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
Hi, you can put this code into a file like mp3-utf8.pl, chmod it to have the x bit (like 'chmod +x mp3-utf8.pl') and execute it with the mp3 file as an argument, like './mp3-utf8.pl mysong.mp3'.
Alternative to chmod, you could run the script with 'perl mp3-utf8.pl' with which you tell 'perl' to go and run the script mp3-utf8.pl

A little explanation for each line of code for your information:

#!/usr/bin/perl ### The 'shabang' line, telling the kernel to use perl as interpreter for this file.
die "File $ARGV[0] doesn't exist" unless -f $ARGV[0]; ### exit if the file passed as an argument isn't a file (like non-existent or a directory, for instance).
use MP3::Mplib; ### 'use' tells perl to load a module, called MP3::MPlib
my $mp3 = MP3::Mplib->new($ARGV[0]); ### instantiate an object with the loaded module. it is 'loading' the mp3 file passed as an argument. the object is called '$mp3'
my $v2tag = $mp3->get_v2tag; ### with this line, the tag is retrieved from the mp3 file into the variable '$v2tag'.
print "Error writing tags of $ARGV[0]\n" unless $mp3->set_v2tag($v2tag,&UTF8); ### says: tell there is a problem unless you are successful in writing the previously read tag into the file, using UTF8 encoding.

I hope this answers your question... or I may have interpreted your question completely wrong... in that case, sorry for the redundant response..
If you want something else answered, please respond, I might be able to help you out. . . ?
 
Old 07-29-2005, 09:02 AM   #3
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Original Poster
Rep: Reputation: 30
Thank you very much for your answer, I was not expecting that much :-)
I still have a last prob with that: is there a way I automate this process? I mean it's a script supposed to help amarok to display correctly id3-v2 tags correctly using utf8. I am guessing that this script should be run by amarok in background all the time ( correct me if I'm wrong) and I was wondering how to tell amarok to do so...
thanks for your help,
A.
 
Old 07-29-2005, 09:50 AM   #4
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
This script seems to be designed to alter mp3 files.. once run on an mp3 file, the file is prepared to be read correctly by amarok. Try it out on one mp3 file that amarok has trouble with. copy it to a new filename, run this script on it, and open the 'altered' file with amarok.. determine if it worked fine.

This script is not meant to run 'while amarok runs'...
 
Old 07-29-2005, 10:36 AM   #5
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Original Poster
Rep: Reputation: 30
Hi,
Thank you very much. Actually it works great and now it looks like I am going to be able to "repair" all these tags. However, I still have a little issue: I have to do that on all the songs using unicode charaters and I have * alot* of them, is it possible to have a command that apply these script to *all* the mp3 file within a given directory (the unicode one are in a separated directory), because doing everything by hand... would be *very* long... there are around a thousand songs to alter...
Thanks again for your help!
A.
 
Old 07-29-2005, 10:44 AM   #6
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
If you have the script in your home directory, you can do something like

Code:
cd mp3directory                                    # Go to the directory with the mp3(s)
ls *mp3 | xargs ~/the-script                  # Apply the script to each of them
 
Old 07-29-2005, 10:49 AM   #7
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Original Poster
Rep: Reputation: 30
Thanks for your answer but this gives me an error:
doing it within the mp3 directory I get:
$ ls *.mp3 | xargs /home/arnaud/mp3-utf8.pl
ls: *mp3: No such file or directory
File doesn't exist at /home/arnaud/mp3-utf8.pl line 2.
Any ideas?
A.
 
Old 07-29-2005, 10:55 AM   #8
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Where is your script? If it is not your home directory, you have to replace ~/ with the correct path.

Let's say that you have your mp3 files in /home/arnaud/mp3files and that your script is named mp3-utf8.pl and it is located in /home/arnaud/myscript/

Then you have to "go to the mp3's folder" with "cd /home/arnaud/mp3files" and then run "ls *mp3 | xargs /home/arnaud/myscript/mp3-utf8.pl".

If the mp3 and the script are in the same directory, you only have to put "ls *mp3 | xargs mp3-utf8.pl".

HTH
 
Old 07-29-2005, 11:04 AM   #9
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Original Poster
Rep: Reputation: 30
Thanks but my script is in the right place (/home/arnaud/mp3-utf8.pl). However the problem is that the mp3 files are all in different folders. I was looking for a way to affect all the mp3 files within my mp3 directory in spite of the fact that in this directory mp3 files are organized in their own different folders. Well if it's too much of a pain I'll just removed those folders and put all the mp3 files in only one directory.
Thanks,
A.
 
Old 07-30-2005, 03:24 AM   #10
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
Go to the 'main folder' in which all the mp3 files are (or in subdirectories of that folder (ie, the root folder of arr mp3 files)).

for i in `find . -name *mp3`; do echo "Processing $i"; <directory-to-script>/mp3-utf8.pl $i;done

That would do it from the shell prompt...
Also, the script could be redesigned to handle directories instead of single files... but that would be a redesign, this is quicker.
 
Old 07-30-2005, 04:42 PM   #11
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Original Poster
Rep: Reputation: 30
Hi thanks for your answer!
Actually it gives me a lot of error saying that a file doesn't exist at /home/arnaud/mp3/mp3-utf8.pl line 2.
I tried previously with for f in `find /mp3 -name \*.mp3`; do ./mp3-utf8.pl "$f"; done
but same result...
Any ideas?
Thanks a lot!
A.

Last edited by Arnaud_B; 07-30-2005 at 06:27 PM.
 
Old 07-30-2005, 06:19 PM   #12
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by Arnaud_B
Hi thanks for your answer! I actually did it with $perl mp3-utf8.pl `find /mp3 -name \*.mp3` and it seems to work like that... could you explain me the difference between this and the command you suggested me?
Thanks a lot!
A.
Code:
find /mp3 -name "*.mp3"  -print | xargs mp3-utf8.pl
would have been my preferred way to do it.

Last edited by eddiebaby1023; 07-30-2005 at 06:20 PM.
 
Old 07-31-2005, 04:31 AM   #13
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 372

Rep: Reputation: 42
For what Eddybaby1023 writes to work, the script would have to be redesigned, as it would receive lots of parameters, while it can only take one. Here's a redesign to process a lot of subderictories in one run:

Just give it a directory to process recursively.

Code:
#!/usr/bin/perl

use strict;
use MP3::Mplib;

my $directory=shift or die "Give a directory to process recursively\n"; #Argument directory is read into variable $directory

if(! -d $directory)
{
  die "Directory $directory doesn't exist\n";
}

# the directory exists, now we are reading its contents and do our job.

#### Functions ####

###############################################################################
#
# processDirectory
#
# To process a directory recursively. It calls itself with new arguments
# (directories) when it finds one.
#
# input parameters: 1: a directory to process.
# output: none
#
###############################################################################
sub processDirectory
{
  my $dirToProcess=shift;
  opendir DH,"$dirToProcess";
  my @files=readdir DH; # Read directory contents into array
  closedir DH;
  foreach my $item (@files) # Loop through array with files
  {
    next if $item =~ /^\.\.?$/; # Skip directories '.' ond '..' for obvious reasons
    if(-d "$dirToProcess/$item") # Is this item a directory?
    {
      processDirectory("$dirToProcess/$item");
      next;
    }
    if(-f "$dirToProcess/$item" && $item =~ /\.(mp3|MP3)$/) # Is this item a File AND does it end on mp3 or MP3 ?
    {
      rewriteFile("$dirToProcess/$item");
      next;
    }
    # This is obviously no mp3 file or directory
    warn "Skipped:   $dirToProcess/$item: no mp3 file\n";
  }
}

###############################################################################
#
# rewriteFile
#
# To do the actual rewriting (this was the original script in fact.
# I do not have these libraries, so I couldn't test the error handling
#
# input parameters: 1: a file (mp3) to rewrite the tags of
# output: none
#
###############################################################################
sub rewriteFile
{
  my $fileToRewrite=shift;
  my $mp3 = MP3::Mplib->new($fileToRewrite);
  my $v2tag = $mp3->get_v2tag;
  $mp3->set_v2tag($v2tag,&UTF8) or warn("Error writing to file $fileToRewrite: $!\n");
  print "Processed: $fileToRewrite\n";
}

#### Main program (very small :) ) ####

processDirectory($directory);
Hope this helps ??

Last edited by rhoekstra; 07-31-2005 at 04:39 AM.
 
Old 07-31-2005, 03:27 PM   #14
Arnaud_B
Member
 
Registered: Jun 2004
Location: New York
Distribution: Debian
Posts: 219

Original Poster
Rep: Reputation: 30
wow... thanks a lot rhoekstra. From what I understand
the script will be applied to all files and files within subfolders right?
ok I am going to try now and let you know...
Thanks a lot for that much help!!
A.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 07:25 PM
Perl Script Gerardoj Programming 3 10-10-2003 12:18 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 06:03 AM
perl script... killjoy Programming 0 03-29-2001 04:42 PM


All times are GMT -5. The time now is 08:36 PM.

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