【发布时间】:2014-09-02 11:31:07
【问题描述】:
有人知道如何在 Java 中获取 aac(音频格式)mp4(文件格式)音频的媒体长度吗?
任何帮助将不胜感激。
【问题讨论】:
有人知道如何在 Java 中获取 aac(音频格式)mp4(文件格式)音频的媒体长度吗?
任何帮助将不胜感激。
【问题讨论】:
这是我找到的解决方案(使用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;
}
【讨论】:
muxer(@user3489820 提到)是从 mp4parser 中分离出来的,现在分别位于:org.mp4parser/muxer。 Maven 包更改为org.mp4parser/mp4parser。有关详细信息,请参阅 https://mvnrepository.com/artifact/org.mp4parser/isoparser 并参阅 IsoParser、muxer 和 streaming JavaDoc。
我更新了@user3489820 的代码示例,添加了导入,分解出冗余代码并消除了不必要的强制转换。我还删除了 MemoryDataSourceImpl 调用,因为我的项目需要发现磁盘上 MP4 文件的长度,并且不需要该类:
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();
}
}
【讨论】:
你可以试试这个代码...
文件 file = new File("filename.mp3"); AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file); 地图属性 = baseFileFormat.properties(); 持续时间长 = (Long) properties.get("duration");
【讨论】:
您可以使用 ffmpeg 获取任何媒体的持续时间:
首先从运行时获取命令的输出:
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;
}
然后解析“持续时间”的输出:
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;
}
总共:
String out=execute("ffmpeg -i myfile.mp4");
int time=getTime(out);
【讨论】: