| 
<?php
/*
 * An advanced example that uses getID3() library to rename a set of mp3 files according
 * to their id3 metadata.
 *
 * @package        BatchFileRename
 * @author        Mohamed ELkholy <[email protected]>
 * @copyright    Copyright (c) 2012 Mohamed Elkholy
 * @version        SVN: $Id: advanced.getID3.php 5 2012-04-17 18:53:36Z admin $
 * @license        LGPL <http://www.gnu.org/licenses/lgpl.txt>
 * @link        http://www.phpclasses.org/package/7472
 */
 
 // Include BatchFileRename class file
 require ("../src/BatchFileRename.php");
 
 /*
 * Include getID3 library
 *
 * Note: getID3 library is not included in the downloaded package so you can download it
 * from http://getid3.sourceforge.net/
 */
 require ("getid3/getid3.php");
 
 // Create a new class instance and enable simulation mode
 $batchFileRename = new BatchFileRename(array(
 "simulationMode" => true,
 ));
 
 /**
 * Utility function that gets the id3 metadata of mp3 files.
 *
 * @param   string  $filepath    The  mp3 file path
 * @param   string  $tag        The  id3 tag
 * @return  string    The value of the id3 metadata tag
 */
 function get_id3_metadata($filepath, $tag) {
 
 static $getID3, $files;
 
 if (!isset($getID3)) {
 $getID3 = new getID3();
 }
 
 if (!isset($files)) {
 $files = array();
 }
 
 if (!isset($files[$filepath])) {
 $id3info = $getID3->analyze($filepath);
 getid3_lib::CopyTagsToComments($id3info);
 $files[$filepath] = $id3info;
 }
 
 if (!isset($files[$filepath]['comments'][$tag][0])) {
 return NULL;
 }
 
 return $files[$filepath]['comments'][$tag][0];
 }
 
 /**
 * A custom callback for excluding mp3 files that does not have id3 metadata.
 *
 * The $fileinfo paramater contains file informations like:
 *  $fileinfo->path         The file path
 *  $fileinfo->name         The file name
 *  $fileinfo->basename     The file name without the extension
 *  $fileinfo->extension    The file extension
 *  $fileinfo->size         The file size
 *
 * @param   object  $fileinfo    Object containing file information
 * @return  bool    If FALSE the file will be excluded, TRUE the file will be included
 */
 function filterNoID3Tags($fileinfo) {
 
 $id3_artist = get_id3_metadata($fileinfo->path, 'artist');
 $id3_title = get_id3_metadata($fileinfo->path, 'title');
 
 if (empty($id3_artist) || empty($id3_title)) {
 return false;
 }
 
 return true;
 }
 
 // Define the fileSet filters
 $batchFileRename->setFileSetOptions(array(
 "directoryPath" => "/path/to/directory/",
 "includeExtensions" => array("mp3"),
 "callback" => "filterNoID3Tags",
 ));
 
 /**
 * The callback for the filename variable "artist".
 *
 * The $fileinfo paramater contains file informations:
 *  $fileinfo->path         The file path
 *  $fileinfo->name         The file name
 *  $fileinfo->basename     The file name without the extension
 *  $fileinfo->extension    The file extension
 *  $fileinfo->size         The file size
 *  $fileinfo->count        The file index number within the fileSet
 *
 * The $renameinfo paramater contains renamed file informations:
 *  $renameinfo->name       The new file name
 *  $renameinfo->basename   The new file name without the extension
 *  $renameinfo->extension  The new file extension
 *
 * @param   object  $fileinfo   Object containing file information
 * @param   object  $renameinfo Object containing renamed file information
 * @return  string  The value of the variable in the filename
 */
 function artistFileNameVariable($fileinfo, $renameinfo) {
 
 return get_id3_metadata($fileinfo->path, 'artist');
 }
 
 /**
 * The callback for the filename variable "title".
 *
 * @param   object  $fileinfo   Object containing file information
 * @param   object  $renameinfo Object containing renamed file information
 * @return  string  The value of the variable in the filename
 */
 function titleFileNameVariable($fileinfo, $renameinfo) {
 
 return get_id3_metadata($fileinfo->path, 'title');
 }
 
 // Define the Filename Variables "title", "artist" and their callbacks.
 $batchFileRename->setFileNameVariable("title", "titleFileNameVariable");
 $batchFileRename->setFileNameVariable("artist", "artistFileNameVariable");
 
 // Define the renaming rules
 $batchFileRename->setRenameRules(array(
 "fixedFileName" => "<##> - <artist> - <title>",
 ));
 
 // Rename the files
 $batchFileRename->rename();
 
 echo "<pre>";
 
 echo "Successfully renamed files:";
 print_r($batchFileRename->getResultArray());
 
 // Check if an error occurs while renaming the files
 if ($batchFileRename->isError()) {
 echo "Some files were not renamed!";
 print_r($batchFileRename->getErrorsArray());
 }
 
 echo "</pre>";
 |