2015-10-05 24 views
0

想法

合併單個視頻軌道和多音軌我創建一個保存到設備的功能爲合併一個視頻軌道與一個(或者,任選兩個)音軌電影編輯軟件。在Android

首先,我有多個視頻剪輯合併到單個視頻軌道使用MP4Parser(link)。

然後,有多個音頻剪輯我想合併到單個音軌。不應該追加這些剪輯,而是在特定時間將其合併到單個音軌中。例如。我們有兩個音頻剪輯(A1,A2)和一個60秒的視頻軌道(V1)。這些音頻片段可能會重疊,或者它們之間有白色噪音。整個音軌的長度必須與視頻軌道相匹配,最長可達60秒。 可以有多達100個音頻剪輯添加到音軌1

  • V1 - 60.0小號
  • A1 - 0.3秒
  • A2 - 1.1秒即可

末,有可能是一個可選的第二個音軌,其中包含一個配樂,以適應V1視頻軌道。

摘要

這是它會是什麼樣子:

視頻軌道1:------------------------ -------------------------------------------------- ------] 60秒

音軌1:[-A1 - A2 --------------------------- -----------------------------------------] 60秒

音軌2:[----------------------------------------------- --------------------------------] 60秒

問題

我試圖通過添加白噪聲的x秒(空WAV文件)音軌如上所述得到一個完整長度的軌道接近的問題,但如果聲音顯然是行不通的重疊。我還有什麼其他方法可以解決這個問題?

private static final String OUTPUT = "output.mp4"; 
private static final String STORED_LOCATION = "/storage/emulated/0/" 

/** 
* Merges two videos that are located in /storage/emulated/0/ and saves it to the same place with the given parameters. Uses the ffmpeg/javacv library. All this is done in an Async task, not blocking the UI thread but showing a progress bar and a toast at the end. 
* 
*/ 
private void mergeVideosAsync() 
{ 
    new AsyncTask<Void, Void, String>() 
    { 
     @Override 
     protected String doInBackground(Void... arg0) 
     { 
      try 
      { 
       List<Movie> movieList = new ArrayList<>(); 
       for (int i = 0; i < mVideoPathList.size(); ++i) 
       { 
        movieList.add(MovieCreator.build(new File(mVideoPathList.get(i)).getAbsolutePath())); 
       } 

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

       for (Movie m : movieList) 
       { 
        for (Track t : m.getTracks()) 
        { 
         if (t.getHandler().equals("soun")) 
         { 
          //TODO: Add audio tracks here to the merging process 
          // 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()]))); 
       } 
       BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(result); 
       mOutputPath = String.format(STORED_LOCATION + File.separator + OUTPUT_FILENAME); 
       WritableByteChannel fc = new RandomAccessFile(mOutputPath, "rw").getChannel(); 
       out.writeContainer(fc); 
       fc.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return mOutputPath; 
     } 
    }.execute(); 
} 
+0

你能解決這個問題嗎?如果是的話,你能告訴我們如何? – LostPuppy

回答

0

如果你的音軌重疊,那麼你有問題,因爲你需要重新編碼音頻。 如果音軌是不重疊的,那麼你可能能夠使用SilenceTrackImpl:

Track nuAudio = new AppendTrack(
    audioTrackA1, new SilenceTrackImpl(100), 
    audioTrackA2, new SilenceTrackImpl(500),   
    audioTrackA3, new SilenceTrackImpl(1000), 
    audioTrackA4, new SilenceTrackImpl(50), 
) 

等。