How to compress a video into a specific size in kotlin

302 views Asked by At

i am implementing an android app in kotlin that records a 15 seconds video using camera2 API and i want to compress it into a specific size . How can i figure it out ?

1

There are 1 answers

0
Orcun On

This can be done using a library like ffmpeg. An ffmpeg command like below would reduce the size significantly:

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

where input.mp4 is your source video file and output.mp4 would be the target. It re-encodes your video file with libx265 to have H.265 format. You can read more about it here: https://unix.stackexchange.com/questions/28803/how-can-i-reduce-a-videos-size-with-ffmpeg

Of course, to be able to use ffmpeg in android, you need to add it to your project. The most complete implementation I have seen so far is:

https://github.com/arthenica/ffmpeg-kit/tree/main/android

With the help of that library, you can execute aforementioned ffmpeg command.