我正在寻找使用 PHP 设置 mp3 专辑艺术的最佳或任何方法。

建议?

有帮助吗?

解决方案

专辑封面是标识为“附图片”所致的ID3v2规范的数据帧,并 getID3()现在仅写在ID3v2的所有可能的数据帧用纯PHP的一种方式。

看这个源: http://getid3.sourceforge.net/source/write.id3v2.phps

搜索此文在源:

// 4.14  APIC Attached picture

有负责编写专辑封面的一段代码。

另一种方式,这似乎是没有纯PHP一样慢,是使用一些外部应用程序,将由PHP脚本启动。如果你的服务而设计的高负荷下工作,二进制编译工具将是一个更好的解决方案。

其他提示

一个更好(更快)的方式来做到这将是通过外部应用程序和PHP exec()函数以有趣的命令。我建议 eyeD3

不知道这仍然是一个问题,但:

令人惊讶的完整getid3()( http://getid3.org )项目将解决所有的问题。请查看论坛职位的详细信息。

安装使用getId3作曲家composer require james-heinrich/getid3 然后使用此代码更新您的ID3标签

// Initialize getID3 engine
$getID3 = new getID3;

// Initialize getID3 tag-writing module
$tagwriter = new getid3_writetags;
$tagwriter->filename = 'path/to/file.mp3';
$tagwriter->tagformats = array('id3v2.4');
$tagwriter->overwrite_tags    = true;
$tagwriter->remove_other_tags = true;
$tagwriter->tag_encoding      = 'UTF-8';

$pictureFile = file_get_contents("path/to/image.jpg");

$TagData = array(
    'title' => array('My Title'),
    'artist' => array('My Artist'),
    'album' => array('This Album'),
    'comment' => array('My comment'),
    'year' => array(2018),
    'attached_picture' => array(
        array (
            'data'=> $pictureFile,
            'picturetypeid'=> 3,
            'mime'=> 'image/jpeg',
            'description' => 'My Picture'
        )
    )
);

$tagwriter->tag_data = $TagData;

// write tags
if ($tagwriter->WriteTags()){
    return true;
}else{
    throw new \Exception(implode(' : ', $tagwriter->errors));
}

您可以查看到 getID3()项目。我不能保证它能够处理图像,但它声称能够写ID3标签的MP3,所以我认为这将是你最好的选择。

而不是仅仅共享代码专辑封面更新,我一会在这里发布我的整个MP3的包装类getID3的,所以你可以根据需要使用

用法

$mp3 = new Whisppa\Music\MP3($mp3_filepath);

//Get data
$mp3->title
$mp3->artist
$mp3->album
$mp3->genre

//set properties
$mp3->year = '2014';

//change album art
$mp3->set_art(file_get_contents($pathtoimage), 'image/jpeg', 'New Caption');//sets front album art

//save new details
$mp3->save();

<?php

namespace Whisppa\Music;

class MP3
{
    protected static $_id3;

    protected $file;
    protected $id3;
    protected $data     = null;


    protected $info =  ['duration'];
    protected $tags =  ['title', 'artist', 'album', 'year', 'genre', 'comment', 'track', 'attached_picture', 'image'];
    protected $readonly_tags =  ['attached_picture', 'comment', 'image'];
                                //'popularimeter' => ['email'=> 'music@whisppa.com', 'rating'=> 1, 'data'=> 0],//rating: 5 = 255, 4 = 196, 3 = 128, 2 = 64,1 = 1 | data: counter


    public function __construct($file)
    {
        $this->file = $file;
        $this->id3  = self::id3();
    }

    public function update_filepath($file)
    {
        $this->file = $file;
    }

    public function save()
    {
        $tagwriter = new \GetId3\Write\Tags;
        $tagwriter->filename = $this->file;
        $tagwriter->tag_encoding = 'UTF-8';
        $tagwriter->tagformats = ['id3v2.3', 'id3v1'];
        $tagwriter->overwrite_tags = true;
        $tagwriter->remove_other_tags = true;

        $tagwriter->tag_data = $this->data;

        // write tags
        if ($tagwriter->WriteTags())
            return true;
        else
            throw new \Exception(implode(' : ', $tagwriter->errors));
    }


    public static function id3()
    {
        if(!self::$_id3)
            self::$_id3 = new \GetId3\GetId3Core;

        return self::$_id3;
    }

    public function set_art($data, $mime = 'image/jpeg', $caption = 'Whisppa Music')
    {
        $this->data['attached_picture'] = [];

        $this->data['attached_picture'][0]['data']            = $data;
        $this->data['attached_picture'][0]['picturetypeid']   = 0x03;    // 'Cover (front)'    
        $this->data['attached_picture'][0]['description']     = $caption;
        $this->data['attached_picture'][0]['mime']            = $mime;

        return $this;
    }

    public function __get($key)
    {
        if(!in_array($key, $this->tags) && !in_array($key, $this->info) && !isset($this->info[$key]))
            throw new \Exception("Unknown property '$key' for class '" . __class__ . "'");

        if($this->data === null)
            $this->analyze();

        if($key == 'image')
            return isset($this->data['attached_picture']) ? ['data' => $this->data['attached_picture'][0]['data'], 'mime' => $this->data['attached_picture'][0]['mime']] : null;
        else if(isset($this->info[$key]))
            return $this->info[$key];
        else
            return isset($this->data[$key]) ? $this->data[$key][0] : null;
    }

    public function __set($key, $value)
    {
        if(!in_array($key, $this->tags))
            throw new \Exception("Unknown property '$key' for class '" . __class__ . "'");
        if(in_array($key, $this->readonly_tags))
            throw new \Exception("Tying to set readonly property '$key' for class '" . __class__ . "'");

        if($this->data === null)
            $this->analyze();

        $this->data[$key] = [$value];
    }

    protected function analyze()
    {
        $data = $this->id3->analyze($this->file);

        $this->info =  [
                'duration' => isset($data['playtime_seconds']) ? ceil($data['playtime_seconds']) : 0,
            ];

        $this->data = isset($data['tags']) ? array_intersect_key($data['tags']['id3v2'], array_flip($this->tags)) : [];
        $this->data['comment'] = ['http://whisppa.com'];

        if(isset($data['id3v2']['APIC']))
            $this->data['attached_picture'] = [$data['id3v2']['APIC'][0]];
    }


}

注意

这里没有任何错误处理代码尚未。目前,我只是凭着例外,当我尝试运行任何操作。 随意修改和配合使用。需要PHP GETID3

下面是用于添加图像并使用getID3 ID3数据的基本代码。 (@frostymarvelous'包装包括等同的代码,但是我认为是有帮助的显示的基础。)

<?php
    // Initialize getID3 engine
    $getID3 = new getID3;

    // Initialize getID3 tag-writing module
    $tagwriter = new getid3_writetags;
    $tagwriter->filename = 'audiofile.mp3';
    $tagwriter->tagformats = array('id3v2.3');
    $tagwriter->overwrite_tags    = true;
    $tagwriter->remove_other_tags = true;
    $tagwriter->tag_encoding      = $TextEncoding;

    $pictureFile=file_get_contents("image.jpg");

    $TagData = array(
        'title' => 'My Title',
        'artist' => 'My Artist',        
        'attached_picture' => array(   
            array (
                'data'=> $pictureFile,
                'picturetypeid'=> 3,
                'mime'=> 'image/jpeg',
                'description' => 'My Picture'
            )
        )
    );
?>

使用PHP的这个内置功能,

<?php
    $tag = id3_get_tag( "path/to/example.mp3" );
    print_r($tag);
?>

我认为 PHP 不可能做到这一点。我的意思是,我认为一切皆有可能,但它可能不是原生 PHP 解决方案。来自 PHP 文档, ,我认为唯一可以更新的项目是:

  • 标题
  • 艺术家
  • 专辑
  • 类型
  • 评论
  • 追踪

对不起。也许 Perl、Python 或 Ruby 可能有一些解决方案。

我不确定你是否熟悉 Perl(我个人不喜欢它,但是,它擅长做这样的事情......)。下面的脚本似乎能够提取并编辑 MP3 中的专辑封面: http://www.plunder.com/-download-66279.htm

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top