use ffprobe to parse media format

use ffprobe to parse media format
pull/107/head
xufulong 5 years ago
parent db450ae573
commit aa7a6cd17e
  1. 4
      app/src/main/AndroidManifest.xml
  2. 6
      app/src/main/java/com/frank/ffmpeg/activity/MainActivity.java
  3. 107
      app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.java
  4. 2
      app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java
  5. 16
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java
  6. 7
      app/src/main/res/layout/activity_main.xml
  7. 35
      app/src/main/res/layout/activity_probe.xml
  8. 1
      app/src/main/res/values/strings.xml

@ -51,7 +51,9 @@
<!--视频播放拖动预览-->
<activity android:name=".activity.VideoPreviewActivity"
android:screenOrientation="portrait" />
<!--检测多媒体格式数据-->
<activity android:name=".activity.ProbeFormatActivity"
android:screenOrientation="portrait" />
</application>
</manifest>

@ -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;
}

@ -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);
}
}
}

@ -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 {

@ -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(" ");
}
}

@ -69,6 +69,13 @@
android:text="@string/video_preview"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/btn_probe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/media_probe"
android:layout_marginTop="10dp" />
</LinearLayout>
</ScrollView>

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_probe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_probe_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="@string/media_probe"
android:textColor="@color/colorPrimary"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/txt_probe_format"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/btn_probe_format"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<ProgressBar
android:id="@+id/progress_probe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>

@ -19,6 +19,7 @@
<string name="media_extra_audio">提取音频</string>
<string name="media_extract_video">提取视频</string>
<string name="media_play">音视频播放</string>
<string name="media_probe">解析多媒体格式</string>
<string name="video_cut">视频剪切</string>
<string name="video_concat">视频拼接</string>

Loading…
Cancel
Save