[ID3 Dev] Tag archive and a couple questions
Paul Taylor
paul_t100 at fastmail.fm
Fri Jan 5 03:41:57 PST 2007
For anyone familiar with Java Ive created such a program, its available
from the jaudiotagger cvs repository
(https://jaudiotagger.dev.java.net/), or the source code is below:
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
public class MergeID3AndMP3Files
{
public static void main(final String[] args)
{
MergeID3AndMP3Files test = new MergeID3AndMP3Files();
if(args.length==0)
{
System.err.println("usage MergeID3AndMP3Files FromDir ToDir
mp3File");
System.err.println(" You must enter the from
dir,outputdir and the mp3file to append");
System.exit(1);
}
else if(args.length!=3)
{
System.err.println("usage MergeID3AndMP3Files FromDir ToDir
mp3File");
System.err.println(" Only three parameters accepted");
System.exit(1);
}
File rootDir = new File(args[0]);
if(!rootDir.isDirectory())
{
System.err.println("usage MergeID3AndMP3Files FromDir ToDir
mp3File");
System.err.println(" Directory "+args[0] +" could not
be found");
System.exit(1);
}
File toDir = new File(args[1]);
if(!rootDir.isDirectory())
{
System.err.println("usage MergeID3AndMP3Files FromDir ToDir
mp3File");
System.err.println(" Directory "+args[1] +" could not
be found");
System.exit(1);
}
File mp3File = new File(args[2]);
if(!mp3File.isFile())
{
System.err.println("usage MergeID3AndMP3Files FromDir ToDir
mp3File");
System.err.println(" Mp3File "+args[2] +" could not be
found");
System.exit(1);
}
Date start = new Date();
System.out.println("Started to merge from:"+rootDir.getPath()+" at "
+ DateFormat.getTimeInstance().format(start));
test.scanSingleDir(rootDir,toDir,mp3File);
Date finish = new Date();
System.out.println("Finished to merge from:"+rootDir.getPath()
+ DateFormat.getTimeInstance().format(finish));
System.out.println("Attempted to merge:"+
MergeID3AndMP3Files.count);
System.out.println("Successful to
merge:"+(MergeID3AndMP3Files.count - MergeID3AndMP3Files.failed));
System.out.println("Failed to merge:"+
MergeID3AndMP3Files.failed);
}
private static int count =0;
private static int failed=0;
/**
* Recursive function to scan directory
*/
private void scanSingleDir(final File fromDir,final File toDir,final
File mp3File)
{
final File[] audioFiles = fromDir.listFiles(new
MergeID3AndMP3Files.MP3FileFilter());
if (audioFiles.length > 0)
{
for (int i = 0; i < audioFiles.length; i++)
{
MergeID3AndMP3Files.count++;
try
{
copyAudioToTmp(toDir,audioFiles[i],mp3File);
}
catch (Throwable t)
{
System.err.println("Unable to merge record:"+
MergeID3AndMP3Files.count +":"+mp3File.getPath());
MergeID3AndMP3Files.failed++;
t.printStackTrace();
}
}
}
final File[] audioFileDirs = fromDir.listFiles(new
MergeID3AndMP3Files.DirFilter());
if (audioFileDirs.length > 0)
{
for (int i = 0; i < audioFileDirs.length; i++)
{
scanSingleDir(audioFileDirs[i],new
File(toDir,audioFileDirs[i].getName()),mp3File);
}
}
}
final class MP3FileFilter
extends javax.swing.filechooser.FileFilter
implements java.io.FileFilter
{
/**
* allows Directories
*/
private final boolean allowDirectories;
/**
* Create a default MP3FileFilter. The allowDirectories field will
* default to false.
*/
public MP3FileFilter()
{
this(false);
}
/**
* Create an MP3FileFilter. If allowDirectories is true, then
this filter
* will accept directories as well as mp3 files. If it is false
then
* only mp3 files will be accepted.
*
* @param allowDirectories whether or not to accept directories
*/
private MP3FileFilter(final boolean allowDirectories)
{
this.allowDirectories = allowDirectories;
}
/**
* Determines whether or not the file is an mp3 file. If the
file is
* a directory, whether or not is accepted depends upon the
* allowDirectories flag passed to the constructor.
*
* @param file the file to test
* @return true if this file or directory should be accepted
*/
public final boolean accept(final File file)
{
return (
((file.getName()).toLowerCase().endsWith(".mp3"))
||
(file.isDirectory() && (this.allowDirectories == true))
);
}
/**
* Returns the Name of the Filter for use in the Chooser Dialog
*
* @return The Description of the Filter
*/
public final String getDescription()
{
return new String(".mp3 Files");
}
}
public final class DirFilter implements java.io.FileFilter
{
public DirFilter()
{
}
/**
* Determines whether or not the file is an mp3 file. If the
file is
* a directory, whether or not is accepted depends upon the
* allowDirectories flag passed to the constructor.
*
* @param file the file to test
* @return true if this file or directory should be accepted
*/
public final boolean accept(final File file)
{
return file.isDirectory();
}
}
public static File copyAudioToTmp(File toDir,File tagFile,File mp3File)
{
File outputFile = new File(toDir.getPath(), tagFile.getName());
boolean result = append(tagFile,mp3File,outputFile);
return outputFile;
}
private static boolean append(File fromFile1,File fromFile2, File
toFile)
{
try
{
FileInputStream in = new FileInputStream(fromFile1);
FileInputStream in2 = new FileInputStream(fromFile2);
toFile.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(toFile);
BufferedInputStream inBuffer = new BufferedInputStream (in);
BufferedInputStream inBuffer2 = new BufferedInputStream (in2);
BufferedOutputStream outBuffer = new BufferedOutputStream(out);
int theByte;
while ((theByte = inBuffer.read()) > -1)
{
outBuffer.write(theByte);
}
while ((theByte = inBuffer2.read()) > -1)
{
outBuffer.write(theByte);
}
outBuffer.close();
inBuffer.close();
inBuffer2.close();
out.close();
in.close();
in2.close();
// cleanupif files are not the same length
if ((fromFile1.length() + fromFile2.length())!= toFile.length())
{
toFile.delete();
return false;
}
return true;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
}
Paul Taylor wrote:
> Hi, this is great news, but I think it is worth pointing out these
> files only contain the tag they contain no audio so are not actually
> mp3s, to test against a library properly you need
> to append a simple mp3 to each of the files, this could be done after
> downloading the libray with a simple program.
>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: id3v2-unsubscribe at id3.org
> For additional commands, e-mail: id3v2-help at id3.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: id3v2-unsubscribe at id3.org
For additional commands, e-mail: id3v2-help at id3.org
More information about the ID3v2
mailing list