使用 getid3 (id3v2) 将 APIC 写入 mp3 文件

发布于 2024-10-15 03:52:22 字数 450 浏览 6 评论 0原文

我正在尝试使用 getid3 将 APIC 图片写入 mp3 文件。这是代码;

$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。我应该调整它的大小还是什么?

我哪里错了?

谢谢

I am trying to write APIC picture to mp3 file with getid3. here is the code;

$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
);

but it doesnt work. image size is around 1.5 MB. should i resize it or sth ?

where am i wrong ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰ゝ天使的微笑 2024-10-22 03:52:22

查看他们网站上的演示: 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
);

注意:这只是在快速浏览了演示代码之后,我还没有使用过这个库或测试过这个代码。

Looking at the demo they have on their website: http://www.getid3.org/source/demo.write.phps

snippet of code:

$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];
}

Seems like the data key needs to be the image content, not just the path to the image file.
So in your case, it should be something like:

$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
);

Note: This is just after a quick glance at the demo code, I have not used this library or tested this code.

遇见了你 2024-10-22 03:52:22

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

测试并工作:)

GetID3 needs the content of the file to be send for the data, not the file path. Then only it will be able to embed them into the file. Try

$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
);

tested and working :)

我要还你自由 2024-10-22 03:52:22

我在源代码中发现了这一点:

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

I found this in the source code:

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>

So picture data must be binary.

The solution is here: getid3 demo

完美的未来在梦里 2024-10-22 03:52:22

这个为我工作了很长时间:

    $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 

This one is working for me for long time:

    $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 
伴梦长久 2024-10-22 03:52:22
    <?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);
}

对于任何只需要更新 ID3 标签(包括专辑封面)的人来说,上面的代码都可以正常工作。您需要有 getID3 库才能工作。
这个答案是基于 JacopKane 的答案,所以功劳归于他

    <?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);
}

For anyone who just needd to update their ID3 tags including the Album Art , above code works just fine You need to have getID3 library to work .
This Answer is based on JacopKane's Answer so credit goes to him

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文