Sample code of Android local video compression scheme
preface
This article does not discuss short video recording similar to second shooting, but users select an existing local video and upload it after compression. In fact, the realization of second shooting is to customize the video recording function, so as to control the recording time, resolution, bit rate, etc., generate a small video and upload it. We can't control the parameters of the original video. It may be that a large video needs compression.
thinking
The video is transcoded by ffmpeg, and the video with smaller resolution and bit rate is generated by setting parameters to realize compression. Of course, the function of ffmpeg is far more than that, which is a big topic.
Open source libraries used: https://github.com/WritingMinds/ffmpeg-android-java
usage method
Basic principle: the executable file ffmpeg in Android environment is stored locally, and the code executes the compression command of ffmpeg.
This method is executed asynchronously, so it is best to execute it in the application. Method has a callback whether the execution is successful or not. I pass null here and don't care about the result. After execution, look at the directory in the mobile phone:
Since it is an executable file, it can certainly be executed in the Android shell environment. The ADB shell enters the mobile phone and has a look (provided that the mobile phone has obtained root permission):
Execute a command of ffmpeg: for example, view the current version of ffmpeg:. / ffmpeg - version
Then you can use the commands of ffmpeg in the code: write the command to String[], then call fFmpeg.execute.
Get information about video files
Compressed video:
Parameter interpretation:
Problem solving
This open source library is used for video compression. There are many problems in actual development, which are solved one by one below
1. Compression progress feedback
After the transcoding command is executed, onprogress just keeps outputting strings, and the text is very long. Regular expressions are needed to intercept the transcoding progress feedback:
2. Low bit rate video compression will become larger
In practice, it is found that some videos with poor original quality become larger after compression.
Processing method: execute the command to extract video information before compression. Videos less than 1024KB / s will not be compressed:
After successful compression, check the compressed file and the size of the original file. If it becomes larger, use the original file directly.
3. Multi thread compression of multiple videos
The command to execute ffmpeg in the open source library is executed in asyctask:
The execute method is a serial method after API 11, which means that the open source library has been limited to single thread.
Change to: ffmpexecuteasynctask. Executeonexecutor (executors. Newcachedthreadpool()); You can use multithreading
During the test, it is found that multiple videos are compressed at the same time, and the mobile phone will be seriously heated. It is strongly recommended to use the original design.
4. Compression speed and quality
The performance of the mobile phone is limited, and the speed of compressed video is not ideal. Even if the transcoded video is compressed by the format factory on the PC side, it is not very fast.
The compression quality is OK and can basically maintain the same definition as the original video. Here are the test data:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.