Five minutes JAVA code teaches you: FFmpeg realizes video trial
1. Business scenario
With the advent of the era of payment for knowledge, online learning platforms are now springing up, and companies of all sizes are developing online learning platforms. Therefore, the following needs will arise: course video demo function .
For the windows version, please click here:
Five minutes of code teaches you: JAVA realizes the video demo function (window version)
PS: (The code used in this blog, the ffmpeg installation package, and the cmd command line tool will be packaged and placed in the attachment below ↓↓↓↓ for your download and reference.)
2. Implementation ideas
After the platform user uploads his own course, the server intercepts a short segment of the course video for storage (for example, intercept the first 30 seconds of the video content as the trial content). Therefore, after the user uploads the video, two videos (1. original video 2. trial video) are generated after processing by the server. Ordinary users access the video to play the trial video, and paying users access the video to play the original video.
3. Introduction to FFmpeg open source framework
Here, Java is used to call FFmpeg to process video and audio, which is implemented by manually installing FFmpeg in the system, and then executing commands to call.
FFmpeg official website: http://ffmpeg.org/
(FFmpeg Readme: Converting video and audio has become so easy!)
4. Centos7 environment implementation
4.1 Download and upload Linux from official website
Download the compressed package ffmpeg-4.2.4.tar.bz2 of the Linux version at http://ffmpeg.org/download.html#releases, as shown below:
Use the tool to upload the downloaded FFmpeg compressed package to linux, here I use SecureCRT to upload:
4.2 Unzip and install
1. Enter the installation directory /usr/ffmpeg, as shown in the figure below:
unzip the compressed package, use tar -jxvf ffmpeg-4.2.4.tar.bz2, if an error is reported, as shown in the figure below:
2. If there is a problem with the above figure, install bzip2 and you will be
prompted : Bzip2: Unable to exec: There is no such file or directory.
Reason: Need to install the bzip2 package.
Solution: yum install -y bzip2
3. After the installation is successful, you can successfully execute tar -jxvf ffmpeg-4.2.4.tar.bz2, as shown below:
4. Enter the decompression directory and install yasm, as shown in the figure below:
If you are prompted, select "y", as shown in the figure below:
5. Execute the command after installation:
./configure --enable-shared --prefix=/usr/ffmpeg
6. Execute the make command, here I waited for half an hour (wait patiently,,,,)
7. Execute the make install command (installation), as shown in Figure
8. Modify the file /etc/ld.so.conf
[root@vmsp ffmpeg-4.2.4]# vi /etc/ld.so.conf
Add /usr/ffmpeg/lib/, as shown below:
9. Execute ldconfig to make it effective
[root@vmsp ffmpeg-4.2.4]# ldconfig
10. Configure environment variables
Use the vi /etc/profile command to open the profile file and add environment variables at the end of the file:
PATH=$PATH:/usr/ffmpeg/bin
export PATH
Execute source /etc/profile to make it effective
11. Check whether the configuration is successful
4.2 Java calls ffmpeg video screenshot
Here I provide a demo, you can encapsulate it into a tool method, and call it in the project.
/**
* @Author : zhangS
* @Date :2020-08-18
*/
public class ExeclLinuxCMD {
/**
* 根据名称截取目标视频,生成试看视频
* @param timeLength 截取时长
* @param oldVideoName 原视频名
* @param newVideoName 试看视频名称
* @return
*/
public static Object exec(Integer timeLength,String oldVideoName,String newVideoName) {
String cmd = "ffmpeg -ss 0:0:0 -t 0:0:"+timeLength+" -i /usr/ffmpeg/"+oldVideoName+" -vcodec copy -acodec copy /usr/ffmpeg/"+newVideoName;
try {
String[] cmdA = {
"/bin/sh", "-c", cmd };
Process process = Runtime.getRuntime().exec(cmdA);
LineNumberReader br = new LineNumberReader(new InputStreamReader(
process.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
sb.append(line).append("\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
//String netsString = exec("netstat -nat|grep -i 80").toString();
exec(50,"test.mp4","test2.mp4");
System.out.println("==========execute success=============");
}
}
4.3 Effect display
After the user uploads the video, the server generates a trial video by calling ffmpeg to intercept the video, as shown below:
4.4 Integration into the project
The user uploads the video through the background, and then generates 2 copies through FFmpeg. The page only needs to make business judgments on the user, and then play the corresponding video:
Play demo video:
Play full video:
5. Technical point analysis
This blog is mainly for the dem compilation of the video trial function. I will also provide the video interception operation under windows, which is convenient for other developers to use a solution for reference when encountering the development scene of the video trial.
There are many other streaming media processing functions in the FFmpeg framework. Here we focus on the implementation of video interception methods. This blog is implemented by manually installing FFmpeg in the system, and then directly executing the command line in Java.