Skip to content

Instantly share code, notes, and snippets.

@sangheonhan
Created January 11, 2019 07:09
Show Gist options
  • Select an option

  • Save sangheonhan/17799303ebbfbb5a88de864c8ca6e856 to your computer and use it in GitHub Desktop.

Select an option

Save sangheonhan/17799303ebbfbb5a88de864c8ca6e856 to your computer and use it in GitHub Desktop.
파일 이름을 이용해 id3v2 태그의 곡 이름, 앨범 이름, 아티스트을 갱신하는 스크립트
{
"name": "bookworm/fixid3",
"description": "Fix encoding of ID3 tags",
"require": {
"nette/finder": "2.2.*",
"nass600/get-id3": "dev-master"
},
"authors": [
{
"name": "bookworm",
"email": "[email protected]"
}
]
}
<?php
require 'vendor/autoload.php';
use Nette\Utils\Finder;
$id3 = new getID3();
if ($argc != 2 || !isset($argv[1]) || !is_dir($argv[1])) {
echo "Usage: $argv[0] <directory>\n\n";
exit(1);
}
foreach (Finder::findFiles('*.mp3')->from($argv[1]) as $file) {
$id3->analyze($file);
$pattern = '/^(\.\/)?
(?<artist>[^\/]+)\/
(?<album>[^\/]+)\/
(?<disc>\d+)?-?
(?<track_number>\d+)\s
(?<title>[^\/]+)\.mp3$
/x';
if (preg_match($pattern, $file, $m)) {
if (!isset($id3->info['tags']) || !isset($id3->info['tags']['id3v2'])) {
echo "WARNING:\tNo ID3 v2 tag. ({$file})\n";
$tags = array();
} else {
$tags = $id3->info['tags']['id3v2'];
}
$tagwriter = new getid3_writetags;
$tagwriter->filename = $file;
$tagwriter->tagformats = array('id3v2.4');
$tagwriter->overwrite_tags = true;
$tagwriter->tag_encoding = 'UTF-8';
$tagwriter->remove_other_tags = true;
$tags['artist'] = $tags['album'] = $tags['title'] = array();
$tags['artist'][] = $m['artist'];
$tags['album'][] = $m['album'];
$tags['title'][] = $m['title'];
if (!isset($m['track_number']) || empty($tags['track_number'])) {
$tags['track_number'] = array($m['track_number']);
} else {
}
$tagwriter->tag_data = $tags;
if ($tagwriter->WriteTags() == false) {
echo "ERROR:\t".implode("\n", $tagwriter->errors).".\n";
} else {
echo "OK:\t{$file}\n";
}
}
}
<?php
require 'vendor/autoload.php';
use Nette\Utils\Finder;
if ($argc != 2 || !isset($argv[1]) || !is_file($argv[1])) {
echo "Usage: $argv[0] <filename>\n\n";
exit(1);
}
$id3 = new getID3();
$id3->analyze($argv[1]);
var_dump($id3->info);
print_r($id3->info);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment