Java uses FFmpeg rough processing video transcoding, cover image
This is the 12th day of my participation in Gwen Challenge
Rough Processing of video transcoding and cover image by FFmpeg (1)
Business scenarios:
Transcoding the uploaded video using Java and uploading the video to the file storage server
Video processing tool class VideoUtil
package com.ozx.util;
import it.sauronsoftware.jave.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/ * * *@author Gxin
* @date2021/6/26 11:09 * /
public class VideoUtil {
/** * the path configuration can be imported into the configuration file */
private final static String FFMPEG_PATH = "E: \ \ FFmpeg \ \ FFmpeg - 2 - win64 - static \ \ bin \ \ FFmpeg exe";
private final static String SOURCE = "C:\\Users\\VULCAN\\Desktop\\test1\\";
private final static String TARGET = "C:\\Users\\VULCAN\\Desktop\\test2\\";
// Set the screenshot size
private final static String IMAGE_SIZE = "800x600";
// Set the snapshot time
private final static String IMAGE_TIME = "00:00:02";
private final static Logger logger = LoggerFactory.getLogger(VideoUtil.class);
/** * Convert the uploaded video to MP4 format and take screenshots */
public static List<String> transformAndCompressVideo(MultipartFile file) {
List<String> list = new ArrayList<>(0);
// Convert the video to MP4 format
String prefix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
// Temporary file path as conversion source
String sourceVideoPath = SOURCE + UUID.randomUUID().toString().replace("-"."") + prefix;
File source = new File(sourceVideoPath);
try {
file.transferTo(source);
} catch (Exception e) {
logger.error("Failed to convert multipartFile to file,{}", e);
}
// Destination file path after conversion
String targetVideoPath = TARGET + UUID.randomUUID().toString().replace("-"."") + ".mp4";
File target = new File(targetVideoPath);
// Audio attributes
AudioAttributes audio = new AudioAttributes();
// Set the audio encoding libFaac PGM encoding
audio.setCodec("libmp3lame");
// Set the audio bit rate
audio.setBitRate(new Integer(56000));
// Set the audio channel
audio.setChannels(new Integer(1));
// Set the audio sampling rate
audio.setSamplingRate(new Integer(22050));
// Video properties
VideoAttributes video = new VideoAttributes();
// Set the video encoding
video.setCodec("mpeg4");
// Set the video bit rate. The higher the bit rate, the sharper the file will be
video.setBitRate(new Integer(1200000));
// Set the video frame rate
video.setFrameRate(new Integer(25));
// Transcoding property
EncodingAttributes attr = new EncodingAttributes();
// Set the video format
attr.setFormat("mp4");
// Set the audio properties
attr.setAudioAttributes(audio);
// Set the video properties
attr.setVideoAttributes(video);
// Create a transcoder
Encoder encoder = new Encoder();
try {
encoder.encode(source, target, attr);
} catch (Exception e) {
logger.error("Video format conversion failed,{}", e);
}
// Video capture frame 2s as the cover image
String targetImagePath = TARGET + UUID.randomUUID().toString().replace("-"."") + ".jpg";
File imageTarget = new File(targetImagePath);
covPic(sourceVideoPath, IMAGE_TIME, targetImagePath, IMAGE_SIZE);
list.add(sourceVideoPath);
// Delete the local file after uploading to the storage server and add targetVideoPath and targetImagePath as well
returnlist; }}Copy the code
Video transcoding and cut cover picture test class
package com.ozx.util;
import com.ozx.mybatisplus.utils.video.VideoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
/ * * *@author Gxin
* @date 2021/6/26 15:05
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class VideoTest {
@Test
public void test1(a) {
List<String> fileList = null;
try {
File file = new File("C:\\Users\\VULCAN\\Desktop\\test\\test2.wmv");
FileInputStream fis = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(),"text/plain", IOUtils.toByteArray(fis));
fileList = VideoUtil.transformAndCompressVideo(multipartFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Delete temporary files after transcoding upload is complete
for (String filePath : fileList) {
File file = newFile(filePath); VideoUtil.deleteFile(file); }}}}Copy the code