翻出了4年前买的ipod shuffle,想起高中时候拿老人机上学,自己悄咪咪的买了个shuffle来听歌
然后现在给shuffle连上了电脑,但是里面的歌曲因为苹果的原因全都是一些什么四个大写英文字母,根本不知道这首歌是什么歌,然后歌曲又太多一个个改忒麻烦。
于是我就想到用Java来写一个程序,帮助我一次性修改这些文件名。
首先下载JAudioTagger.jar库,下载地址
(1)这个类是用来方便查找歌曲名+其他各种信息的
public class Tag {
private String SongName;
private String Artist;
private String Album;
private String length;
public String getSongName() {
return SongName;
}
public String getArtist() {
return Artist;
}
public void setArtist(String artist) {
Artist = artist;
}
public void setSongName(String songName) {
SongName = songName;
}
public String getAlbum() {
return Album;
}
public void setAlbum(String album) {
Album = album;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
}
(2)这个类是用来读取MP3文件并搜索其信息的
package com.example.util;
import java.io.IOException;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import com.example.entity.Tag;
public class TagInfoUtil {
public static Tag Mp3InfoRead(String path) {
MP3File file;
try {
file = new MP3File(path);
String songName = file.getID3v2Tag().frameMap.get("TIT2")
.toString();
String artist = file.getID3v2Tag().frameMap.get("TPE1").toString();
String album = file.getID3v2Tag().frameMap.get("TALB").toString();
String length = file.getMP3AudioHeader().getTrackLengthAsString();
songName = songName.substring(6, songName.length() - 3);// 歌名
artist = artist.substring(6, artist.length() - 3);// 歌手名
album = album.substring(6, album.length() - 3);// 唱片集名
Tag tag = new Tag();
tag.setSongName(songName);
tag.setAlbum(album);
tag.setArtist(artist);
tag.setLength(length);
// System.out.println("歌名" + songName);
// System.out.println("歌手" + artist);
// System.out.println("专辑:" + album);
return tag;
} catch (IOException | TagException | ReadOnlyFileException
| CannotReadException | InvalidAudioFrameException
| NullPointerException e) {
e.printStackTrace();
throw new RuntimeException("获取Mp3 tag信息出错!");
}
}
}
(3)文件夹读取类,这里是读取文件夹!不要用单个文件
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.example.entity.Tag;
import com.example.util.TagInfoUtil;
public class ReadFile {
public static boolean NameAlter(String filepath)
throws FileNotFoundException, IOException {
try {
TagInfoUtil mp = new TagInfoUtil();
File file = new File(filepath);
if (file.isDirectory()) {
System.out.println("开始修改文件名");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);// 读取一个MP3文件
if (!readfile.getName().contains(".mp3")) {
readfile.delete();// 如果不是MP3文件就删掉
continue;
}
if (!readfile.isDirectory()) {
// System.out.println("path=" + readfile.getPath());
// System.out.println(
// "absolutepath=" + readfile.getAbsolutePath());
// System.out.println("name=" + readfile.getName());
Tag t = mp.Mp3InfoRead(readfile.getAbsolutePath());
File newFile = new File(
filepath + File.separator + t.getSongName()
+ "-" + t.getArtist() + ".mp3");// 重命名格式为
// 歌名-歌手
if (readfile.renameTo(newFile)) {
System.out.println("success");
} else {
System.out.println("dissuccess");
}
}
}
}
} catch (Exception e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
}
(4)测试用
import java.io.FileNotFoundException;
import java.io.IOException;
public class test {
public static void main(String[] args)
throws FileNotFoundException, IOException {
String filename = "G:\\music";
ReadFile.NameAlter(filename);
}
}
有一些MP3文件有问题,会出现NullPointException,有可能是文件出现问题,导致文件指针找不到,这样的话手动修改吧,反正也不多。