SoFunction
Updated on 2025-04-10

Sample code for Android video compression

Recently, due to project requirements, I need to compress and send the video I recorded myself. This article mainly talks about video compression, so we won’t make too much statement about the recording, uploading and sending of videos here.

background:

I have never done a video project before, so when I first accepted this project, I was a little overwhelmed. Of course, facing difficulties is a necessary quality for us to attack the city lion. So I searched for knowledge about video encoding and decoding online, and the one I found the most was ffmpeg. So what is ffmpeg? Simply put, FFmpeg is an open source computer program that can be used to record, convert digital audio, video, and convert them into streams. Use an LGPL or GPL license. It provides a complete solution for recording, converting and streaming audio and video. It contains a very advanced audio/video codec library, libavcodec. In order to ensure high portability and codec quality, many codes in libavcodec are developed from scratch.

It mainly includes: video collection, video editing, video screenshots, video watermarks, etc. If you want to know it carefully, you can use Baidu to go on your own.

Okay, I won’t talk about the following principles. I will only tell you how to use them. After all, I just know how to use them.

The download address will be attached below.

After downloading, just follow the directory I put.

cmd = "-y -i /storage/emulated/0/coollang/vedio/2.mp4 -strict -2 -vcodec libx264 -preset ultrafast -crf 24 -acodec aac -ar 44100 -ac 2 -b:a 96k -s 640x352 -aspect 16:9 /storage/emulated/0/coollang/vedio/1.mp4"; 
    com = new Compressor(this); 
 
    (new InitListener() { 
      @Override 
      public void onLoadSuccess() { 
        (cmd,new CompressListener() { 
          @Override 
          public void onExecSuccess(String message) { 
            ("success",message); 
          } 
 
          @Override 
          public void onExecFail(String reason) { 
            ("fail",reason); 
          } 
 
          @Override 
          public void onExecProgress(String message) { 
            ("progress",message); 
          } 
        }); 
      } 
 
      @Override 
      public void onLoadFail(String reason) { 
        ("fail",reason); 
      } 
    }); 

What you need to pay attention to in this demo is to store the files in the assets, jin, libs folders in the corresponding directory.

The following command means:

Copy the codeThe code is as follows:

"-y -i directory to be compressed -strict -2 -vcodec libx264 -preset ultrafast -crf 24 -acodec aac -ar 44100 -ac 2 -b:a 96k -s 640x352 -aspect 16:9 compressed directory";

InitListener is a listening to the compression process. OnExecSuccess means the compression is successful, and onExecFail means the compression is failed. Generally, the failure is because your own address errors, so be careful. onExecProgress compression progress.

It's enough to know so much about this tool, the key is to try it yourself.

Download address:Android-Video-Compressor_jb51.rar

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.