문제
나는 PHP를 사용하여 mp3의 앨범 아트를 설정하는 가장 좋은 방법을 찾고 있습니다.
제안?
해결책
앨범 아트는 ID3V2 사양으로 인해 "첨부 된 그림"으로 식별되는 데이터 프레임이며, getId3 ()는 순수한 PHP와 함께 ID3V2에서 가능한 모든 데이터 프레임을 작성하는 한 가지 방법 일뿐입니다.
이 출처를보십시오.http://getid3.sourceforge.net/source/write.id3v2.phps
소스 에서이 텍스트를 검색하십시오.
// 4.14 APIC Attached picture
앨범 아트를 작성하는 코드가 있습니다.
순수한 PHP만큼 느리지 않는 또 다른 방법은 PHP 스크립트에 의해 시작될 외부 응용 프로그램을 사용하는 것입니다. 높은 부하 하에서 작동하도록 설계된 경우 바이너리 컴파일 된 도구가 더 나은 솔루션이됩니다.
다른 팁
더 나은 (빠른) 방법은 외부 응용 프로그램과 php exec () 기능을 재미있게하는 것입니다. 추천합니다 IED3.
이것이 여전히 문제인지 확실하지 않지만 :
놀랍도록 완전한 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() 프로젝트.이미지를 처리할 수 있다고 약속할 수는 없지만 MP3용 ID3 태그를 작성할 수 있다고 주장하므로 이것이 최선의 선택이 될 것이라고 생각합니다.
앨범 아트 업데이트 코드를 공유하기보다는 getID3의 MP3 래퍼 클래스 전체를 게시하여 원하는대로 사용할 수 있습니다.
용법
$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