Domanda

Sto cercando il migliore o qualche modo per impostare il tipo di Album di mp3 utilizzando PHP.

Suggerimenti?

È stato utile?

Soluzione

album arte è un frame di dati identificato come “immagine singola” specificazione dovuta ID3v2, e getID3 () ora è solo un modo per scrivere tutte le possibili frame di dati in ID3v2 con puro PHP.

Guardate questa fonte: http://getid3.sourceforge.net/source/write.id3v2.phps

Cerca il testo nel codice:

// 4.14  APIC Attached picture

c'è un pezzo di codice responsabile per la scrittura degli album.

Un altro modo, che sembra non essere lento come puro PHP, è quello di utilizzare alcune applicazioni esterne, che sarà lanciato da script PHP. Se il servizio progettato per funzionare sotto un carico elevato, compilata strumento binario sarà una soluzione migliore.

Altri suggerimenti

Un modo migliore (più veloce) per fare questo sarebbe attraverso un'applicazione esterna e l'exec PHP () per divertirsi un comando. Suggerirei eyed3 .

Non è sicuro questo è ancora un problema, ma:

il getID3 incredibilmente completo () ( http://getid3.org ) progetto risolverà tutti i vostri problemi. Scopri questo post forum maggiori informazioni.

Installa getID3 utilizzando compositore composer require james-heinrich/getid3 Quindi utilizzare questo codice per aggiornare i tag 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));
}

È possibile esaminare il getID3 () del progetto . Non posso promettere che può gestire immagini, ma lo fa sostengono di essere in grado di scrivere i tag ID3 per i file MP3 e quindi penso che sarà la soluzione migliore.

Piuttosto che solo di condividere il codice per la copertina dell'album di aggiornamento, mi vado a postare la mia intera MP3 classe wrapper di getID3 qui in modo che si può utilizzare come vuoi

Utilizzo

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

Classe

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


}

Nota

Non c'è alcun codice di gestione degli errori di sicurezza.Attualmente, sto solo contando sulle eccezioni quando si tenta di eseguire qualsiasi operazione.Sentitevi liberi di modificare e utilizzare come forma.Richiede PHP GETID3

Ecco il codice base per aggiungere un'immagine e dati ID3 utilizzando getID3. (@Frostymarvelous wrapper include il codice equivalente, però penso che sia utile per mostrare le basi.)

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

Con questa funzione intrinseca di PHP,

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

Non credo che sia davvero possibile con PHP. Voglio dire, suppongo che tutto è possibile, ma non può essere una soluzione PHP nativa. Dal PHP Docs , penso che gli unici elementi che possono essere aggiornati sono:

  • Titolo
  • Artisti
  • Album
  • Anno
  • genere
  • Commento
  • traccia

man spiacenti. Forse Perl, Python, o Ruby potrebbe avere qualche soluzione.

Non sono sicuro se si ha familiarità con il Perl (io personalmente non mi piace, ma, è bravo a cose come questa ...). Ecco uno script che sembra essere in grado di tirare in e modificare le copertine degli album in formato MP3: http: //www.plunder.com/-download-66279.htm

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top