Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

java - Crop a Video file with diverse Aspect ratio

I'm trying to give my users the possibility to crop their video in diverse aspect ratio before uploading them on my fireplace database. I'm using an Android-VideoCropView for display the video editor layout for my users and SimpleVideoEditor to crop the video.However, I'm having a hard time to merge both library together when I'm trying to get the info from the VideoCropView and then crop the file with the SimpleVideoEditor this is the result I should have and this is the result I got. So my question is what I'm doing bad and how can I fix that?

Here is my Code

     Uri fileUri = Uri.fromFile(new File(stringvideofile));
                                        String destPath = getVideoFilePath();


                                        //1.
                                        float baseWidthPx =  mVideoCropView.getVideoWidth();    // should be +// todo: match every views one side , then set that side. need an if-else
                                        float  baseHeightPx = mVideoCropView.getVideoHeight() ;
                                        // 2. get the center of crop quad
                                        float  cropQuadCenterX, cropQuadCenterY;
                                        cropQuadCenterX = mVideoCropView.getRealPositionX() + baseWidthPx/2;
                                        cropQuadCenterY = mVideoCropView.getRealPositionY() + baseHeightPx/2;

                                        // 3. get video frame quad center
                                        float  videoQuadCenterX, videoQuadCenterY;
                                        videoQuadCenterX =  mVideoCropView.getWidth()/2;
                                        videoQuadCenterY = mVideoCropView.getHeight()/2;

                                        float dx = videoQuadCenterX - cropQuadCenterX;
                                        float dy = videoQuadCenterY - cropQuadCenterY;

                                        dx = 2*dx/baseWidthPx;  // idk why multiply with 2 :/ divide with baseSizePx to get ratio, that makes sense but why 2???
                                        dy = 2*dy/baseHeightPx;

                                        float translateX = dx, translateY = dy;

                                        FillModeCustomItem fillModeCustomItem = new FillModeCustomItem(
                                                mVideoCropView.getScaleX(),
                                                mVideoCropView.getRotate(),
                                                translateX,
                                                translateY,
                                                mVideoCropView.getVideoWidth(),
                                                mVideoCropView.getVideoHeight()
                                        );


                                       GlFilterGroup glFilterGroup;
                                        if(composerGlFilter == null) {
                                         composerGlFilter = new GlFilter();
                                        }

                                        glFilterGroup  = new GlFilterGroup(new GlFilter()); // the default filter
                                        Log.e(TAG, "currentGlFilter is null");



                                        final Logger mylogger = new Logger() {
                                            @Override
                                            public void debug(String tag, String message) {
                                                Log.d(tag, message);
                                            }

                                            @Override
                                            public void error(String tag, String message, Throwable error) {
                                                Log.e(tag, "Message: "+message + ". Error: "+error.getLocalizedMessage());
                                            }

                                            @Override
                                            public void warning(String tag, String message) {
                                                Log.w(tag, message);
                                            }
                                        };


                                        mp4Composer = null;
                                        mp4Composer = new Mp4Composer(fileUri, destPath, CropVideoActivity.this, mylogger);
                                        mp4Composer
                                                .size(mVideoCropView.getWidth(), mVideoCropView.getHeight()) // fake it till you make it
                                                .fillMode(FillMode.CUSTOM)
                                                .customFillMode(fillModeCustomItem);



                                        if(composerGlFilter != null){
                                            mp4Composer.filter(composerGlFilter);
                                        }else{
                                            mp4Composer.filter(glFilterGroup);
                                        }

                                        mp4Composer.trim(mStartTimeMs,  mVideoCropView.getDuration())
                                                .listener(new Mp4Composer.Listener() {
                                                    @Override
                                                    public void onProgress(double progress) {
                                                        Log.d(TAG, "onProgress = " + progress);
                                                        new Thread()
                                                        {
                                                            public void run()
                                                            {
                                                                CropVideoActivity.this.runOnUiThread(new Runnable()
                                                                {
                                                                    public void run()
                                                                    {
                                                                        //Do your UI operations like dialog opening or Toast here
                                                                        Toast.makeText( CropVideoActivity.this, "onProgress = " + progress, Toast.LENGTH_SHORT).show();
                                                                    }
                                                                });
                                                            }
                                                        }.start();
                                                    }

                                                    @Override
                                                    public void onCompleted() {
                                                        Log.d(TAG, "onCompleted()");

                                                        new Thread()
                                                        {
                                                            public void run()
                                                            {
                                                                CropVideoActivity.this.runOnUiThread(new Runnable()
                                                                {
                                                                    public void run()
                                                                    {
                                                                        //Do your UI operations like dialog opening or Toast here
                                                                        Toast.makeText( CropVideoActivity.this, "codec complete path = " + destPath, Toast.LENGTH_SHORT).show();
                                                                    }
                                                                });
                                                            }
                                                        }.start();
                                                    }

                                                    @Override
                                                    public void onCanceled() {
                                                        Log.d(TAG, "onCanceled");

                                                        new Thread()
                                                        {
                                                            public void run()
                                                            {
                                                                CropVideoActivity.this.runOnUiThread(new Runnable()
                                                                {
                                                                    public void run()
                                                                    {
                                                                        //Do your UI operations like dialog opening or Toast here
                                                                        Toast.makeText( CropVideoActivity.this, "videoProcessing onCanceled", Toast.LENGTH_SHORT).show();
                                                                    }
                                                                });
                                                            }
                                                        }.start();
                                                    }

                                                    @Override
                                                    public void onFailed(Exception exception) {
                                                        Log.e(TAG, "onFailed()", exception);

                                                        new Thread()
                                                        {
                                                            public void run()
                                                            {
                                                                CropVideoActivity.this.runOnUiThread(new Runnable()
                                                                {
                                                                    public void run()
                                                                    {
                                                                        //Do your UI operations like dialog opening or Toast here
                                                                        Toast.makeText( CropVideoActivity.this, "exception.getMessage()  : "+exception.getMessage(), Toast.LENGTH_SHORT).show();

                                                                    }
                                                                });
                                                            }
                                                        }.start();

                                                

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...