How does Instagram crop video in android app?

前端 未结 3 963
忘了有多久
忘了有多久 2021-01-21 15:20

I am trying to record the video in 1:1 ratio (square) and i cant get it to work, if i set custom resolution in media recorder instance i get an error on media recorder start.

相关标签:
3条回答
  • 2021-01-21 15:52

    Actually just adding this dependency:

    compile 'com.writingminds:FFmpegAndroid:0.3.2'
    

    then using below code makes the thing. Only problem is, it's not so fast. In my tests, 20sec length 720x480 video cropped to 480x in 6-7 second with a galaxy s6.

    FFmpeg ffmpeg = FFmpeg.getInstance(context);
    String[] complexCommand = {"-y", "-i", originalFilePath, "-vf", "crop=480:480:80:0", "-preset", "ultrafast", "-strict", "-2", "-c:v", "libx264", "-c:a", "copy", originalFilePath.replace(".mp4", "_crop.mp4")};
    try {
        ffmpeg.execute(complexCommand, new ExecuteBinaryResponseHandler() {
            @Override
            public void onStart() {super.onStart();}
    
            @Override
            public void onSuccess(String message) {super.onSuccess(message);}
    
            @Override
            public void onProgress(String message) {super.onProgress(message);}
    
            @Override
            public void onFailure(String message) {super.onFailure(message);}
    
            @Override
            public void onFinish() {super.onFinish();}
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        Log.e("ffmpeg", "ffmpeg already running");
    }
    

    There's another better solution using with new MediaCodec if your min sdk is api 18

    0 讨论(0)
  • 2021-01-21 15:54
    1. You can take a look at https://github.com/boxme/SquareCamera. It's used to crop a image, but the idea can be used to crop a video. The main idea is to add a black cover top and bottom(or left and right)to make it seemed like a square.
    2. FFmpeg can be used, of course, though it is a little complicated.
    0 讨论(0)
  • 2021-01-21 16:06

    Using OpenGL and shader can solve this problem. Or you can use libyuv to do it on CPU.

    0 讨论(0)
提交回复
热议问题