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