|
|
|
@ -1,8 +1,6 @@ |
|
|
|
|
package com.frank.ffmpeg.activity; |
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint; |
|
|
|
|
import android.media.MediaMetadataRetriever; |
|
|
|
|
import android.media.MediaPlayer; |
|
|
|
|
import android.os.Bundle; |
|
|
|
|
import android.os.Environment; |
|
|
|
|
import android.os.Handler; |
|
|
|
@ -12,16 +10,17 @@ import android.view.View; |
|
|
|
|
import android.widget.LinearLayout; |
|
|
|
|
import android.widget.TextView; |
|
|
|
|
|
|
|
|
|
import com.frank.ffmpeg.FFmpegCmd; |
|
|
|
|
import com.frank.ffmpeg.R; |
|
|
|
|
import com.frank.ffmpeg.handler.FFmpegHandler; |
|
|
|
|
import com.frank.ffmpeg.util.FFmpegUtil; |
|
|
|
|
import com.frank.ffmpeg.util.FileUtil; |
|
|
|
|
import com.frank.ffmpeg.util.ThreadPoolUtil; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.util.Locale; |
|
|
|
|
|
|
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN; |
|
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_CONTINUE; |
|
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH; |
|
|
|
|
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_PROGRESS; |
|
|
|
|
|
|
|
|
@ -33,8 +32,7 @@ public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
|
private final static String TAG = MediaHandleActivity.class.getSimpleName(); |
|
|
|
|
private static final String PATH = Environment.getExternalStorageDirectory().getPath(); |
|
|
|
|
private String videoFile; |
|
|
|
|
private String temp = PATH + File.separator + "temp.mp4"; |
|
|
|
|
private String audioFile = PATH + File.separator + "tiger.mp3"; |
|
|
|
|
|
|
|
|
|
private LinearLayout layoutProgress; |
|
|
|
|
private TextView txtProgress; |
|
|
|
@ -48,35 +46,6 @@ public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
public void handleMessage(Message msg) { |
|
|
|
|
super.handleMessage(msg); |
|
|
|
|
switch (msg.what) { |
|
|
|
|
case MSG_CONTINUE: |
|
|
|
|
String audioFile = PATH + File.separator + "tiger.mp3"; |
|
|
|
|
String muxFile = PATH + File.separator + "media-mux.mp4"; |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
MediaPlayer mediaPlayer = new MediaPlayer(); |
|
|
|
|
mediaPlayer.setDataSource(videoFile); |
|
|
|
|
mediaPlayer.prepare(); |
|
|
|
|
//ms
|
|
|
|
|
int videoDuration = mediaPlayer.getDuration() / 1000; |
|
|
|
|
Log.i(TAG, "videoDuration=" + videoDuration); |
|
|
|
|
mediaPlayer.release(); |
|
|
|
|
MediaMetadataRetriever mediaRetriever = new MediaMetadataRetriever(); |
|
|
|
|
mediaRetriever.setDataSource(audioFile); |
|
|
|
|
String duration = mediaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); |
|
|
|
|
int audioDuration = (int) (Long.parseLong(duration) / 1000); |
|
|
|
|
Log.i(TAG, "audioDuration=" + audioDuration); |
|
|
|
|
mediaRetriever.release(); |
|
|
|
|
int mDuration = Math.min(audioDuration, videoDuration); |
|
|
|
|
//mux video and audio
|
|
|
|
|
String[] commandLine = FFmpegUtil.mediaMux(temp, audioFile, mDuration, muxFile); |
|
|
|
|
if (ffmpegHandler != null) { |
|
|
|
|
ffmpegHandler.isContinue(false); |
|
|
|
|
ffmpegHandler.executeFFmpegCmd(commandLine); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case MSG_BEGIN: |
|
|
|
|
layoutProgress.setVisibility(View.VISIBLE); |
|
|
|
|
layoutMediaHandle.setVisibility(View.GONE); |
|
|
|
@ -121,7 +90,8 @@ public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
initViewsWithClick( |
|
|
|
|
R.id.btn_mux, |
|
|
|
|
R.id.btn_extract_audio, |
|
|
|
|
R.id.btn_extract_video |
|
|
|
|
R.id.btn_extract_video, |
|
|
|
|
R.id.btn_dubbing |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -152,17 +122,9 @@ public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
switch (viewId) { |
|
|
|
|
case R.id.btn_mux://mux
|
|
|
|
|
try { |
|
|
|
|
videoFile = srcFile; |
|
|
|
|
commandLine = FFmpegUtil.extractVideo(srcFile, temp); |
|
|
|
|
if (ffmpegHandler != null) { |
|
|
|
|
ffmpegHandler.isContinue(true); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
case R.id.btn_mux://mux:pure video and pure audio
|
|
|
|
|
ThreadPoolUtil.executeSingleThreadPool(() -> mediaMux(srcFile)); |
|
|
|
|
return; |
|
|
|
|
case R.id.btn_extract_audio://extract audio
|
|
|
|
|
String extractAudio = PATH + File.separator + "extractAudio.aac"; |
|
|
|
|
commandLine = FFmpegUtil.extractAudio(srcFile, extractAudio); |
|
|
|
@ -171,6 +133,9 @@ public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
String extractVideo = PATH + File.separator + "extractVideo.mp4"; |
|
|
|
|
commandLine = FFmpegUtil.extractVideo(srcFile, extractVideo); |
|
|
|
|
break; |
|
|
|
|
case R.id.btn_dubbing://dubbing
|
|
|
|
|
ThreadPoolUtil.executeSingleThreadPool(() -> mediaDubbing(srcFile)); |
|
|
|
|
return; |
|
|
|
|
default: |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
@ -179,6 +144,41 @@ public class MediaHandleActivity extends BaseActivity { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void muxVideoAndAudio(String videoPath, String outputPath) { |
|
|
|
|
String[] commandLine = FFmpegUtil.mediaMux(videoPath, audioFile, true, outputPath); |
|
|
|
|
int result = FFmpegCmd.executeSync(commandLine); |
|
|
|
|
if (result != 0) { |
|
|
|
|
commandLine = FFmpegUtil.mediaMux(videoPath, audioFile, false, outputPath); |
|
|
|
|
result = FFmpegCmd.executeSync(commandLine); |
|
|
|
|
Log.e(TAG, "mux audio and video result=" + result); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void mediaMux(String srcFile) { |
|
|
|
|
mHandler.sendEmptyMessage(MSG_BEGIN); |
|
|
|
|
String suffix = FileUtil.getFileSuffix(srcFile); |
|
|
|
|
String muxPath = PATH + File.separator + "mux" + suffix; |
|
|
|
|
Log.e(TAG, "muxPath=" + muxPath); |
|
|
|
|
muxVideoAndAudio(srcFile, muxPath); |
|
|
|
|
mHandler.sendEmptyMessage(MSG_FINISH); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void mediaDubbing(String srcFile) { |
|
|
|
|
mHandler.sendEmptyMessage(MSG_BEGIN); |
|
|
|
|
String dubbingSuffix = FileUtil.getFileSuffix(srcFile); |
|
|
|
|
String dubbingPath = PATH + File.separator + "dubbing" + dubbingSuffix; |
|
|
|
|
String temp = PATH + File.separator + "temp" + dubbingSuffix; |
|
|
|
|
String[] commandLine1 = FFmpegUtil.extractVideo(srcFile, temp); |
|
|
|
|
int dubbingResult = FFmpegCmd.executeSync(commandLine1); |
|
|
|
|
if (dubbingResult != 0) { |
|
|
|
|
Log.e(TAG, "extract video fail, result=" + dubbingResult); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
muxVideoAndAudio(temp, dubbingPath); |
|
|
|
|
FileUtil.deleteFile(temp); |
|
|
|
|
mHandler.sendEmptyMessage(MSG_FINISH); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void onDestroy() { |
|
|
|
|
super.onDestroy(); |
|
|
|
|