Jump to content

MP3 ID3 Tags


acondiff

Recommended Posts

So I am trying to write a script where it browses a folder and gets all the mp3 id3 data for all the mp3's in the folder.

 

<?php

$dir = TEMPLATEPATH . "/flash/music/";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($fileread = readdir($dh)) !== false) {
		if($fileread == "." || $fileread == ".." || $fileread == "index.php" ) 
        	continue; 
        	
            $fullpath = $dir . $fileread;
            echo "filename: <b>$fullpath</b>";
            echo "<br>";

            $musicfile = $fullpath; 
            fopen($fullpath, 'r');
            fseek($musicfile, -128, SEEK_END);
            
            $tag = fread($musicfile, 3);
            
            if($tag == "TAG")
            {
            $data["song"] = trim(fread($musicfile, 30));
            $data["artist"] = trim(fread($musicfile, 30));
            $data["album"] = trim(fread($musicfile, 30));
            }
            else
                    die("MP3 file does not have any ID3 tag!");

            fclose($musicfile);

            while(list($key, $value) = each($data))
                {
                print("$key: $value<br>\r\n");
                }
        }
    }
}

?>

 

Its not working. I get this error:

 

filename: /[my_server_path]/public_html/new/wp-content/themes/default/flash/music/09 Low.mp3

Warning: fseek(): supplied argument is not a valid stream resource in /[my_server_path]/public_html/new/wp-content/themes/default/functions.php on line 1039

Warning: fread(): supplied argument is not a valid stream resource in /[my_server_path]/public_html/new/wp-content/themes/default/functions.php on line 1041
MP3 file does not have any ID3 tag!

Any help?

 

Thanks.

Link to comment
Share on other sites

 $musicfile = $fullpath; 
            fopen($fullpath, 'r');

 

$musicfile is set to a string and the resource from fopen isn't assigned to a variable

 

 $musicfile =  fopen($fullpath, 'r');

 

$musicfile is set to the file resource that can be used with fseek and fread

Link to comment
Share on other sites

Thank you so much. That got rid of one error. It is still saying that it doesn't have an ID3 tag when I know it does.

 

filename: /[my_server_path]/public_html/new/wp-content/themes/default/flash/music/09 Low.mp3

MP3 file does not have any ID3 tag!

 

I did try changing this

if($tag == "TAG")

 

to this

 

if($tag )

 

but that didn't work either. its not running through that if statement. somethings probably wrong with $tag.

 

I actually ran this very similar script on one file and it did work, I am just trying to apply it to a whole folder instead of just one file.

 

<?php
     $mp3 = "1.mp3"; //The mp3 file.

     $filesize = filesize($mp3);
     $file = fopen("1.mp3", "r");

     fseek($file, -128, SEEK_END); // It reads the 

     $tag = fread($file, 3);

     if($tag == "TAG")
     {
         $data["song"] = trim(fread($file, 30));
         $data["artist"] = trim(fread($file, 30));
         $data["album"] = trim(fread($file, 30));
         $data["year"] = trim(fread($file, 4));
         $data["comment"] = trim(fread($file, 30));
         $data["genre"] = trim(fread($file, 1));

     }
     else
         die("MP3 file does not have any ID3 tag!");

     fclose($file);

     while(list($key, $value) = each($data))
     {
         print("$key: $value<br>\r\n");
     }
?>

Link to comment
Share on other sites

UPDATE:

 

This code:

<?php

$dir = TEMPLATEPATH . "/flash/music/";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($fileread = readdir($dh)) !== false) {
		if($fileread == "." || $fileread == ".." || $fileread == "index.php" ) 
        	continue; 
        	
            $fullpath = $dir . $fileread;

            $musicfile =  fopen($fullpath, 'r');
            fseek($musicfile, -128, SEEK_END);
            
            
            $tag = fread($musicfile, 3);
            
            if($tag == "TAG")
            {
            	$data["song"] = trim(fread($musicfile, 30));
            	$data["artist"] = trim(fread($musicfile, 30));
            	$data["album"] = trim(fread($musicfile, 30));
            } 
            else
            {
                echo "MP3 file does not have any ID3 tag!";

            }

            fclose($musicfile);

            while(list($key, $value) = each($data))
            {
                print("$key: $value<br>\r\n");
            }
        }
    }
}

?>

 

Returns this:

MP3 file does not have any ID3 tag!

Warning: Variable passed to each() is not an array or object in /hermes/bosweb/web070/b703/ipw.condiffp/public_html/new/wp-content/themes/default/functions.php on line 1053

song: X&Y

artist: Coldplay

album: X&Y

 

So oddly enough, it is only retrieving the second songs id3 data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.