|
|
|
@ -7,9 +7,9 @@ import android.os.Bundle; |
|
|
|
|
import android.os.Environment; |
|
|
|
|
import android.os.Handler; |
|
|
|
|
import android.os.Message; |
|
|
|
|
import android.support.v7.app.AppCompatActivity; |
|
|
|
|
import android.util.Log; |
|
|
|
|
import android.view.View; |
|
|
|
|
import android.widget.LinearLayout; |
|
|
|
|
import android.widget.ProgressBar; |
|
|
|
|
import com.frank.ffmpeg.FFmpegCmd; |
|
|
|
|
import com.frank.ffmpeg.R; |
|
|
|
@ -22,18 +22,19 @@ import java.io.File; |
|
|
|
|
* 使用ffmpeg进行音视频合成与分离 |
|
|
|
|
* Created by frank on 2018/1/23. |
|
|
|
|
*/ |
|
|
|
|
public class MediaHandleActivity extends AppCompatActivity implements View.OnClickListener{ |
|
|
|
|
public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
|
private final static String TAG = MediaHandleActivity.class.getSimpleName(); |
|
|
|
|
private static final String PATH = Environment.getExternalStorageDirectory().getPath(); |
|
|
|
|
private String srcFile = PATH + File.separator + "hello.mp4"; |
|
|
|
|
private String videoFile = PATH + File.separator + "flash-tree.mp4";//flash-tree.mp4
|
|
|
|
|
String temp = PATH + File.separator + "temp.mp4"; |
|
|
|
|
private String videoFile; |
|
|
|
|
private String temp = PATH + File.separator + "temp.mp4"; |
|
|
|
|
private boolean isMux; |
|
|
|
|
private final static int MSG_MUX = 100; |
|
|
|
|
private final static int MSG_BEGIN = 101; |
|
|
|
|
private final static int MSG_FINISH = 102; |
|
|
|
|
private ProgressBar progress_media; |
|
|
|
|
private ProgressBar progressMedia; |
|
|
|
|
private int viewId; |
|
|
|
|
private LinearLayout layoutMediaHandle; |
|
|
|
|
|
|
|
|
|
@SuppressLint("HandlerLeak") |
|
|
|
|
private Handler mHandler = new Handler(){ |
|
|
|
@ -73,12 +74,12 @@ public class MediaHandleActivity extends AppCompatActivity implements View.OnCli |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case MSG_BEGIN: |
|
|
|
|
progress_media.setVisibility(View.VISIBLE); |
|
|
|
|
setGone(); |
|
|
|
|
progressMedia.setVisibility(View.VISIBLE); |
|
|
|
|
layoutMediaHandle.setVisibility(View.GONE); |
|
|
|
|
break; |
|
|
|
|
case MSG_FINISH: |
|
|
|
|
progress_media.setVisibility(View.GONE); |
|
|
|
|
setVisible(); |
|
|
|
|
progressMedia.setVisibility(View.GONE); |
|
|
|
|
layoutMediaHandle.setVisibility(View.VISIBLE); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
@ -86,83 +87,70 @@ public class MediaHandleActivity extends AppCompatActivity implements View.OnCli |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
int getLayoutId() { |
|
|
|
|
return R.layout.activity_media_handle; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onCreate(Bundle savedInstanceState) { |
|
|
|
|
super.onCreate(savedInstanceState); |
|
|
|
|
setContentView(R.layout.activity_media_handle); |
|
|
|
|
|
|
|
|
|
hideActionBar(); |
|
|
|
|
initView(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void initView() { |
|
|
|
|
progress_media = (ProgressBar) findViewById(R.id.progress_media) ; |
|
|
|
|
findViewById(R.id.btn_mux).setOnClickListener(this); |
|
|
|
|
findViewById(R.id.btn_extract_audio).setOnClickListener(this); |
|
|
|
|
findViewById(R.id.btn_extract_video).setOnClickListener(this); |
|
|
|
|
progressMedia = getView(R.id.progress_media); |
|
|
|
|
layoutMediaHandle = getView(R.id.layout_media_handle); |
|
|
|
|
initViewsWithClick( |
|
|
|
|
R.id.btn_mux, |
|
|
|
|
R.id.btn_extract_audio, |
|
|
|
|
R.id.btn_extract_video |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setVisible() { |
|
|
|
|
findViewById(R.id.btn_mux).setVisibility(View.VISIBLE); |
|
|
|
|
findViewById(R.id.btn_extract_audio).setVisibility(View.VISIBLE); |
|
|
|
|
findViewById(R.id.btn_extract_video).setVisibility(View.VISIBLE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void setGone() { |
|
|
|
|
findViewById(R.id.btn_mux).setVisibility(View.GONE); |
|
|
|
|
findViewById(R.id.btn_extract_audio).setVisibility(View.GONE); |
|
|
|
|
findViewById(R.id.btn_extract_video).setVisibility(View.GONE); |
|
|
|
|
@Override |
|
|
|
|
public void onViewClick(View view) { |
|
|
|
|
viewId = view.getId(); |
|
|
|
|
selectFile(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onClick(View v) { |
|
|
|
|
int handleType; |
|
|
|
|
switch (v.getId()){ |
|
|
|
|
case R.id.btn_mux: |
|
|
|
|
handleType = 0; |
|
|
|
|
break; |
|
|
|
|
case R.id.btn_extract_audio: |
|
|
|
|
handleType = 1; |
|
|
|
|
break; |
|
|
|
|
case R.id.btn_extract_video: |
|
|
|
|
handleType = 2; |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
handleType = 0; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
doHandleMedia(handleType); |
|
|
|
|
void onSelectedFile(String filePath) { |
|
|
|
|
doHandleMedia(filePath); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 调用ffmpeg处理音视频 |
|
|
|
|
* @param handleType handleType |
|
|
|
|
* @param srcFile srcFile |
|
|
|
|
*/ |
|
|
|
|
private void doHandleMedia(int handleType){ |
|
|
|
|
private void doHandleMedia(String srcFile) { |
|
|
|
|
String[] commandLine = null; |
|
|
|
|
switch (handleType){ |
|
|
|
|
case 0://音视频合成
|
|
|
|
|
if (!FileUtil.checkFileExist(videoFile)){ |
|
|
|
|
if (!FileUtil.checkFileExist(srcFile)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (!FileUtil.isVideo(srcFile)) { |
|
|
|
|
showToast(getString(R.string.wrong_video_format)); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (viewId){ |
|
|
|
|
case R.id.btn_mux://音视频合成
|
|
|
|
|
try { |
|
|
|
|
//视频文件有音频,先把纯视频文件抽取出来
|
|
|
|
|
commandLine = FFmpegUtil.extractVideo(videoFile, temp); |
|
|
|
|
videoFile = srcFile; |
|
|
|
|
commandLine = FFmpegUtil.extractVideo(srcFile, temp); |
|
|
|
|
isMux = true; |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case 1://提取音频
|
|
|
|
|
if (!FileUtil.checkFileExist(srcFile)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
case R.id.btn_extract_audio://提取音频
|
|
|
|
|
String extractAudio = PATH + File.separator + "extractAudio.aac"; |
|
|
|
|
commandLine = FFmpegUtil.extractAudio(srcFile, extractAudio); |
|
|
|
|
break; |
|
|
|
|
case 2://提取视频
|
|
|
|
|
if (!FileUtil.checkFileExist(srcFile)){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
case R.id.btn_extract_video://提取视频
|
|
|
|
|
String extractVideo = PATH + File.separator + "extractVideo.mp4"; |
|
|
|
|
commandLine = FFmpegUtil.extractVideo(srcFile, extractVideo); |
|
|
|
|
break; |
|
|
|
@ -176,7 +164,7 @@ public class MediaHandleActivity extends AppCompatActivity implements View.OnCli |
|
|
|
|
* 执行ffmpeg命令行 |
|
|
|
|
* @param commandLine commandLine |
|
|
|
|
*/ |
|
|
|
|
private void executeFFmpegCmd(final String[] commandLine){ |
|
|
|
|
private void executeFFmpegCmd(final String[] commandLine) { |
|
|
|
|
if(commandLine == null){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|