2011-01-30 55 views
2

我正在嘗試將APIC圖片寫入帶有getid3的mp3文件。這裏是代碼;將APIC寫入帶有getid3的mp3文件(id3v2)

$cover = "/home/user/public_html/artwork/cover.jpg"; 
$TagData['attached_picture'][]=array(
'picturetypeid'=>2, // Cover. More: module.tag.id3v2.php -> function APICPictureTypeLookup 
'description'=>'cover', // text field 
'mime'=>'image/jpeg', // Mime type image 
'data'=>$cover // Image data 
); 

但它不工作。圖像大小約爲1.5 MB。我應該調整它還是......?

我在哪裏錯了?

感謝

+0

你在使用什麼庫,以及如何將數據保存到MP3文件? – 2011-01-30 17:26:04

+0

我使用getid3庫http://www.getid3.org/ – 2011-01-30 17:30:02

回答

1

看着他們在其網站上的演示:http://www.getid3.org/source/demo.write.phps

的代碼片段:

$fd = fopen($_FILES['userfile']['tmp_name'], 'rb') 
$APICdata = fread($fd, filesize($_FILES['userfile']['tmp_name'])); 
fclose ($fd); 

$imagetypes = array(1=>'gif', 2=>'jpeg', 3=>'png'); 
if (isset($imagetypes[$APIC_imageTypeID])) { 
    $TagData['attached_picture'][0]['data']   = $APICdata; 
    $TagData['attached_picture'][0]['picturetypeid'] = $_POST['APICpictureType']; 
    $TagData['attached_picture'][0]['description'] = $_FILES['userfile']['name']; 
    $TagData['attached_picture'][0]['mime']   = 'image/'.$imagetypes[$APIC_imageTypeID]; 
} 

好像數據的關鍵必須是圖像內容,而不僅僅是圖像文件的路徑。 所以你的情況,應該是這樣的:

$cover = "/home/user/public_html/artwork/cover.jpg"; 
$fd = fopen($cover, 'rb') 
$APICdata = fread($fd, filesize($coverFile)); 
fclose ($fd); 

$TagData['attached_picture'][]=array(
'picturetypeid'=>2, // Cover. More: module.tag.id3v2.php -> function APICPictureTypeLookup 
'description'=>'cover', // text field 
'mime'=>'image/jpeg', // Mime type image 
'data'=>$APICdata // Image data 
); 

注意:這僅僅是在之後的演示代碼看一眼,我沒有用過這個庫或測試此代碼。

0

GetID3需要爲數據發送文件的內容,而不是文件路徑。那麼只有它可以將它們嵌入到文件中。嘗試

$cover = "/home/user/public_html/artwork/cover.jpg"; 
$TagData['attached_picture'][]=array(
    'picturetypeid'=>2, // Cover. More: module.tag.id3v2.php -> function APICPictureTypeLookup 
    'description'=>'cover', // text field 
    'mime'=>'image/jpeg', // Mime type image 
    'data'=> file_get_contents($cover) // Image data; not the file name 
); 

測試工作:)

0

我發現這個源代碼:

case 'APIC': 
    // 4.14 APIC Attached picture 
    // Text encoding  $xx 
    // MIME type   <text string> $00 
    // Picture type  $xx 
    // Description  <text string according to encoding> $00 (00) 
    // Picture data  <binary data> 

所以圖像數據必須是二進制。

的解決方案是在這裏:getid3 demo

0

這一個是爲我工作了很長一段時間:

$TagData = array(); //your other tags 
    $fd = fopen($albumPath, 'rb'); 
    $APICdata = fread($fd, filesize($albumPath)); 
    fclose($fd); 

    if($APICdata) { 
     $TagData += array(
      'attached_picture' => array(0 => array(
       'data'   => $APICdata, 
       'picturetypeid' => '0x03', 
       'description' => 'cover', 
       'mime'   => image_type_to_mime_type($albumExifType) 
      )) 
     ); 
    } 
    //and write the tags to file 
0
<?php 
///////////////////////////////////////////////////////////////// 
/// getID3() by James Heinrich <[email protected]>    // 
// available at http://getid3.sourceforge.net     // 
//   or http://www.getid3.org       // 
//   also https://github.com/JamesHeinrich/getID3  // 
///////////////////////////////////////////////////////////////// 
//                // 
// /demo/demo.simple.write.php - part of getID3()    // 
// Sample script showing basic syntax for writing tags   // 
// See readme.txt for more details        // 
//               /// 
///////////////////////////////////////////////////////////////// 

//die('Due to a security issue, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in '.$_SERVER['PHP_SELF']); 

$TextEncoding = 'UTF-8'; 
$albumPath = "img/img.jpg"; // path to your image 


require_once('../getid3/getid3.php'); 
// Initialize getID3 engine 
$getID3 = new getID3; 
$getID3->setOption(array('encoding'=>$TextEncoding)); 

require_once('../getid3/write.php'); 
// Initialize getID3 tag-writing module 
$tagwriter = new getid3_writetags; 
//$tagwriter->filename = '/path/to/file.mp3'; 
$tagwriter->filename = 'uploads/problem.mp3'; 

//$tagwriter->tagformats = array('id3v1', 'id3v2.3'); 
$tagwriter->tagformats = array('id3v2.3'); 

// set various options (optional) 
$tagwriter->overwrite_tags = true; // if true will erase existing tag data and write only passed data; if false will merge passed data with existing tag data (experimental) 
$tagwriter->remove_other_tags = false; // if true removes other tag formats (e.g. ID3v1, ID3v2, APE, Lyrics3, etc) that may be present in the file and only write the specified tag format(s). If false leaves any unspecified tag formats as-is. 
$tagwriter->tag_encoding  = $TextEncoding; 
$tagwriter->remove_other_tags = true; 

// populate data array 
$TagData = array(
    'title'   => array('My Song'), 
    'artist'  => array('My Song'), 
    'album'   => array('My Song'), 
    'year'   => array('20015'), 
    'genre'   => array('My Song'), 
    'comment'  => array('My Song'), 
    'track'   => array('01'), 
); 

$fd = fopen($albumPath, 'rb'); 
$APICdata = fread($fd, filesize($albumPath)); 
fclose($fd); 

if($APICdata) { 
    $TagData += array(
     'attached_picture' => array(0 => array(
      'data'   => $APICdata, 
      'picturetypeid' => '0x03', 
      'description' => 'cover', 
      'mime'   => image_type_to_mime_type(@$albumExifType) 
     )) 
    ); 
} 



$tagwriter->tag_data = $TagData; 

// write tags 
if ($tagwriter->WriteTags()) { 
    echo 'Successfully wrote tags<br>'; 
    if (!empty($tagwriter->warnings)) { 
     echo 'There were some warnings:<br>'.implode('<br><br>', $tagwriter->warnings); 
    } 
} else { 
    echo 'Failed to write tags!<br>'.implode('<br><br>', $tagwriter->errors); 
} 

對於任何人誰只是needd更新他們的ID3標籤,包括專輯封面,上面代碼工作得很好你需要讓get3庫去工作。 這個答案是基於JacopKane的答案,所以他的功勞