Skip to content

Instantly share code, notes, and snippets.

@vladholubiev
Created June 28, 2014 12:24
Show Gist options
  • Save vladholubiev/495bc070f7b7e0e28988 to your computer and use it in GitHub Desktop.
Save vladholubiev/495bc070f7b7e0e28988 to your computer and use it in GitHub Desktop.
Counts length of all music in folder. days:hours:min:sec
package com.company;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException, UnsupportedAudioFileException {
ArrayList<String> paths = new ArrayList<>();
long ms = 0;
int i = 0; //Iterator to show progress
Files.walk(Paths.get("E:\\Music\\")).forEach(filePath -> {
if (Files.isRegularFile(filePath) && filePath.toString().endsWith("mp3")) {
paths.add(filePath.toString()); //Add to array of paths
}
});
for (String s : paths) {
File file = new File(s);
AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
ms += (((long) properties.get("duration") / 1000));
System.out.println(i++ + " / " + paths.size());
}
int sec = (int) (ms / 1000) % 60;
int min = (int) ((ms / (1000 * 60)) % 60);
int h = (int) ((ms / (1000 * 60 * 60)) % 24);
int days = (int) (ms / (1000 * 60 * 60 * 24));
String result = String.format("%02d:%02d:%02d:%02d", days, h, min, sec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment