-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMp3Converter.java
More file actions
131 lines (82 loc) · 3.64 KB
/
Mp3Converter.java
File metadata and controls
131 lines (82 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mp3metagen;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cmc.music.common.ID3ReadException;
import org.cmc.music.metadata.IMusicMetadata;
import org.cmc.music.clean.*;
import org.cmc.music.myid3.*;
import org.cmc.music.metadata.MusicMetadata;
import org.cmc.music.metadata.MusicMetadataSet;
/**
*
* @author stephenmalloy
*/
public class Mp3Converter extends Thread {
private Mp3ConvertGUI mcg;
private ArrayList listOfFiles;
private FileManager fm;
private int successCount, failureCount;
public Mp3Converter(Mp3ConvertGUI mcg, ArrayList listOfFiles) {
this.mcg = mcg;
this.listOfFiles = listOfFiles;
this.fm = new FileManager();
}
@Override
public void run() {
successCount = 0;
failureCount = 0;
if (listOfFiles.size() > 0) {
mcg.clearOutput();
mcg.setProgressBarMaxInterval(listOfFiles.size());
mcg.updateOutput("Detected " + listOfFiles.size() + " MP3 files to convert.\r\n");
for (int i = 0; i < listOfFiles.size(); i++) {
// TODO - We need to account for Windows here. The file path format should change.
// System.getProperty("os.name")
String fileName = mcg.mp3Path + "/" + (String) listOfFiles.get(i);
mcg.updateOutput("Attempting to convert " + fileName + "\r\n");
File src = new File(fileName);
try {
MusicMetadataSet src_set = new MyID3().read(src); // read metadata
if (src_set == null) {
mcg.updateOutput("No metadata detected for " + fileName + ". Aborting.\r\n");
failureCount++;
} else {
//System.out.println("Meta data is: " + src_set);
IMusicMetadata metadata = src_set.getSimplified();
String newName = metadata.getSongTitle() + "_" + metadata.getArtist() + "_" + metadata.getAlbum();
newName = newName.replaceAll(" ", "_");
newName = newName.replaceAll("null_", "").trim() + ".mp3";
if (mcg.doOverwrite) {
fm.renameFile(fileName, mcg.mp3Path + "/" + newName);
} else if (mcg.doWriteToPath) {
fm.copyFile(fileName, mcg.outputPath + "/" + newName);
}
mcg.updateOutput("Rename complete for " + newName + "\r\n");
successCount++;
}
} catch (IOException ex) {
Logger.getLogger(Mp3Converter.class.getName()).log(Level.SEVERE, null, ex);
} catch (ID3ReadException ex) {
Logger.getLogger(Mp3Converter.class.getName()).log(Level.SEVERE, null, ex);
}
mcg.updateProgress(i, listOfFiles.size());
} // end list iterator
mcg.clearProgress();
mcg.updateOutput("\r\nConversion complete. " + successCount + " successful - " + failureCount + " failed");
Toolkit.getDefaultToolkit().beep();
} else {
// update UI
mcg.clearOutput();
mcg.updateOutput("No MP3 files detected.");
} // end if files to convert
} // end run method
}