2

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.

mediaRecorder.setVideoSize(480, 480);

but it works if i set the resolution supported by camera.

Camera.Parameters p = c.getParameters();
        List<Camera.Size> list = p.getSupportedPreviewSizes();
        //They are ordered from largest to smallest, so the largest will be...
        for (Camera.Size i : list) {
            if (i.width <= 640 && i.width >= 480) {
                size = i;
            }
            Log.d(TAG, "i.height: " + i.height + " i.width" + i.width);
        }
        if (size != null) {
            p.setPreviewSize(size.width, size.height);
            c.setParameters(p);
        }

then set

 mediaRecorder.setVideoSize(size.width, size.height);

I usually get 480 by 640. Is there a way to crop the video on android? I have seen ffmpeg library but i need to use this in project and i am not sure how LGPL works. Is there a simpler solution to this?

ddog
  • 670
  • 1
  • 10
  • 25
  • 1
    "it works if i set the resolution supported by camera" -- only by luck. Use `getSupportedVideoSizes()` to find the sizes supported for video recording, not `getSupportedPreviewSizes()`. "How does Instagram crop video in android app?" -- they might do it how Vine does, which is to not use `MediaRecorder`, but instead capture preview frames and stitch them together to create a movie (in Instagram's case, they'd crop the frames to be square as part of the stitching work). Neither this nor the `ffmpeg` solution will be especially easy to implement. – CommonsWare Aug 13 '14 at 12:29
  • tnx for the hint, do you have any usefull links how this image stiching is done? getSupportedVideoSizes() requests api level 11 and i am working with level 10 that is why i used getSupportedPreviewSizes() – ddog Aug 13 '14 at 13:15
  • "do you have any usefull links how this image stiching is done?" -- no, sorry. – CommonsWare Aug 13 '14 at 14:53
  • @CommonsWare how ffmpeg to implement in Android SDK ? – mob_web_dev Feb 22 '17 at 10:45

3 Answers3

3

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

asozcan
  • 1,370
  • 1
  • 17
  • 24
0
  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.
Chris Tsang
  • 181
  • 12
  • by adding a black cover top and bottom, I still see the video captured for the view above the square camera, any thoughts? – Babs Jun 11 '17 at 18:29
0

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

Chris Tsang
  • 181
  • 12