How to compress user-generated content videos on a client device using FFmpeg?

Hey Gumlet. I’m building a TikTok clone on Android. Is is possible to reduce the overall size of the videos to cut down the storage costs? I’d like to retain the video quality. Would FFmpeg be the right choice? Are there any other alternatives?

- welcome to the official Gumlet community. I’ll request one of our technical team members to help you with FFmpeg. However, you could simply use Gumlet’s service which is optimized to do this.

In fact, Gumlet will do much more to optimize your streams for the best video quality for variety of networks and devices. Give it a try and let me know if you face issues.

I think video compression is influenced by a variety of factors, the most essential of which are bitrate, codec, and rate-control approaches.
I think you won’t receive much compression if, you choose a very high target bitrate, and your video quality will suffer if you choose a very low target bitrate. On client devices, optimizing the intended bitrate for each video is a time-consuming and resource-intensive job.

I hope this helps.

Agree with you but, I believe, this brings us back to the video codec section. Choosing a different codec for the target video would require a lot of study of the codec’s many properties. Like H.264 and H.265, video codecs are extremely sophisticated algorithms, with each parameter affecting the final video quality and size.

Recently I went over the documentation and found that FFmpeg contains a helpful option called “-preset” that has a separate set of values for each video codec, with each value corresponding to the codec-specific parameters and their values.

The “-preset” option adjusts compression quality and quality-size trade-off. The FFmpeg Documentation contains default values and associated properties for each codec.

You can read it once and I hope your query will get resolved.

I think the Rate-control methods will help you. Basically, Rate-control methods allow us to control visual quality-size trade-off. There are a few rate control methods, you can read about them in detail here.
Suppose, that we are choosing H.264 (libx264) video codec as our target video codec, we still haven’t figured out how we going to choose bitrate for a particular video.

For this use case, we are going forward with the Constant Quality (CQ) / Constant Rate Factor (CRF) rate control method.

However, FFmpeg allows us to compress video with the CRF rate control method using -crf. In H.264 and H.265, the value of -crf ranges from 0 to 51 (like the QP). Whereas 23 is a good default for x264, and 28 is the default for x265. 18 (or 24 for x265) should be visually very good and anything lower will just increase file size. You can find the compression of a video using FFmpeg with the CRF rate control method below.
With CRF/CQ method you don’t have to worry about setting a target bitrate for each video.
You just need to play a bit with the -preset and -crf parameters to achieve optimal compression speed, compression rate, and visual quality of compressed video for your user-generated content.

Thanks for your help.