| 1 | |
package org.jaudiotagger.test; |
| 2 | |
|
| 3 | |
import java.io.*; |
| 4 | |
import java.text.DateFormat; |
| 5 | |
import java.util.Date; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | 0 | public class MergeID3AndMP3Files |
| 13 | |
{ |
| 14 | |
|
| 15 | |
public static void main(final String[] args) |
| 16 | |
{ |
| 17 | 0 | MergeID3AndMP3Files test = new MergeID3AndMP3Files(); |
| 18 | |
|
| 19 | 0 | if (args.length == 0) |
| 20 | |
{ |
| 21 | 0 | System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File"); |
| 22 | 0 | System.err.println(" You must enter the from dir,outputdir and the mp3file to append"); |
| 23 | 0 | System.exit(1); |
| 24 | |
} |
| 25 | 0 | else if (args.length != 3) |
| 26 | |
{ |
| 27 | 0 | System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File"); |
| 28 | 0 | System.err.println(" Only three parameters accepted"); |
| 29 | 0 | System.exit(1); |
| 30 | |
} |
| 31 | 0 | File rootDir = new File(args[0]); |
| 32 | 0 | if (!rootDir.isDirectory()) |
| 33 | |
{ |
| 34 | 0 | System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File"); |
| 35 | 0 | System.err.println(" Directory " + args[0] + " could not be found"); |
| 36 | 0 | System.exit(1); |
| 37 | |
} |
| 38 | |
|
| 39 | |
|
| 40 | 0 | File toDir = new File(args[1]); |
| 41 | 0 | if (!rootDir.isDirectory()) |
| 42 | |
{ |
| 43 | 0 | System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File"); |
| 44 | 0 | System.err.println(" Directory " + args[1] + " could not be found"); |
| 45 | 0 | System.exit(1); |
| 46 | |
} |
| 47 | |
|
| 48 | 0 | File mp3File = new File(args[2]); |
| 49 | 0 | if (!mp3File.isFile()) |
| 50 | |
{ |
| 51 | 0 | System.err.println("usage MergeID3AndMP3Files FromDir ToDir mp3File"); |
| 52 | 0 | System.err.println(" Mp3File " + args[2] + " could not be found"); |
| 53 | 0 | System.exit(1); |
| 54 | |
} |
| 55 | |
|
| 56 | 0 | Date start = new Date(); |
| 57 | 0 | System.out.println("Started to merge from:" + rootDir.getPath() + " at " + DateFormat.getTimeInstance().format(start)); |
| 58 | 0 | test.scanSingleDir(rootDir, toDir, mp3File); |
| 59 | 0 | Date finish = new Date(); |
| 60 | 0 | System.out.println("Finished to merge from:" + rootDir.getPath() + DateFormat.getTimeInstance().format(finish)); |
| 61 | 0 | System.out.println("Attempted to merge:" + MergeID3AndMP3Files.count); |
| 62 | 0 | System.out.println("Successful to merge:" + (MergeID3AndMP3Files.count - MergeID3AndMP3Files.failed)); |
| 63 | 0 | System.out.println("Failed to merge:" + MergeID3AndMP3Files.failed); |
| 64 | |
|
| 65 | 0 | } |
| 66 | |
|
| 67 | |
|
| 68 | 0 | private static int count = 0; |
| 69 | 0 | private static int failed = 0; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private void scanSingleDir(final File fromDir, final File toDir, final File mp3File) |
| 78 | |
{ |
| 79 | |
|
| 80 | 0 | final File[] audioFiles = fromDir.listFiles(new MergeID3AndMP3Files.MP3FileFilter()); |
| 81 | 0 | if (audioFiles.length > 0) |
| 82 | |
{ |
| 83 | 0 | for (File audioFile : audioFiles) |
| 84 | |
{ |
| 85 | 0 | MergeID3AndMP3Files.count++; |
| 86 | |
|
| 87 | |
try |
| 88 | |
{ |
| 89 | 0 | copyAudioToTmp(toDir, audioFile, mp3File); |
| 90 | |
} |
| 91 | 0 | catch (Throwable t) |
| 92 | |
{ |
| 93 | 0 | System.err.println("Unable to merge record:" + MergeID3AndMP3Files.count + ":" + mp3File.getPath()); |
| 94 | 0 | MergeID3AndMP3Files.failed++; |
| 95 | 0 | t.printStackTrace(); |
| 96 | 0 | } |
| 97 | |
} |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | final File[] audioFileDirs = fromDir.listFiles(new MergeID3AndMP3Files.DirFilter()); |
| 101 | 0 | if (audioFileDirs.length > 0) |
| 102 | |
{ |
| 103 | 0 | for (File audioFileDir : audioFileDirs) |
| 104 | |
{ |
| 105 | 0 | scanSingleDir(audioFileDir, new File(toDir, audioFileDir.getName()), mp3File); |
| 106 | |
} |
| 107 | |
} |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
final class MP3FileFilter extends javax.swing.filechooser.FileFilter implements java.io.FileFilter |
| 111 | |
{ |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
private final boolean allowDirectories; |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
public MP3FileFilter() |
| 123 | |
{ |
| 124 | 0 | this(false); |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
private MP3FileFilter(final boolean allowDirectories) |
| 135 | 0 | { |
| 136 | 0 | this.allowDirectories = allowDirectories; |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public final boolean accept(final File file) |
| 148 | |
{ |
| 149 | 0 | return (((file.getName()).toLowerCase().endsWith(".mp3")) || (file.isDirectory() && (this.allowDirectories))); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public final String getDescription() |
| 158 | |
{ |
| 159 | 0 | return ".mp3 Files"; |
| 160 | |
} |
| 161 | |
} |
| 162 | |
|
| 163 | 0 | public final class DirFilter implements java.io.FileFilter |
| 164 | |
{ |
| 165 | |
public DirFilter() |
| 166 | 0 | { |
| 167 | |
|
| 168 | 0 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
public final boolean accept(final File file) |
| 180 | |
{ |
| 181 | 0 | return file.isDirectory(); |
| 182 | |
} |
| 183 | |
|
| 184 | |
public static final String IDENT = "$Id: MergeID3AndMP3Files.java 836 2009-11-12 15:44:07Z paultaylor $"; |
| 185 | |
} |
| 186 | |
|
| 187 | |
public static File copyAudioToTmp(File toDir, File tagFile, File mp3File) |
| 188 | |
{ |
| 189 | 0 | File outputFile = new File(toDir.getPath(), tagFile.getName()); |
| 190 | 0 | boolean result = append(tagFile, mp3File, outputFile); |
| 191 | 0 | return outputFile; |
| 192 | |
} |
| 193 | |
|
| 194 | |
private static boolean append(File fromFile1, File fromFile2, File toFile) |
| 195 | |
{ |
| 196 | |
try |
| 197 | |
{ |
| 198 | 0 | FileInputStream in = new FileInputStream(fromFile1); |
| 199 | 0 | FileInputStream in2 = new FileInputStream(fromFile2); |
| 200 | |
|
| 201 | 0 | toFile.getParentFile().mkdirs(); |
| 202 | 0 | FileOutputStream out = new FileOutputStream(toFile); |
| 203 | 0 | BufferedInputStream inBuffer = new BufferedInputStream(in); |
| 204 | 0 | BufferedInputStream inBuffer2 = new BufferedInputStream(in2); |
| 205 | 0 | BufferedOutputStream outBuffer = new BufferedOutputStream(out); |
| 206 | |
|
| 207 | |
int theByte; |
| 208 | |
|
| 209 | 0 | while ((theByte = inBuffer.read()) > -1) |
| 210 | |
{ |
| 211 | 0 | outBuffer.write(theByte); |
| 212 | |
} |
| 213 | |
|
| 214 | 0 | while ((theByte = inBuffer2.read()) > -1) |
| 215 | |
{ |
| 216 | 0 | outBuffer.write(theByte); |
| 217 | |
} |
| 218 | |
|
| 219 | 0 | outBuffer.close(); |
| 220 | 0 | inBuffer.close(); |
| 221 | 0 | inBuffer2.close(); |
| 222 | 0 | out.close(); |
| 223 | 0 | in.close(); |
| 224 | 0 | in2.close(); |
| 225 | |
|
| 226 | |
|
| 227 | 0 | if ((fromFile1.length() + fromFile2.length()) != toFile.length()) |
| 228 | |
{ |
| 229 | 0 | toFile.delete(); |
| 230 | |
|
| 231 | 0 | return false; |
| 232 | |
} |
| 233 | |
|
| 234 | 0 | return true; |
| 235 | |
} |
| 236 | 0 | catch (IOException e) |
| 237 | |
{ |
| 238 | 0 | e.printStackTrace(); |
| 239 | 0 | return false; |
| 240 | |
} |
| 241 | |
} |
| 242 | |
} |