From aa7a6cd17eff0014ae05722ce82657bcf42caac0 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Wed, 8 Jan 2020 00:12:32 +0800 Subject: [PATCH] use ffprobe to parse media format use ffprobe to parse media format --- app/src/main/AndroidManifest.xml | 4 +- .../frank/ffmpeg/activity/MainActivity.java | 6 +- .../ffmpeg/activity/ProbeFormatActivity.java | 107 ++++++++++++++++++ .../frank/ffmpeg/handler/FFmpegHandler.java | 2 +- .../com/frank/ffmpeg/util/FFmpegUtil.java | 16 ++- app/src/main/res/layout/activity_main.xml | 7 ++ app/src/main/res/layout/activity_probe.xml | 35 ++++++ app/src/main/res/values/strings.xml | 1 + 8 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.java create mode 100644 app/src/main/res/layout/activity_probe.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5b15bca..8035830 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -51,7 +51,9 @@ - + + \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java index 00cbb46..821c0a2 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java @@ -29,7 +29,8 @@ public class MainActivity extends BaseActivity { R.id.btn_push, R.id.btn_live, R.id.btn_filter, - R.id.btn_preview + R.id.btn_preview, + R.id.btn_probe ); } @@ -61,6 +62,9 @@ public class MainActivity extends BaseActivity { case R.id.btn_preview://视频拖动实时预览 intent.setClass(MainActivity.this, VideoPreviewActivity.class); break; + case R.id.btn_probe://解析音视频多媒体格式 + intent.setClass(MainActivity.this, ProbeFormatActivity.class); + break; default: break; } diff --git a/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.java new file mode 100644 index 0000000..9349493 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.java @@ -0,0 +1,107 @@ +package com.frank.ffmpeg.activity; + +import android.annotation.SuppressLint; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.View; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; +import android.widget.TextView; + +import com.frank.ffmpeg.R; +import com.frank.ffmpeg.handler.FFmpegHandler; + +import com.frank.ffmpeg.util.FFmpegUtil; +import com.frank.ffmpeg.util.FileUtil; + +import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN; +import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH; + +/** + * Using ffprobe to parse media format data + * Created by frank on 2020/1/7. + */ + +public class ProbeFormatActivity extends BaseActivity { + + private TextView txtProbeFormat; + private ProgressBar progressProbe; + private RelativeLayout layoutProbe; + private FFmpegHandler ffmpegHandler; + + @SuppressLint("HandlerLeak") + private Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + switch (msg.what) { + case MSG_BEGIN: + progressProbe.setVisibility(View.VISIBLE); + layoutProbe.setVisibility(View.GONE); + break; + case MSG_FINISH: + progressProbe.setVisibility(View.GONE); + layoutProbe.setVisibility(View.VISIBLE); + break; + default: + break; + } + } + }; + + @Override + int getLayoutId() { + return R.layout.activity_probe; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + hideActionBar(); + initView(); + ffmpegHandler = new FFmpegHandler(mHandler); + } + + private void initView() { + progressProbe = getView(R.id.progress_probe); + layoutProbe = getView(R.id.layout_probe); + initViewsWithClick(R.id.btn_probe_format); + txtProbeFormat = getView(R.id.txt_probe_format); + } + + @Override + public void onViewClick(View view) { + selectFile(); + } + + @Override + void onSelectedFile(String filePath) { + doHandleAudio(filePath); + } + + /** + * use ffprobe to parse video/audio format metadata + * + * @param srcFile srcFile + */ + private void doHandleAudio(final String srcFile) { + if (!FileUtil.checkFileExist(srcFile)) { + return; + } + String[] commandLine = FFmpegUtil.probeFormat(srcFile); + if (ffmpegHandler != null) { + ffmpegHandler.executeFFprobeCmd(commandLine); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mHandler != null) { + mHandler.removeCallbacksAndMessages(null); + } + } + +} diff --git a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java index f641d94..38ba020 100644 --- a/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java +++ b/app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java @@ -78,7 +78,7 @@ public class FFmpegHandler { @Override public void onEnd(int resultCode, String resultMsg) { - Log.i(TAG, "handle ffprobe onEnd result=" + resultMsg); + Log.e(TAG, "handle ffprobe onEnd result=" + resultMsg); if(isContinue) { mHandler.obtainMessage(MSG_CONTINUE).sendToTarget(); }else { diff --git a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java index 0abb14c..bf06b86 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -358,8 +358,22 @@ public class FFmpegUtil { */ public static String[] moveMoovAhead(String inputFile, String outputFile) { String moovCmd = "ffmpeg -i %s -movflags faststart -acodec copy -vcodec copy %s"; - moovCmd = String.format(Locale.CHINESE, moovCmd, inputFile, outputFile); + moovCmd = String.format(Locale.getDefault(), moovCmd, inputFile, outputFile); return moovCmd.split(" "); } + /** + * 使用ffprobe探测多媒体格式 + * + * @param inputFile inputFile + * @return 多媒体格式数据 + */ + public static String[] probeFormat(String inputFile) { + //show format:ffprobe -i %s -show_format -print_format json + //show stream:ffprobe -i %s -show_streams + String ffprobeCmd = "ffprobe -i %s -show_streams -show_format -print_format json"; + ffprobeCmd = String.format(Locale.getDefault(), ffprobeCmd, inputFile); + return ffprobeCmd.split(" "); + } + } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5845e0d..bf0852d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -69,6 +69,13 @@ android:text="@string/video_preview" android:layout_marginTop="10dp" /> +