• PHP > gestion des tags ID des fichiers sons (mp3, flac, etc.)

      https://www.google.fr/search?q=php+id3+tag+picture&oq=php+id3+tag+picture

       

      Extract cover from mp3 file

       

      I made an uploader for mp3 files.
      I simply want to know how to extract from the id3 tag (with PHP of course)
      - the cover
      - the comments

      I use the function id3_get_tag in PHP but it doesn’t extract the cover nor the comments.

      Thanks for your help.

       

      Post by James Heinrich » Thu Mar 02, 2006 1:02 am

      Code: Select all

      $getID3 = new getID3;
      $fileinfo = $getID3->analyze($filename);
      $picture = @$fileinfo['id3v2']['APIC'][0]['data']; // binary image data
      $comments = @$fileinfo['id3v2']['comments']; // multi-dimensional array
      

      Post by mazout » Thu Mar 02, 2006 3:56 pm

      Thanx I know extracts comments and covers (adding some code of course). 

      Great ! Thank you.

      --

      Post by jarc » Sun Jan 21, 2007 4:26 pm

      James Heinrich wrote: 

      Code: Select all

      
      $picture = @$fileinfo['id3v2']['APIC'][0]['data']; // binary image data
      
      

      Hi
      With this we can extract the data, but how can we show the data like a jpg image?

      Thanks

      Post by ameshkin69 » Thu Feb 01, 2007 6:48 am

      This is simply not working, iu have no idea what I’m doing wrong but I cannot seem to get the data for the picture which is attatched to an mp3 file! 

      Does anyone have code that works?

      Post by TEMAndroid » Sun Oct 28, 2007 7:27 pm

      Anybody can help? I have same problem :(((

      Post by TEMAndroid » Mon Oct 29, 2007 7:59 pm

      I solved the problem, may be not correctly but it works! :)
      in file module.tag.id3v2.php add string (1261): 

      Code: Select all

      $parsedFrame['data']             = substr($parsedFrame['data'], 2);

      Please check this bug correctly :)

      Sorry for my english

      Post by saulo » Tue Mar 11, 2008 8:21 pm

      hi, i’ve been traying show a cover from any mp3 and i can’t get the solution. 

      please somebody help me, my code is:

      $getID3 = new getID3;

      $ThisFileInfo = $getID3->analyze("musica/01-maroon_5-if_i_never_see_your_face_again.mp3″);

      getid3_lib::CopyTagsToComments($ThisFileInfo);

      $picture = @$ThisFileInfo['id3v2']['APIC'][0]['data']; // binary image data

      echo $picture;

      echo @$ThisFileInfo['comments_html']['artist'][0]."<br>"; // artist from any/all available tag formats
      echo @$ThisFileInfo['tags']['id3v2']['title'][0]."<br>"; // title from ID3v2
      echo @$ThisFileInfo['audio']['bitrate']."<br>"; // audio bitrate
      echo @$ThisFileInfo['playtime_string']."<br>"; // playtime in minutes:seconds, formatted string

      thanks for the help.

      Post by saulo » Tue Mar 11, 2008 8:47 pm

      hi guys, i have the answer, if you need, there it is: 

      test1.php (here show the info of mp3)

      <?php
      require_once(‘getid3/getid3/getid3.php’);
      //$filename="musica/yoursong.mp3″;

      // include getID3() library (can be in a different directory if full path is specified)

      // Initialize getID3 engine
      $getID3 = new getID3;
      // Analyze file and store returned data in $ThisFileInfo
      $ThisFileInfo = $getID3->analyze("musica/01-maroon_5-if_i_never_see_your_face_again.mp3″);

      // Optional: copies data from all subarrays of [tags] into [comments] so
      // metadata is all available in one location for all tag formats
      // metainformation is always available under [tags] even if this is not called
      getid3_lib::CopyTagsToComments($ThisFileInfo);

      $picture = @$ThisFileInfo['id3v2']['APIC'][0]['data']; // binary image data

      echo $picture;

      // Output desired information in whatever format you want
      // Note: all entries in [comments] or [tags] are arrays of strings
      // See structure.txt for information on what information is available where
      // or check out the output of /demos/demo.browse.php for a particular file
      // to see the full detail of what information is returned where in the array
      echo @$ThisFileInfo['comments_html']['artist'][0]."<br>"; // artist from any/all available tag formats
      echo @$ThisFileInfo['tags']['id3v2']['title'][0]."<br>"; // title from ID3v2
      echo @$ThisFileInfo['audio']['bitrate']."<br>"; // audio bitrate
      echo @$ThisFileInfo['playtime_string']."<br>"; // playtime in minutes:seconds, formatted string

      echo "<img src=’cover.php’ width=’200′>";

      ?>

      and cover.php (here show the binary data)

      <?
      require_once(‘getid3/getid3/getid3.php’);

      $getID3 = new getID3;
      #$getID3->option_tag_id3v2 = true; # Don’t know what this does yet
      $getID3->analyze("musica/01-maroon_5-if_i_never_see_your_face_again.mp3″);
      if (isset($getID3->info['id3v2']['APIC'][0]['data'])) {
      $cover = $getID3->info['id3v2']['APIC'][0]['data'];
      } elseif (isset($getID3->info['id3v2']['PIC'][0]['data'])) {
      $cover = $getID3->info['id3v2']['PIC'][0]['data'];
      } else {
      $cover = null;
      }
      if (isset($getID3->info['id3v2']['APIC'][0]['image_mime'])) {
      $mimetype = $getID3->info['id3v2']['APIC'][0]['image_mime'];
      } else {
      $mimetype = ‘image/jpeg’; // or null; depends on your needs
      }

      if (!is_null($cover)) {
      // Send file
      header("Content-Type: " . $mimetype);

      if (isset($getID3->info['id3v2']['APIC'][0]['image_bytes'])) {
      header("Content-Length: " . $getID3->info['id3v2']['APIC'][0]['image_bytes']);
      }

      echo($cover);
      }
      ?>

      nice day!

      Post by saulo » Tue Mar 11, 2008 8:53 pm

      hi guys, i have the answer, if you need, there it is: 

      test1.php (here show the info of mp3)

      <?php
      require_once(‘getid3/getid3/getid3.php’);
      //$filename="musica/yoursong.mp3″;

      // include getID3() library (can be in a different directory if full path is specified)

      // Initialize getID3 engine
      $getID3 = new getID3;
      // Analyze file and store returned data in $ThisFileInfo
      $ThisFileInfo = $getID3->analyze("musica/01-maroon_5-if_i_never_see_your_face_again.mp3″);

      // Optional: copies data from all subarrays of [tags] into [comments] so
      // metadata is all available in one location for all tag formats
      // metainformation is always available under [tags] even if this is not called
      getid3_lib::CopyTagsToComments($ThisFileInfo);

      $picture = @$ThisFileInfo['id3v2']['APIC'][0]['data']; // binary image data

      echo $picture;

      // Output desired information in whatever format you want
      // Note: all entries in [comments] or [tags] are arrays of strings
      // See structure.txt for information on what information is available where
      // or check out the output of /demos/demo.browse.php for a particular file
      // to see the full detail of what information is returned where in the array
      echo @$ThisFileInfo['comments_html']['artist'][0]."<br>"; // artist from any/all available tag formats
      echo @$ThisFileInfo['tags']['id3v2']['title'][0]."<br>"; // title from ID3v2
      echo @$ThisFileInfo['audio']['bitrate']."<br>"; // audio bitrate
      echo @$ThisFileInfo['playtime_string']."<br>"; // playtime in minutes:seconds, formatted string

      echo "<img src=’cover.php’ width=’200′>";

      ?>

      and cover.php (here show the binary data)

      <?
      require_once(‘getid3/getid3/getid3.php’);

      $getID3 = new getID3;
      #$getID3->option_tag_id3v2 = true; # Don’t know what this does yet
      $getID3->analyze("musica/01-maroon_5-if_i_never_see_your_face_again.mp3″);
      if (isset($getID3->info['id3v2']['APIC'][0]['data'])) {
      $cover = $getID3->info['id3v2']['APIC'][0]['data'];
      } elseif (isset($getID3->info['id3v2']['PIC'][0]['data'])) {
      $cover = $getID3->info['id3v2']['PIC'][0]['data'];
      } else {
      $cover = null;
      }
      if (isset($getID3->info['id3v2']['APIC'][0]['image_mime'])) {
      $mimetype = $getID3->info['id3v2']['APIC'][0]['image_mime'];
      } else {
      $mimetype = ‘image/jpeg’; // or null; depends on your needs
      }

      if (!is_null($cover)) {
      // Send file
      header("Content-Type: " . $mimetype);

      if (isset($getID3->info['id3v2']['APIC'][0]['image_bytes'])) {
      header("Content-Length: " . $getID3->info['id3v2']['APIC'][0]['image_bytes']);
      }

      echo($cover);
      }
      ?>

      nice day!

      Post by saulo » Tue Mar 11, 2008 8:54 pm

      hi guys, i have the answer, if you need, there it is: 

      test1.php (here show the info of mp3)

      <?php
      require_once(‘getid3/getid3/getid3.php’);
      //$filename="musica/yoursong.mp3″;

      // include getID3() library (can be in a different directory if full path is specified)

      // Initialize getID3 engine
      $getID3 = new getID3;
      // Analyze file and store returned data in $ThisFileInfo
      $ThisFileInfo = $getID3->analyze("musica/01-maroon_5-if_i_never_see_your_face_again.mp3″);

      // Optional: copies data from all subarrays of [tags] into [comments] so
      // metadata is all available in one location for all tag formats
      // metainformation is always available under [tags] even if this is not called
      getid3_lib::CopyTagsToComments($ThisFileInfo);

      $picture = @$ThisFileInfo['id3v2']['APIC'][0]['data']; // binary image data

      echo $picture;

      // Output desired information in whatever format you want
      // Note: all entries in [comments] or [tags] are arrays of strings
      // See structure.txt for information on what information is available where
      // or check out the output of /demos/demo.browse.php for a particular file
      // to see the full detail of what information is returned where in the array
      echo @$ThisFileInfo['comments_html']['artist'][0]."<br>"; // artist from any/all available tag formats
      echo @$ThisFileInfo['tags']['id3v2']['title'][0]."<br>"; // title from ID3v2
      echo @$ThisFileInfo['audio']['bitrate']."<br>"; // audio bitrate
      echo @$ThisFileInfo['playtime_string']."<br>"; // playtime in minutes:seconds, formatted string

      echo "<img src=’cover.php’ width=’200′>";

      ?>

      and cover.php (here show the binary data)

      <?
      require_once(‘getid3/getid3/getid3.php’);

      $getID3 = new getID3;
      #$getID3->option_tag_id3v2 = true; # Don’t know what this does yet
      $getID3->analyze("musica/yoursong.mp3″);
      if (isset($getID3->info['id3v2']['APIC'][0]['data'])) {
      $cover = $getID3->info['id3v2']['APIC'][0]['data'];
      } elseif (isset($getID3->info['id3v2']['PIC'][0]['data'])) {
      $cover = $getID3->info['id3v2']['PIC'][0]['data'];
      } else {
      $cover = null;
      }
      if (isset($getID3->info['id3v2']['APIC'][0]['image_mime'])) {
      $mimetype = $getID3->info['id3v2']['APIC'][0]['image_mime'];
      } else {
      $mimetype = ‘image/jpeg’; // or null; depends on your needs
      }

      if (!is_null($cover)) {
      // Send file
      header("Content-Type: " . $mimetype);

      if (isset($getID3->info['id3v2']['APIC'][0]['image_bytes'])) {
      header("Content-Length: " . $getID3->info['id3v2']['APIC'][0]['image_bytes']);
      }

      echo($cover);
      }
      ?>

      nice day!

      Post by sala » Tue Mar 31, 2009 11:39 am

      Thanks! saulo ….. 

      That one i want………….

      Post by ssubham » Sun Jan 23, 2011 4:04 pm

      Hi,
      Is there any way to get the spectrum(amplitude) from the mp3 files? 

      Thank you for the cooperation in advance.

      Regards,
      subham

      Post by James Heinrich » Sun Jan 23, 2011 4:41 pm

      ssubham wrote:Is there any way to get the spectrum(amplitude) from the mp3 files?

      Sure, just decode the MPEG audio, run it through a Fourier transform and there you go. That is far far far outside the scope of getID3, however.

      Post by ssubham » Mon Jan 24, 2011 12:04 pm

      James Heinrich wrote: 

      ssubham wrote:Is there any way to get the spectrum(amplitude) from the mp3 files?

      Sure, just decode the MPEG audio, run it through a Fourier transform and there you go. That is far far far outside the scope of getID3, however.

      thank you for the reply. Is there anyother way to get it?

      Post by James Heinrich » Mon Jan 24, 2011 12:22 pm

      No. Anything that gives you a spectral display of the audio will be doing exactly that.

      How can I extract the album art from an MP3 ID3 tag?

      I can successfully get the mp3 ID3 tags out of my mp3 app, using php. I am left with a huge BLOB. I’m sure this is pretty straightforward, but I just need a little help in the right direction. What I’d like to end up with is (BLOB DATA CORRECTED):

      $media_year = implode($ThisFileInfo['comments_html']['year']);
      $media_genre = implode($ThisFileInfo['comments_html']['genre']);
      $media_jpg = [BLOBDATA];

      (I know that makes no programming sense, but you get the idea. The first two lines work fine for me, but I don’t know how to get the BLOBDATA from the ID3 tags and convert it to a JPG that I can display.)

      In another tutorial I see this, but it doesn’t connect the dots for me:

      [comments] => Array
          (
              [picture] => Array
                  (
                      [0] => Array
                          (
                              [data] => <binary data>
                              [image_mime] => image/jpeg
                          )
      
                  )
      
          )

      I loathe asking readers to "do it for me" so, please forgive me. But I’ve tried every resource I can find. Any help is supremely appreciated.

      3 Answers

      i use getid3 library for this work fine like

        <?php
        $Path="mp3 file path";
        $getID3 = new getID3;
        $OldThisFileInfo = $getID3->analyze($Path);
        if(isset($OldThisFileInfo['comments']['picture'][0])){
           $Image='data:'.$OldThisFileInfo['comments']['picture'][0]['image_mime'].';charset=utf-8;base64,'.base64_encode($OldThisFileInfo['comments']['picture'][0]['data']);
        }
        ?>
        <img id="FileImage" width="150" src="<?php echo @$Image;?>" height="150">

      in this code i embedded image in to HTML with base64_encode that you can see

       

      mohammad mohsenipur

      you can use file_put_contents() to save the id3 image data to a file.

       

       $path = "/Applications/MAMP/htdocs/site/example.mp3"; //example. local file path to mp3
        $getID3 = new getID3;
        $tags = $getID3->analyze($Path);
      
      if (isset($tags['comments']['picture']['0']['data'])) {
          $image = $tags['comments']['picture']['0']['data'];
          if(file_put_contents(FCPATH . "imageTest.jpg", $image)) {
              echo 'Image Added';
          } else {
              echo 'Image Not Added';
          }
      }

      or you can display the image in place using

      header('Content-Type: image/jpeg');
      echo $tags['comments']['picture']['0']['data'];

      I found an Article which explains the Structure of ID3 Tags. http://www.stagetwo.eu/article/19-MP3-ID3-Tags-Wie-kann-man-die-teile-lesen This should make you able to understand and to parse them. It’s in German but maybe the article about the ID3 Tags Structure is also readable with google translator. I like the description of the important bytes and for example the explanation about the SyncSafe integer.

      To your Problem it describes how the APIC (Image) Frame is structured. When you parsed these Structure and you got the bytestream just use imagecreatefromstring($byteStream) Now you have your image resource out of the MP3 :)

       

 

Aucun commentaire

 

Laissez un commentaire