Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
microshow committed Jun 26, 2019
1 parent 8d16855 commit 769ed6b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ dependencies {

implementation project(path: ':rxffmpeg')

// implementation 'com.github.microshow:RxFFmpeg:1.2.3'
// implementation 'com.github.microshow:RxFFmpeg:1.2.4'
}
16 changes: 6 additions & 10 deletions app/src/main/java/io/microshow/rxffmpegdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,23 @@

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.microshow.rxffmpeg.RxFFmpegCommandSupport;
import io.microshow.rxffmpeg.RxFFmpegInvoke;
import io.microshow.rxffmpeg.RxFFmpegSubscriber;
import io.microshow.rxffmpegdemo.databinding.ActivityMainBinding;
import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = MainActivity.class.getSimpleName();

// String path = Environment.getExternalStorageDirectory().getPath();

//需要申请的权限,必须先在AndroidManifest.xml有声明,才可以动态获取权限
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};


private ActivityMainBinding binding;

private ProgressDialog mProgressDialog;
private ProgressDialog mProgressDialog;

//权限
private RxPermissions rxPermissions = null;
Expand All @@ -49,7 +45,7 @@ public void accept(Boolean aBoolean) throws Exception {
if (aBoolean) {// 用户同意了权限
runFFmpegRxJava();
} else {//用户拒绝了权限
Toast.makeText(MainActivity.this,"您拒绝了权限,请往设置里开启权限",Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "您拒绝了权限,请往设置里开启权限", Toast.LENGTH_LONG).show();
}
}
});
Expand All @@ -70,7 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {
/**
* rxjava方式调用
*/
private void runFFmpegRxJava () {
private void runFFmpegRxJava() {
openProgressDialog();

final String text = binding.editText.getText().toString();
Expand Down Expand Up @@ -112,9 +108,9 @@ public void openProgressDialog() {
mProgressDialog = Utils.openProgressDialog(this);
}

private void showDialog (String message) {
private void showDialog(String message) {
endTime = System.nanoTime();
Utils.showDialog(this, message, Utils.convertUsToTime((endTime-startTime)/1000, false));
Utils.showDialog(this, message, Utils.convertUsToTime((endTime - startTime) / 1000, false));
}

}
7 changes: 4 additions & 3 deletions app/src/main/java/io/microshow/rxffmpegdemo/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class Utils {

/**
* 微秒转换为 时分秒毫秒,如 00:00:00.000
* @param us 微秒
*
* @param us 微秒
* @param autoEllipsis true:如果小时为0,则这样显示00:00.000; false:全部显示 00:00:00.000
* @return
*/
Expand Down Expand Up @@ -53,15 +54,15 @@ public static String convertUsToTime(long us, boolean autoEllipsis) {
if (hour > 0) {
mUsDurationText.append(strHour).append(":");
}
}else {
} else {
mUsDurationText.append(strHour).append(":");
}
mUsDurationText.append(strMinute).append(":")
.append(strSecond).append(".").append(strMilliSecond);
return mUsDurationText.toString();
}

public static void showDialog (Context context, String message, String runTime) {
public static void showDialog(Context context, String message, String runTime) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage(message + "\n\n耗时时间:" + runTime);
Expand Down
4 changes: 2 additions & 2 deletions configs.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ ext {
//支持包版本
supportLibValue = "28.0.0"
//应用 versionCode
versionCodeValue = 9
versionCodeValue = 10
//应用 version 名称,更多设置里显示的
versionNameValue = "1.2.4"
versionNameValue = "1.2.5"

}
25 changes: 17 additions & 8 deletions rxffmpeg/src/main/java/io/microshow/rxffmpeg/AudioVideoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class AudioVideoUtils {

/**
* 根据视频的宽X高计算出一个合适的码率
*
* @param wxh 宽X高的值
* @return
*/
Expand All @@ -25,7 +26,7 @@ public static int getFitBitRate(int wxh) {
} else if (wxh <= 960 * 544) {
return 2 * 1024 * 1024;
} else if (wxh <= 1280 * 720) {
return (int)(2.5 * 1024 * 1024);
return (int) (2.5 * 1024 * 1024);
} else if (wxh <= 1920 * 1088) {
return 3 * 1024 * 1024;
} else {
Expand All @@ -35,6 +36,7 @@ public static int getFitBitRate(int wxh) {

/**
* 查找视频轨道
*
* @param extractor
* @return
*/
Expand All @@ -53,6 +55,7 @@ public static int selectVideoTrack(MediaExtractor extractor) {

/**
* 查找音频轨道
*
* @param extractor
* @return
*/
Expand All @@ -71,6 +74,7 @@ public static int selectAudioTrack(MediaExtractor extractor) {

/**
* 获取视频信息 时长 微秒 us
*
* @param url
* @return
*/
Expand All @@ -90,10 +94,11 @@ public static long getDuration(String url) {

/**
* 获取视频宽
*
* @param videoPath
* @return
*/
public static int getVideoWidth (String videoPath) {
public static int getVideoWidth(String videoPath) {
MediaMetadataRetriever retr = new MediaMetadataRetriever();
retr.setDataSource(videoPath);
String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); // 视频宽度
Expand All @@ -108,10 +113,11 @@ public static int getVideoWidth (String videoPath) {

/**
* 获取视频高
*
* @param videoPath
* @return
*/
public static int getVideoHeight (String videoPath) {
public static int getVideoHeight(String videoPath) {
MediaMetadataRetriever retr = new MediaMetadataRetriever();
retr.setDataSource(videoPath);
String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH); // 视频宽度
Expand All @@ -126,10 +132,11 @@ public static int getVideoHeight (String videoPath) {

/**
* 获取视频旋转方向
*
* @param videoPath
* @return
*/
public static int getVideoRotation (String videoPath) {
public static int getVideoRotation(String videoPath) {
MediaMetadataRetriever retr = new MediaMetadataRetriever();
retr.setDataSource(videoPath);
String rotation = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION); // 视频旋转方向
Expand All @@ -139,24 +146,26 @@ public static int getVideoRotation (String videoPath) {

/**
* 获取视频时长 单位:秒
*
* @param videoPath
* @return
*/
public static int getVideoDuration (String videoPath) {
public static int getVideoDuration(String videoPath) {
MediaMetadataRetriever retr = new MediaMetadataRetriever();
retr.setDataSource(videoPath);
String rotation = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); // 视频时长 毫秒
retr.release();
return Integer.parseInt(rotation)/1000;//转为秒
return Integer.parseInt(rotation) / 1000;//转为秒
}

/**
* 判断是否是横屏的视频
*
* @param videoPath
* @return
*/
public static boolean isHorizontalVideo (String videoPath) {
if (getVideoWidth (videoPath) >= getVideoHeight (videoPath)) {
public static boolean isHorizontalVideo(String videoPath) {
if (getVideoWidth(videoPath) >= getVideoHeight(videoPath)) {
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class RxFFmpegCommandList extends ArrayList<String> {

public RxFFmpegCommandList () {
public RxFFmpegCommandList() {
super();
this.add("ffmpeg");
this.add("-y");
Expand All @@ -17,7 +17,7 @@ public RxFFmpegCommandList () {
/**
* 清除命令集合
*/
public void clearCommands () {
public void clearCommands() {
this.clear();
}

Expand All @@ -26,7 +26,7 @@ public RxFFmpegCommandList append(String s) {
return this;
}

public String[] build () {
public String[] build() {
String[] command = new String[this.size()];
for (int i = 0; i < this.size(); i++) {
command[i] = this.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public class RxFFmpegCommandSupport {

public static String[] getBoxblur () {
public static String[] getBoxblur() {
RxFFmpegCommandList cmdlist = new RxFFmpegCommandList();
cmdlist.append("-i");
cmdlist.append("/storage/emulated/0/1/input.mp4");
Expand Down
Loading

0 comments on commit 769ed6b

Please sign in to comment.