Simple and fast merge video and audio of mp4 files using javacv and ffmpeg framework
1. Dependence
<dependency> <groupId></groupId> <artifactId>javacv</artifactId> <version>1.5.1</version> </dependency> <!-- Video processing package --> <dependency> <groupId></groupId> <artifactId>ffmpeg-platform</artifactId> <version>4.1.3-1.5.1</version> </dependency>
2. Testing class
Can be packaged according to your needs
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import static .AV_LOG_ERROR; @SpringBootTest public class MP4Test { public static void main(String[] args) throws , { List<String> file = new ArrayList<>(); // Add mp4 files that need to be merged ("C:\Users\Administrator\Desktop\1\source\1.mp4"); ("C:\Users\Administrator\Desktop\1\source\2.mp4"); long start = (); // Merge videoMerge(file, "C:\Users\Administrator\Desktop\1\1\5.mp4"); // Statistical time-consuming ("time consuming" + (() - start) + "millisecond"); } /** * Merge of multiple videos * * @param videoList Address Collection * @param output The combined video output address */ public static void videoMerge(List<String> videoList, String output) throws , { // Frame grabber starts crawling based on the first video FFmpegFrameGrabber grabber = new FFmpegFrameGrabber((0)); // Load (); // Configure frame decoder FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(output, (), (), 0); // Video codec (avcodec.AV_CODEC_ID_H264); (avcodec.AV_CODEC_ID_AAC); // Set up the audio codec // Single channel (1); (true); // Output stream encapsulation format ("mp4"); // Video frame rate (()); // Set log output (error) avutil.av_log_set_level(AV_LOG_ERROR); // Set resolution format yuv420p (avutil.AV_PIX_FMT_YUV420P); // Set the video bit rate (()); // Audio bit rate (()); // Set the audio bit rate // Start coding (); // Loop setting for (String videoPath : videoList) { try (FFmpegFrameGrabber grabberTemp = new FFmpegFrameGrabber(videoPath)) { (); //Set the frame Frame frame; while ((frame = ()) != null) { (frame); } // Process audio frames Frame audioFrame; while ((audioFrame = ()) != null) { (audioFrame); } } catch ( e) { ("Error grabbing frames from video: " + videoPath); (); } } // Turn off the frame codec (); (); } }
This is the end of this article about JavaCV implementing the merger of multiple MP4 videos. For more related JavaCV video merging content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!