Reputation: 1519
Does anybody know how to get media length of the aac ( audio format ) mp4 ( file format ) audio in Java?
Any help would be highly appreciated.
Upvotes: 4
Views: 5032
Reputation: 2682
You can use ffmpeg to get the duration of any media:
First get the output of the command from the runtime:
public String execute(String cmd) {
String output=null;
try {
Runtime rt = Runtime.getRuntime();
Process pr=rt.exec(cmd);
final InputStream es=pr.getErrorStream();
class C extends Thread {
String type, output="";
InputStream is;
public C(InputStream is, String type) { this.is=is; this.type=type; }
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while((line=br.readLine())!=null)
output+=line+" ";
}
catch (IOException ioe) { ioe.printStackTrace(); }
}
};
C t=new C(es);
t.start();
int exitVal=pr.waitFor();
System.out.println("ExitValue: " + exitVal);
output=""+t.output;
}
catch(Throwable t) { t.printStackTrace(); }
return output;
}
Then parse the output for "Duration":
public int getTime(String out) {
int i=0, j=0, k, l, x=0, y=0, ind=0;
String d="", frs="";
BufferedReader dis=null;
String nextline=out;
ind=0;
if((ind=nextline.indexOf("Duration:"))>-1)
d=nextline.substring(ind+9, nextline.indexOf(',', ind+1)).trim();
int ind2=0;
int h=0, m=0, s=0, xxx=0;
if((ind=d.indexOf(":"))>-1) {
h=Integer.parseInt(d.substring(ind2, ind));
ind2=ind+1;
}
if((ind=d.indexOf(":", ind+1))>-1) {
m=Integer.parseInt(d.substring(ind2, ind));
ind2=ind+1;
}
if((ind=d.indexOf(".", ind+1))>-1) {
s=Integer.parseInt(d.substring(ind2, ind));
}
if(ind<d.length()) {
ind2=ind+1;
if((ind=d.indexOf(".", ind+1))>-1) {
xxx=Integer.parseInt(d.substring(ind2, d.length()));
}
}
try {
dis.close();
}
catch (Exception ex) { }
return h*3600+m*60+s;
}
In total:
String out=execute("ffmpeg -i myfile.mp4");
int time=getTime(out);
Upvotes: -1
Reputation: 8403
muxer (mentioned by @user3489820) was hived from mp4parser and now lives separately at: org.mp4parser/muxer. The Maven package was changed to org.mp4parser/mp4parser
. For more information see https://mvnrepository.com/artifact/org.mp4parser/isoparser and see the IsoParser, muxer and streaming JavaDoc.
I updated @user3489820's code example, added imports, factored out redundant code and eliminated unnecessary casts. I also removed the MemoryDataSourceImpl
call because my project needed to discover the length of an MP4 file on disk, and that class was not needed:
import java.io.File;
import org.mp4parser.IsoFile;
import org.mp4parser.boxes.iso14496.part12.MovieHeaderBox;
public class Blah {
public static long getAudioLength(File file) throws Exception {
IsoFile isoFile = new IsoFile(file);
MovieHeaderBox mhb = isoFile.getMovieBox().getMovieHeaderBox();
return mhb.getDuration() / mhb.getTimescale();
}
}
Upvotes: 3
Reputation: 1519
Here is the solution I found ( using mp4parser on github ):
public static long getAudioLength(byte[] content) throws Exception {
IsoFile isoFile = new IsoFile(new MemoryDataSourceImpl(content));
double lengthInSeconds = (double)isoFile.getMovieBox().getMovieHeaderBox().getDuration() / isoFile.getMovieBox().getMovieHeaderBox().getTimescale();
return (long)lengthInSeconds;
}
Upvotes: 4
Reputation: 41
You can try this code...
File file = new File("filename.mp3"); AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file); Map properties = baseFileFormat.properties(); Long duration = (Long) properties.get("duration");
Upvotes: -1