zengjiebin лет назад: 7
Родитель
Сommit
d590c56aa3

+ 1 - 0
app/build.gradle

@@ -427,6 +427,7 @@ dependencies {
     //fast json
     implementation 'com.alibaba:fastjson:1.2.52'
     implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'
+    implementation 'com.github.yangjie10930:EpMedia:v0.9.5'
 }
 
 static def releaseTime() {

+ 93 - 13
app/src/main/java/com/sheep/gamegroup/view/activity/ActPublishArticle.java

@@ -29,6 +29,7 @@ import com.sheep.gamegroup.util.Jump2View;
 import com.sheep.gamegroup.util.ListUtil;
 import com.sheep.gamegroup.util.LogUtil;
 import com.sheep.gamegroup.util.QiNiuUploadUtil;
+import com.sheep.gamegroup.util.TestUtil;
 import com.sheep.gamegroup.util.ViewUtil;
 import com.sheep.gamegroup.view.dialog.DialogProgress;
 import com.sheep.gamegroup.view.fragment.FgtArticleVideo;
@@ -44,7 +45,12 @@ import org.json.JSONObject;
 import java.io.File;
 import java.util.Locale;
 
+import VideoHandle.EpEditor;
+import VideoHandle.EpVideo;
+import VideoHandle.OnEditorListener;
 import butterknife.BindView;
+import io.reactivex.Observable;
+import io.reactivex.ObservableOnSubscribe;
 import io.reactivex.android.schedulers.AndroidSchedulers;
 import io.reactivex.schedulers.Schedulers;
 
@@ -157,7 +163,7 @@ public class ActPublishArticle extends BaseActivity {
         switch (uploadType) {
             case UPLOAD_TYPE_NONE:
                 G.showToast("开始上传视频");
-                uploadFile();
+                compressFile();
                 break;
             case UPLOAD_TYPE_START:
                 G.showToast("正在上传视频");
@@ -166,7 +172,7 @@ public class ActPublishArticle extends BaseActivity {
                 break;
             case UPLOAD_TYPE_FAIL:
                 G.showToast("重新上传视频");
-                uploadFile();
+                compressFile();
                 break;
         }
     }
@@ -176,8 +182,81 @@ public class ActPublishArticle extends BaseActivity {
         return video == null || video.getFilePath() == null || !new File(video.getFilePath()).exists();
     }
 
-    private void uploadFile() {
-        final DialogProgress dialogProgress = DialogProgress.showDialog(this);
+    private static final String COMPRESS_FILE_END = "_compress.mp4";
+    private void compressFile() {
+        final DialogProgress dialogProgress = DialogProgress.showDialog(this, false);
+        if(video.getFilePath().endsWith(COMPRESS_FILE_END)){
+            uploadFile(dialogProgress);
+            return;
+        }
+        ViewUtil.setText(dialogProgress.getTextView(), "压缩视频中");
+        Observable.create((ObservableOnSubscribe<Float>) emitter -> {
+            String videoPath = video.getFilePath();
+            String outVideoPath = videoPath + COMPRESS_FILE_END;
+            long time = System.currentTimeMillis();
+            OnEditorListener onEditorListener = new OnEditorListener() {
+                private float progress;
+                @Override
+                public void onSuccess() {
+                    long curTime = System.currentTimeMillis();
+                    LogUtil.println("goActCutVideo", time, curTime, (curTime-time) / 1000);
+                    switch (video.getOrientation()) {
+                        case 90:
+                        case 270:
+                            int a = video.getHeight();
+                            video.setHeight(video.getWidth());
+                            video.setWidth(a);
+                            break;
+                    }
+                    if(video.getOrientation() > 0) {
+                        video.setOrientation(0);
+                    }
+                    emitter.onComplete();
+                }
+
+                @Override
+                public void onFailure() {
+                    emitter.onComplete();
+                }
+
+                @Override
+                public void onProgress(float progress) {
+                    LogUtil.println("goActCutVideo", progress);
+                    //这里获取处理进度
+                    if(this.progress == 0 || progress - this.progress > 0.05 || this.progress == 1)
+                        emitter.onNext(Math.max(progress, 1));
+                    this.progress = progress;
+                }
+            };
+//            String cmd = String.format(Locale.CHINA, "-threads 6 -i %s -b 500k %s", videoPath, outVideoPath);
+//            EpEditor.execCmd(cmd, 0, onEditorListener);
+            EpVideo epVideo = new EpVideo(videoPath);
+//输出选项,参数为输出文件路径(目前仅支持mp4格式输出)
+            EpEditor.OutputOption outputOption = new EpEditor.OutputOption(outVideoPath);
+//                    outputOption.setWidth(data.getWidth());//输出视频宽,如果不设置则为原始视频宽高
+//                    outputOption.setHeight(data.getHeight());//输出视频高度
+            outputOption.frameRate = 10;//输出视频帧率,默认30
+            outputOption.bitRate = 1;//输出视频码率,默认10
+//            if(video.getOrientation() > 0) {
+//                epVideo.rotation(360 - video.getOrientation(), false);
+//            }
+            EpEditor.exec(epVideo, outputOption, onEditorListener);
+        }).subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe(new AbsObserver<Float>() {
+                    @Override
+                    public void onNext(Float percent) {
+                        ViewUtil.setText(dialogProgress.getTextView(), String.format(Locale.CHINA, "%d%%", (int) (percent * 100)));
+                    }
+
+                    @Override
+                    public void onComplete() {
+                        uploadFile(dialogProgress);
+                    }
+                });
+    }
+
+    private void uploadFile(DialogProgress dialogProgress) {
         uploadType = UPLOAD_TYPE_START;
         QiNiuUploadUtil.getInstance().uploadFile(video.getFilePath()/*, QiNiuUploadUtil.TAG_VIDEO*/, (key, info, response) -> {
             LogUtil.println("qiniu", key, info, response);
@@ -200,15 +279,16 @@ public class ActPublishArticle extends BaseActivity {
                 jsonObject.put("resource", url);
                 int a = video.getWidth();
                 int b = video.getHeight();
-                switch (video.getOrientation()) {
-                    case 90:
-                    case 270:
-                        a = video.getHeight();
-                        b = video.getWidth();
-                        break;
-                }
-                int rotate = 360 - Math.max(0, video.getOrientation());
-                jsonObject.put("cover", String.format(Locale.CHINA, "%s?vframe/jpg/offset/0/rotate/%d&w=%d&h=%d", url, rotate, a, b));
+                jsonObject.put("cover", String.format(Locale.CHINA, "%s?vframe/jpg/offset/0&w=%d&h=%d", url, a, b));
+//                switch (video.getOrientation()) {
+//                    case 90:
+//                    case 270:
+//                        a = video.getHeight();
+//                        b = video.getWidth();
+//                        break;
+//                }
+//                int rotate = 360 - Math.max(0, video.getOrientation());
+//                jsonObject.put("cover", String.format(Locale.CHINA, "%s?vframe/jpg/offset/0/rotate/%d&w=%d&h=%d", url, rotate, a, b));
                 publishArticle(dialogProgress);
             } else {
                 ViewUtil.setText(dialogProgress.getTextView(), "上传失败");