setting album art of a mp3 with php

前端 未结 9 1803
花落未央
花落未央 2021-01-04 16:04

I am looking for the best or any way to set the Album Art of mp3s using PHP.

Suggestions?

相关标签:
9条回答
  • 2021-01-04 16:46

    Here is the basic code for adding an image and ID3 data using getID3. (@frostymarvelous' wrapper includes equivalent code, however I think that it is helpful to show the basics.)

    <?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'
                )
            )
        );
    ?>
    
    0 讨论(0)
  • 2021-01-04 16:50

    Install getId3 using composer composer require james-heinrich/getid3 Then Use this code to update your id3 tags

    // 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));
    }
    
    0 讨论(0)
  • 2021-01-04 16:52

    A better (faster) way to do this would be through an external application and the PHP exec() function to fun a command. I would recommend eyeD3.

    0 讨论(0)
提交回复
热议问题