2017-10-20 245 views
0

你能告訴我,下面的命令是否是正確的 一個用於視頻音頻合併?FFmpeg音頻視頻合併commad

vabsolutePath =視頻文件的絕對路徑(僅視頻),
aabsolutePath =音頻文件的絕對路徑(僅音頻)

String ccommand[] = {"-i",vabsolutePath,"-i",aabsolutePath, "-c:v", "copy", "-c:a", "aac","-shortest", dabsolutePath}; 

下面的代碼在機器人用於合併。 這裏的問題是代碼正在執行,但輸出合併文件「result.mp4」不可播放/未生成。 你能幫忙找出代碼/命令中的問題嗎?

public class Mrge extends AppCompatActivity { 

    private Button var_button_save,var_button_send; 
    Uri vuri=null; 
    public String vabsolutePath=null, aabsolutePath=null, dabsolutePath=null; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.message_layout); 

     OutputStream out; 

     try { 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      InputStream ins = getResources().openRawResource(
        getResources().getIdentifier("anvkl", 
          "raw", getPackageName())); 


      byte[] buf = new byte[1024]; 
      int n; 
      while (-1 != (n = ins.read(buf))) 
       stream.write(buf, 0, n); 

      byte[] bytes = stream.toByteArray(); 

      String root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"; 
      File createDir = new File(root + "master" + File.separator); 

      createDir.mkdir(); 


      File file = new File(root + "master" + File.separator + "master.mp4"); 


      file.createNewFile(); 
      out = new FileOutputStream(file); 
      out.write(bytes); 
      out.close(); 



      vabsolutePath = file.getAbsolutePath(); 

      //------------------------------------------------------------------- 

      ins = getResources().openRawResource(
        getResources().getIdentifier("audio", 
          "raw", getPackageName())); 

      while (-1 != (n = ins.read(buf))) 
       stream.write(buf, 0, n); 

      bytes = stream.toByteArray(); 


      root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"; 
      createDir = new File(root + "audio" + File.separator); 
      createDir.mkdir(); 


      file = new File(root + "audio" + File.separator + "audio.aac"); 

      file.createNewFile(); 
      out = new FileOutputStream(file); 
      out.write(bytes); 
      out.close(); 

      aabsolutePath = file.getAbsolutePath(); 

      root = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"; 
      createDir = new File(root + "result" + File.separator); 
      createDir.mkdir(); 


      file = new File(root + "result" + File.separator + "result.mp4"); 

      file.createNewFile(); 

      dabsolutePath = file.getAbsolutePath(); 


      //------------------------------------------------------------------------ 






     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     String ccommand[] = {"-y", "-i",vabsolutePath,"-i",aabsolutePath, "-c:v", "copy", "-c:a", "aac","-shortest", dabsolutePath}; 

     loadFFMpegBinary(); 
     execFFmpegBinary(ccommand); 

    } 






     FFmpeg ffmpeg; 
     private void loadFFMpegBinary() { 
      try { 
       if (ffmpeg == null) { 

        ffmpeg = FFmpeg.getInstance(this); 
       } 
       ffmpeg.loadBinary(new LoadBinaryResponseHandler() { 
        @Override 
        public void onFailure() { 
         //showUnsupportedExceptionDialog(); 
        } 

        @Override 
        public void onSuccess() { 

        } 
       }); 
      } catch (FFmpegNotSupportedException e) { 
       //showUnsupportedExceptionDialog(); 
      } catch (Exception e) { 

      } 
     } 




    private void execFFmpegBinary(final String[] command) { 
     try { 
      ffmpeg.execute(command, new ExecuteBinaryResponseHandler() 
      { 
       @Override 
       public void onFailure(String s) { 

       } 

       @Override 
       public void onSuccess(String s) { 


       } 


      @Override 
      public void onProgress(String s) { 

      } 

      @Override 
      public void onStart() { 

      } 

      @Override 
      public void onFinish() { 


      } 
     }); 
    } catch (FFmpegCommandAlreadyRunningException e) { 

      String m="hi"; 

    } 




} 


} 
+0

對於將來的問題,請指定是否需要幫助在代碼中執行'ffmpeg'命令,或者如果您需要使用'ffmpeg'本身的幫助。如果是後者,請從代碼/腳本中刪除額外的複雜層,並在[su]處提出任何新問題。包括實際的,無腳本的'ffmpeg'命令和命令的完整日誌/控制檯輸出。 – LordNeckbeard

回答

1

雖然這是不可能給你一個具體的答案,由於缺乏細節,我會說,假設:

  • ffmpeg命令代碼中的正確實施,並且不提供錯誤。
  • 您正在將視頻和音頻格式輸出到適當的輸出格式容器。
  • vabsolutePath只包含視頻和aabsolutePath只包含音頻。否則,使用-map選項手動選擇流而不是依賴默認的stream selection行爲。否則,它可能會選擇錯誤的流。
+0

問題用完整的代碼和問題描述更新。請您檢查? – djac

+0

@djac與'ffmpeg'命令本身或你的代碼有問題嗎?顯示正在發出的實際'ffmpeg'命令以及該命令的完整日誌。 – LordNeckbeard

+0

CAN NOT LINK EXECUTABLE「/ data/data/xxxxxffmpeg」:有文字重定位 。這是錯誤,我在我的gradle中「編譯」com.writingminds:FFmpegAndroid:0.3.2「。 – djac

0

嘗試在下面way-

File moviesDir = Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_MOVIES 
     ); 
     boolean success = true; 
     if (!moviesDir.exists()) { 
      success = moviesDir.mkdir(); 
     } 
     if(success) { 
      File dest = new File(moviesDir, filePrefix + fileExtn); 
      int fileNo = 0; 
      while (dest.exists()) { 
       fileNo++; 
       dest = new File(moviesDir, filePrefix + fileNo + fileExtn); 
      } 
     ....//execute ffmpeg command with dest.getAbsolutePath() as an output file path 

     }else 
     { 
      Snackbar.make(mainlayout, "Unable to create directory", 4000).show(); 

     } 

創建目的地(輸出)文件使用dest.getAbsolutePath()作爲一個輸出文件路徑的ffmpeg command.You可以改變所需的目錄按您的要求。