Just copy your project folder into your music-directory and execute the following:
Once:
composer install
Everytime you want to rename mp3s:
php rename.php
{ | |
"name": "mya/rename", | |
"require": { | |
"james-heinrich/getid3": "^1.9" | |
}, | |
"authors": [ | |
{ | |
"name": "Mansour Yaacoubi", | |
"email": "[email protected]" | |
} | |
] | |
} |
<?php | |
require 'vendor/autoload.php'; | |
$base = array_key_exists(1, $argv) ? $argv[1] : './'; | |
$files = array_diff(scandir($base), [ '.', '..', basename(__DIR__) ]); | |
$encoding = 'UTF-8'; | |
// Initialize getID3 engine | |
$getID3 = new getID3; | |
$getID3->setOption(compact('encoding')); | |
foreach( $files as $file ) { | |
if( is_dir($base.$file) || !str_ends_with(strtolower($file), '.mp3') ) continue; | |
[ $artist, $title ] = explode('-', getNormalFileName($file)); | |
$artist = getArtistArray($artist); | |
$title = getTitleArray($title); | |
$newFileName = implode(' ft ', $artist) . ' - ' . implode(', ', $title) . '.mp3'; | |
renameTags($base.$file, $title, $artist); // Rename Tags | |
rename($base.$file, $base.$newFileName); // Rename Filename | |
} | |
function str_ends_with($haystack, $needle) | |
{ | |
return strrpos($haystack, $needle) + strlen($needle) === | |
strlen($haystack); | |
} | |
function getNormalFileName($file) | |
{ | |
$file = strtolower($file); | |
$file = str_replace([ '_', '.mp3' ], ' ', $file); | |
$file = preg_replace("/\([^)]+\)/", '', $file); | |
return $file; | |
} | |
function getArtistArray($artist) | |
{ | |
$artist = str_replace( [ ' ft. ', ' ft ', ' feat. ', ' feat ', '&', ' et ', ' and ' ], ', ', $artist ); | |
$artist = explode( ',', $artist ); | |
$artist = array_map('trim', $artist); | |
$artist = array_map('ucwords', $artist); | |
$artist = preg_replace('!\s+!', ' ', $artist); | |
return $artist; | |
} | |
function getTitleArray($title) | |
{ | |
$title = explode( ',', $title ); | |
$title = array_map('trim', $title); | |
$title = array_map('ucwords', $title); | |
$title = preg_replace('!\s+!', ' ', $title); | |
return $title; | |
} | |
function renameTags($file, $title, $artist) | |
{ | |
global $encoding; | |
if( !$title ) $title = null; | |
elseif( !is_array($title) ) $title = [ $title ]; | |
if( !$artist ) $artist = null; | |
elseif( !is_array($artist) ) $artist = [ $artist ]; | |
$tag_data = [ | |
'comment' => ['Tagged by Yaacoubi.com'], | |
'popularimeter' => ['email' => '[email protected]', 'rating' => 128, 'data' => 0], | |
'unique_file_identifier' => ['ownerid'=>'[email protected]', 'data' => md5(time()) ], | |
]; | |
$tagwriter = new getid3_writetags; | |
$tagwriter->filename = $file; | |
$tagwriter->tagformats = [ 'id3v2.3' ]; | |
$tagwriter->overwrite_tags = true; | |
$tagwriter->tag_encoding = $encoding; | |
$tagwriter->remove_other_tags = true; | |
if($title) $tag_data['title'] = $title; | |
if($artist) $tag_data['artist'] = $artist; | |
$tagwriter->tag_data = $tag_data; | |
// write tags | |
if ( $tagwriter->WriteTags() ) { | |
$ttl = implode(', ', $title); | |
$art = implode(', ', $artist); | |
echo 'Successfully wrote tags ' . "($ttl by $art)" .PHP_EOL; | |
if (!empty($tagwriter->warnings)) { | |
echo 'There were some warnings:'.PHP_EOL.implode(PHP_EOL.PHP_EOL, $tagwriter->warnings); | |
} | |
} else { | |
echo 'Failed to write tags!'.PHP_EOL.implode(PHP_EOL.PHP_EOL, $tagwriter->errors); | |
} | |
} | |
?> |
Of course there is a lot of room for improvement.