January 26, 2014 | In: How-to vychytávky
extracting mp3 id3 tags with php
extract mp3 info with php and rename file on linux box
how to get all mp3 info from a file with php and rename based on this tags
download complete file (rename to .php after): mp3-getinfo-tags-rename-php.txt
<?php clearstatcache(); /* %f Filename without the path [string] %F Filename with the path [string] %k File size in KB [integer] %a Artist [string] %c Comment [string] %g Musical genre [string] %G Musical genre number [integer] %l Album name [string] %n Track [integer] %t Track Title [string] %y Year [string] %C Copyright flag [string] %e Emphasis [string] %E CRC Error protection [string] %L MPEG Layer [string] %O Original material flag [string] %o Stereo/mono mode [string] %p Padding [string] %v MPEG Version [float] %u Number of good audio frames [integer] %b Number of corrupt audio frames [integer] %Q Sampling frequency in Hz [integer] %q Sampling frequency in kHz [integer] %r Bit Rate in kbps (type and meaning affected by -r option) %m Playing time: minutes only [integer] %s Playing time: seconds only [integer] (usually used in conjunction with %m) %S Total playing time in seconds [integer] %% A single percent sign */ // my mp3 directory to recusively iterate thrue $directory = '/mnt/temp/teachings'; // what info i want from my mp3 file - tags $info = [ 'artist' => '%a', 'year' => '%y', 'comment' => '%c', 'album_name' => '%l', 'track' => '%n', 'track_title' => '%t', 'minutes' => '%m', 'seconds' => '%s', ]; // what my filename will looks like $fileFormat = '%artist-%year-%comment-%album_name-%track-M%minutesS%seconds.mp3'; // build format usable for shell and use newline as delimiter $infoParam = implode('\n', $info); // what this can be... print ''; // scan folders $rdit = new RecursiveDirectoryIterator($directory); foreach (new RecursiveIteratorIterator($rdit) as $file) { // do the job only for files & mp3 ext if($file->isFile() && strtolower($file->getExtension()) == 'mp3') { // get me the id3 tags (mp3info v1 on my debian) $mp3Info = shell_exec('mp3info -p "' . $infoParam . '" ' . $file); // is the info what i expect? $mp3InfoArray = explode("\n", $mp3Info); if(sizeof($mp3InfoArray) != sizeof($info)) { print "check:$file
"; continue; } // make me array with combined $info keys and mp3 info values $mp3InfoArray = array_combine(array_flip($info), $mp3InfoArray); // build new filename $newFile = sprintfa($fileFormat, $mp3InfoArray); // clean a bit $newFile = str_replace([',', '\\', ' ', '/', '%'], '_', $newFile); $newFile = str_replace(['&'], ['and'], $newFile); $newFile = iconv('UTF-8', 'ASCII//TRANSLIT', utf8_encode($newFile));; $newFile = $file->getPath() . '/' . $newFile; // is the new file equal to the old filename? if(strcmp($file, $newFile) != 0) { // does the newfile already exists? if(file_exists($newFile)) { $newFile = substr_replace($newFile, uniqid('-') . '.mp3',-4, 4); } rename($file, $newFile); // what we did with old to new file print "$file:$newFile
"; flush(); } } } function sprintfa ($format, array $values) { foreach ((array)$values as $k => $v) { $format = str_replace("%$k", $v, $format); } return $format; }
Comments are closed.