Java-AAC MP4-如何获取媒体长度?

Java-AAC MP4-如何获取媒体长度?,java,mp4,aac,Java,Mp4,Aac,有人知道如何在Java中获取aac(音频格式)mp4(文件格式)音频的媒体长度吗 非常感谢您的帮助。您可以尝试此代码 File File=新文件(“filename.mp3”); AudioFileFormat baseFileFormat=新MpegAudioFileReader().getAudioFileFormat(文件); 映射属性=baseFileFormat.properties(); Long duration=(Long)properties.get(“duration”) 以

有人知道如何在Java中获取aac(音频格式)mp4(文件格式)音频的媒体长度吗


非常感谢您的帮助。

您可以尝试此代码

File File=新文件(“filename.mp3”); AudioFileFormat baseFileFormat=新MpegAudioFileReader().getAudioFileFormat(文件); 映射属性=baseFileFormat.properties();
Long duration=(Long)properties.get(“duration”)

以下是我找到的解决方案(使用):

muxer(由@user3489820提及)是从mp4parser分离出来的,现在分别居住在:。Maven包已更改为
org.mp4parser/mp4parser
。有关更多信息,请参阅和,以及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();
    }
}

您可以使用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;
  }
然后分析“持续时间”的输出:


这是MP3,不是AAC。MpegAudioFileReader是为MP3设计的。目前,我一直在使用JAAD。问题是,我试图解析的mp4文件最后有一些示例表。这意味着,我需要RandomAccessFile来解析它,但我只有ByteArrayInputStream。所以一个解决方案是在文件系统上重新创建一个文件(这对服务器不好)…虽然我不喜欢这个解决方案,但是还没有找到如何使用“内存中”aac文件(即来自ByteArrayInputStream或仅仅是byte[])最后,用mp4解析器找到了解决方案(不是来自google,只是在google代码上)。好的,找到了。应该花更多时间尝试使用mp4解析器
  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);