配合DiskGenius恢复的无需MP3文件的重复MP3音乐筛选JAVA-SWT
package guidancer.cem;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Text;
import com.mpatric.mp3agic.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class CheckSWT {
protected Shell shlmp;
private Text Or;
private Text Neo;
private String orString;
private String neoString;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
CheckSWT window = new CheckSWT();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlmp.open();
shlmp.layout();
while (!shlmp.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlmp = new Shell();
shlmp.setSize(308, 184);
shlmp.setText("筛选重复MP3");
Button SetOr = new Button(shlmp, SWT.NONE);
SetOr.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog directoryDialog=new DirectoryDialog(shlmp,SWT.OPEN);
directoryDialog.setText("选择源目录");
directoryDialog.setMessage("选择正确的目录");
orString=directoryDialog.open();
if(!Objects.equals(orString, null)) {
Or.setText(orString);
return;
}else {
MessageBox messageBox=new MessageBox(shlmp);
messageBox.setText("警告");
messageBox.setMessage("选择目录有误");
messageBox.open();
}
}
});
SetOr.setBounds(186, 12, 98, 32);
SetOr.setText("源目录");
Or = new Text(shlmp, SWT.BORDER);
Or.setBounds(10, 12, 175, 32);
Neo = new Text(shlmp, SWT.BORDER);
Neo.setBounds(10, 44, 175, 32);
Button SetNeo = new Button(shlmp, SWT.NONE);
SetNeo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog directoryDialog=new DirectoryDialog(shlmp,SWT.OPEN);
directoryDialog.setText("选择新目录");
directoryDialog.setMessage("选择正确的目录");
neoString=directoryDialog.open();
if(!Objects.equals(neoString, null)) {
Neo.setText(neoString);
return;
}else {
MessageBox messageBox=new MessageBox(shlmp);
messageBox.setText("警告");
messageBox.setMessage("选择目录有误");
messageBox.open();
}
}
});
SetNeo.setText("新目录");
SetNeo.setBounds(186, 44, 98, 32);
Button btnOr = new Button(shlmp, SWT.NONE);
btnOr.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
File parentFile = new File(orString);
// 获取原来所有文件
File[] files = parentFile.listFiles();
ArrayList<String> keyArrayList=new ArrayList<String>();
for (File file : files) {
// 创建 Mp3File 对象
String format=file.getName().toLowerCase().substring(file.getName().toLowerCase().lastIndexOf(".") + 1);
if(Objects.equals(format, "mp3")) {
Mp3File mp3file = null;
try {
mp3file = new Mp3File(file);
} catch (UnsupportedTagException | InvalidDataException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 获取ID3v2对象,通过该对象可以获取文件信息如标题、艺术家等内容
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
String keyString=id3v2Tag.getTitle()+id3v2Tag.getAlbum()+id3v2Tag.getArtist()+id3v2Tag.getAlbumArtist()+id3v2Tag.getLength()+id3v2Tag.getComposer()+id3v2Tag.getYear();
// 将修改后的文件保存到新的目录
if((keyArrayList.contains(keyString))==false) {
keyArrayList.add(keyString);
}else {
file.delete();
}
}
}
}
});
btnOr.setBounds(10, 74, 274, 30);
btnOr.setText("仅在源目录进行筛选");
Button btnNeo = new Button(shlmp, SWT.NONE);
btnNeo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
File parentFile = new File(orString);
File targetFile = new File(neoString);
// 创建新文件父目录
targetFile.mkdirs();
// 获取原来所有文件
File[] files = parentFile.listFiles();
ArrayList<String> keyArrayList=new ArrayList<String>();
for (File file : files) {
// 创建 Mp3File 对象
String format=file.getName().toLowerCase().substring(file.getName().toLowerCase().lastIndexOf(".") + 1);
if(Objects.equals(format, "mp3")) {
Mp3File mp3file = null;
try {
mp3file = new Mp3File(file);
} catch (UnsupportedTagException | InvalidDataException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 获取文件名
String fileName = file.getName();
// 获取ID3v2对象,通过该对象可以获取文件信息如标题、艺术家等内容
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
String keyString=id3v2Tag.getTitle()+id3v2Tag.getAlbum()+id3v2Tag.getArtist()+id3v2Tag.getAlbumArtist()+id3v2Tag.getLength()+id3v2Tag.getComposer()+id3v2Tag.getYear();
// 将修改后的文件保存到新的目录
if((keyArrayList.contains(keyString))==false) {
keyArrayList.add(keyString);
try {
mp3file.save(neoString + "\\"+fileName);
} catch (NotSupportedException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
});
btnNeo.setText("源目录向新目录筛选");
btnNeo.setBounds(10, 106, 274, 30);
}
}
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.mpatric</groupId> <artifactId>mp3agic</artifactId> <version>0.9.1</version> </dependency> </dependencies>
DiskGenius的恢复文件功能出来的音频在多次导出后可能会有大量重复,结合SWT和大佬编写好的mp3agic接口实现了一个小程序用于筛选恢复出来的重复mp3音乐

浙公网安备 33010602011771号