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

纵饮孤独 提交于 2019-12-21 20:35:14

问题


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.


回答1:


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




回答2:


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'];



回答3:


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 :)



来源:https://stackoverflow.com/questions/16746647/how-can-i-extract-the-album-art-from-an-mp3-id3-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!