Skip to content

Instantly share code, notes, and snippets.

@xlogix
Created January 15, 2018 06:27
Show Gist options
  • Save xlogix/bf74e138d59ec31000910196c8ae4fec to your computer and use it in GitHub Desktop.
Save xlogix/bf74e138d59ec31000910196c8ae4fec to your computer and use it in GitHub Desktop.
Sorting of Tags in MP3 files
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
public class ID3 {
File mp3File;
private final String encoding = "Cp437";
public String title, artist, album, year, comment;
public Byte track, genre;
public ID3(File mp3File) {
this.mp3File = mp3File;
}
public void readID3() throws IOException {
if (ID3Exists()) {
RandomAccessFile in = new RandomAccessFile(mp3File, "r");
in.seek(in.length() - 125);
byte []buffer = new byte[125];
if (in.read(buffer, 0, 125) != 125) {
in.close();
throw new IOException("ID3 Tag does not exist");
}
in.close();
String tag = new String(buffer, 0, 125, encoding);
title = tag.substring(0, 30).trim();
artist = tag.substring(30, 60).trim();
album = tag.substring(60, 90).trim();
year = tag.substring(90, 94).trim();
comment = tag.substring(94, 123).trim();
track = (byte) tag.charAt(123);
genre = (byte) tag.charAt(124);
} else
throw new IOException("ID3 Tag does not exist");
}
public boolean ID3Exists() throws FileNotFoundException {
RandomAccessFile raf = new RandomAccessFile(mp3File, "r");
try {
if (raf.length() < 129) {
// file to short for an ID3 tag
raf.close();
return false;
} else {
// go to position where "TAG" must be
long seekPos = raf.length() - 128;
raf.seek(seekPos);
byte buffer[] = new byte[3];
if (raf.read(buffer, 0, 3) != 3) {
raf.close();
throw new IOException("Read beyond end of file");
}
String testTag = new String(buffer, 0, 3, encoding);
raf.close();
return testTag.equals("TAG");
}
}
catch (IOException e) {
e.printStackTrace();
}
try {
raf.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return false;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getAlbum() {
return album;
}
public String getYear() {
return year;
}
public String getComment() {
return comment;
}
public Byte getTrack() {
return track;
}
public Byte getGenre() {
return genre;
}
public File getMp3File() {
return mp3File;
}
}
import java.io.*;
import java.util.*;
public class mp3TagSort {
public static void main(String[] args) throws IOException {
File dir = new File("");
if (args.length != 1) {
System.out.println("Program accepts one command-line argument. Exiting!");
System.exit(1);
}
File f = new File(args[0]);
if (f.isDirectory()) {
System.out.println("is directory, proceeding further");
// Call the function
extract(dir);
}
else if (f.isFile()) {
System.out.println("is file. Exiting!");
}
else {
System.out.println("Shouldn't happen");
}
}
private static void extract (File dir) throws IOException {
System.out.println("\n\nDirectory:: " + dir);
File[] MP3Files;
int i = 0;
MP3Files = dir.listFiles(new MP3FileFilter());
for (File f : MP3Files) {
ID3 ID3Tag = new ID3(f);
if (f.isFile() && ID3Tag.ID3Exists()) {
ID3Tag.readID3();
}
}
}
}
class MP3FileFilter implements FileFilter {
public boolean accept(File pathname) {
return pathname.getName().toLowerCase().endsWith(".mp3") || pathname.isDirectory();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment