MergeID3AndMP3Filespublic class MergeID3AndMP3Files extends Object Simple class that will attempt to recusively read all files within a directory ending in .mp3 (but
only actually expected to contain id3 tags), merge them with a given mp3 and write them to the provided
output folder (which must already exist) |
Fields Summary |
---|
private static int | count | private static int | failed |
Methods Summary |
---|
private static boolean | append(java.io.File fromFile1, java.io.File fromFile2, java.io.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;
}
| public static java.io.File | copyAudioToTmp(java.io.File toDir, java.io.File tagFile, java.io.File mp3File)
File outputFile = new File(toDir.getPath(), tagFile.getName());
boolean result = append(tagFile, mp3File, outputFile);
return outputFile;
| public static void | main(java.lang.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 void | scanSingleDir(java.io.File fromDir, java.io.File toDir, java.io.File mp3File)Recursive function to scan directory
final File[] audioFiles = fromDir.listFiles(new MergeID3AndMP3Files.MP3FileFilter());
if (audioFiles.length > 0)
{
for (File audioFile : audioFiles)
{
MergeID3AndMP3Files.count++;
try
{
copyAudioToTmp(toDir, audioFile, 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 (File audioFileDir : audioFileDirs)
{
scanSingleDir(audioFileDir, new File(toDir, audioFileDir.getName()), mp3File);
}
}
|
|