【问题标题】:Java - AAC MP4 - How to get media length?Java - AAC MP4 - 如何获取媒体长度?
【发布时间】:2014-09-02 11:31:07
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: java mp4 aac


    【解决方案1】:

    这是我找到的解决方案(使用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;
    }
    

    【讨论】:

      【解决方案2】:

      muxer(@user3489820 提到)是从 mp4parser 中分离出来的,现在分别位于:org.mp4parser/muxer。 Maven 包更改为org.mp4parser/mp4parser。有关详细信息,请参阅 https://mvnrepository.com/artifact/org.mp4parser/isoparser 并参阅 IsoParsermuxerstreaming 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();
          }
      }
      

      【讨论】:

        【解决方案3】:

        你可以试试这个代码...

        文件 file = new File("filename.mp3"); AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file); 地图属性 = baseFileFormat.properties(); 持续时间长 = (Long) properties.get("duration");

        【讨论】:

        • 这是 MP3,不是 AAC。 MpegAudioFileReader 专为 MP3 设计
        • 目前,我被 JAAD 困住了。问题是,我试图解析的 mp4 文件最后有示例表。这意味着,我需要 RandomAccessFile 来解析它,但我只有 ByteArrayInputStream。所以......一种解决方案是在文件系统上重新创建一个文件(这对服务器不利)......虽然,我不喜欢这个解决方案
        • 试过了,还没有找到如何处理“内存中”aac 文件(即来自 ByteArrayInputStream 或只是 byte[])
        • 最后,用mp4解析器找到了解决方案(不是来自google,只是在google code上)。
        • 好的,找到了。应该花更多时间尝试使用 mp4 解析器
        【解决方案4】:

        您可以使用 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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多