2014-04-17 66 views
0

我想連接2個mp4音頻文件。我累了使用https://code.google.com/p/mp4parser/
我想實現的:我想連接f1和f2並將其保存到f2。如果f2不存在 - f2 = f1。基本上appand f1到f2。
然而,當我使用mergeSongs()得到的音頻文件重複第一音頻文件(f1
常數如何連接mp4文件?

private static String mFileNameFromRec = null; 
    private static String mFileNameToUse = null; 
    mFileNameFromRec = context.getCacheDir().getAbsolutePath(); 
     mFileNameFromRec += "/audiorecordtest.mp4"; 
     mFileNameToUse = context.getCacheDir().getAbsolutePath(); 
     mFileNameToUse += "/audioToUse.mp4"; 

我的方法是下列:

private void mergeSongs() throws Exception { 
      String f1 = mFileNameFromRec; 
      String f2 = mFileNameToUse; 
      File merge = new File(mFileNameToUse); 
      if (!merge.exists()) { 
       InputStream in = new FileInputStream(new File(f1)); 
       OutputStream out = new FileOutputStream(new File(f2)); 

       // Transfer bytes from in to out 
       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = in.read(buf)) > 0) { 
        out.write(buf, 0, len); 
       } 
       in.close(); 
       out.close(); 
       Log.d("audio concatenation","was copied"); 

      } else { 
       Movie[] inMovies = null; 

       inMovies = new Movie[] { MovieCreator.build(f1), 
         MovieCreator.build(f2) }; 

       List<Track> videoTracks = new LinkedList<Track>(); 
       List<Track> audioTracks = new LinkedList<Track>(); 

       for (Movie m : inMovies) { 
        for (Track t : m.getTracks()) { 
         if (t.getHandler().equals("soun")) { 
          Log.d("audio concatenation","add audio track: "+ t.toString()); 
          audioTracks.add(t); 
         } 
         if (t.getHandler().equals("vide")) { 
          videoTracks.add(t); 
         } 
        } 
       } 

       Movie result = new Movie(); 

       if (audioTracks.size() > 0) { 
        result.addTrack(new AppendTrack(audioTracks 
          .toArray(new Track[audioTracks.size()]))); 
       } 
       if (videoTracks.size() > 0) { 
        result.addTrack(new AppendTrack(videoTracks 
          .toArray(new Track[videoTracks.size()]))); 
       } 

       Container out = new DefaultMp4Builder().build(result); 
       File newAudio = new File(mFileNameToUse); 
       FileOutputStream fOut = new FileOutputStream(newAudio); 
       FileChannel fc = fOut.getChannel(); 
       // FileChannel fc = new RandomAccessFile(mFileNameToUse, "rw") 
       // .getChannel(); 

       out.writeContainer(fc); 

       fc.close(); 
       fOut.flush(); 
       fOut.close(); 
       Log.d("audio concatenation","was concated"); 
      } 
     } 
+1

在Linux中使用本機工具帶命令行的/窗口,並調用從Java,不是最好的方式,但我懷疑你也會找到一個純粹的所有編碼的java impl – tgkprog

+0

對不起忘了添加android標籤。 – Yarh

回答

1

Andthere工作溶液
使用庫https://code.google.com/p/mp4parser/
download jars:

isoparser-1.0.1.jar 
aspectjrt-1.7.4.jar 

常數

mFileNameFromRec = context.getCacheDir().getAbsolutePath(); 
     mFileNameFromRec += "/audiorecordtest.mp4"; 
     mFileNameToUse = context.getCacheDir().getAbsolutePath(); 
     mFileNameToUse += "/audioToUse.mp4"; 

代碼

private void mergeSongs() throws Exception { 
     File merge = new File(mFileNameToUse); 
     String f1 = mFileNameFromRec; 
     String f2 = mFileNameToUse; 
     if (!merge.exists()) { 

      InputStream in = new FileInputStream(new File(f1)); 
      OutputStream out = new FileOutputStream(new File(f2)); 

      // Transfer bytes from in to out 
      byte[] buf = new byte[1024]; 
      int len; 
      while ((len = in.read(buf)) > 0) { 
       out.write(buf, 0, len); 
      } 
      in.close(); 
      out.close(); 
      Log.d("audio concatenation", "was copied"); 

     } else { 

      Movie[] inMovies; 

      inMovies = new Movie[] { MovieCreator.build(f2), 
        MovieCreator.build(f1), }; 

      List<Track> videoTracks = new LinkedList<Track>(); 
      List<Track> audioTracks = new LinkedList<Track>(); 

      for (Movie m : inMovies) { 
       for (Track t : m.getTracks()) { 
        if (t.getHandler().equals("soun")) { 
         audioTracks.add(t); 
        } 
        if (t.getHandler().equals("vide")) { 
         videoTracks.add(t); 
        } 
       } 
      } 

      Movie result = new Movie(); 
      if (videoTracks.size() > 0) { 
       result.addTrack(new AppendTrack(videoTracks 
         .toArray(new Track[videoTracks.size()]))); 
      } 

      if (audioTracks.size() > 0) { 
       result.addTrack(new AppendTrack(audioTracks 
         .toArray(new Track[audioTracks.size()]))); 
      } 

      Container out = new DefaultMp4Builder().build(result); 

      RandomAccessFile ram = new RandomAccessFile(String.format(context 
        .getCacheDir() + "/output.mp4"), "rw"); 
      FileChannel fc = ram.getChannel(); 
      out.writeContainer(fc); 
      ram.close(); 
      fc.close(); 
      // IsoFile out = (IsoFile) new DefaultMp4Builder().build(result); 
      // FileOutputStream fos = new FileOutputStream(new File(
      // String.format(context.getCacheDir() + "/output.mp4"))); 
      // out.getBox(fos.getChannel()); 
      // fos.close(); 
      String mergedFilepath = String.format(context.getCacheDir() 
        + "/output.mp4"); 

      copy(new File(mergedFilepath), new File(mFileNameToUse)); 
      Toast.makeText(getApplicationContext(), "success", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }