Nikhil Mathew Nikhil Mathew - 1 year ago 126
Android Question

Capturing Video with 1:1 aspect ratio on Android

I am trying to develop an android app that would record a 15sec 640x640 video using MediaRecorder, extract all frames using ffmpeg, apply some artistic filters using gpuimage and then combine the frames back to a video using ffmpeg.

I'm facing problem in recording a video of resolution 640x640. (Instagram android app does this)

I tried using MediaRecorder's setVideoSize(640, 640) and the output video is 640x640 but the video looks like a 640x480 video stretched vertically to make it 640x640. I guess that's because 640x640 is not a resolution returned by the supported video capture resolution list of the device. Is there any way to tell media recorder to maintain aspect ratio while doing such scaling so that I get a video that was scaled from 640x480 to 640x640 by cropping width wise instead of stretching height wise ?

Answer Source

I finally ended up with capturing at 640x480, cropping it to 480x480 while splitting to frames using ffmpeg, then finally when frames are combined back to video using ffmpeg, it is up-scaled to 640x640.

String[] ffmpegCommand = {"/sdcard/frames/ffmpeg",
    "-i", "/sdcard/frames/test.3gp",
    "-vf", "crop=480:480:80:0,transpose=1",
    "-r", "30", "-an", "-qscale:v", "2", "-vsync", "1", "-threads", "4",  "/sdcard/frames/image%03d.jpg"};

The transpose=1 is required when the app is working in portrait mode.

Note : When capturing, to show only the part of the video that will be included in the final cropped video, I added two black bars of 80 pixels on top of the preview display to hide the 80 pixels on either side of the video that later gets cropped of. So the user gets what he sees on the preview.