Java - AAC MP4 - How to get media length?
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;
}
source to share
muxer (mentioned by @ user3489820) was created in mp4parser and now lives separately at org.mp4parser / muxer . Maven package has been changed to org.mp4parser/mp4parser
. For more information see https://mvnrepository.com/artifact/org.mp4parser/isoparser and have a look at IsoParser , multiplexer and streaming JavaDoc.
I updated @ user3489820's sample code, added imports, crossed out extra code, and removed unnecessary casts. I also removed the call MemoryDataSourceImpl
because my project needed to determine the length of the MP4 file on disk, and this 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();
}
}
source to share
You can use ffmpeg to get the duration of any media:
First, get the command output 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 analyze 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;
}
Total:
String out=execute("ffmpeg -i myfile.mp4");
int time=getTime(out);
source to share