Welcome to my GitHub
(including supporting source code) are classified and summarized here: 161f7349b750ce https://github.com/zq2599/blog_demos
Overview of this article
- This article is the third part of "JavaCV's Camera Actual Combat", as the title, let's practice how to save the video content of the camera as an MP4 file
coding
- Create a new file <font color="blue">RecordCameraSaveMp4.java</font>, which is a subclass of AbstractCameraApplication, and its code is very simple.
- Define a member variable to specify the storage path of the video file. Here, the file name uses the current time string. Please adjust the path according to your own computer:
// 存放视频文件的完整位置,请改为自己电脑的可用目录
private static final String RECORD_FILE_PATH = "E:\\temp\\202111\\28\\camera-"
+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
+ ".mp4";
- The function of saving video frames as mp4 files comes from FrameRecorder, which is an abstract class. This article uses its subclass FFmpegFrameRecorder, so define member variables of FrameRecorder type:
// 帧录制器
protected FrameRecorder recorder;
- Then there is the initialization operation, which shows the instantiation of FFmpegFrameRecorder and the parameter settings:
@Override
protected void initOutput() throws Exception {
// 实例化FFmpegFrameRecorder
recorder = new FFmpegFrameRecorder(RECORD_FILE_PATH, // 存放文件的位置
getCameraImageWidth(), // 分辨率的宽,与视频源一致
getCameraImageHeight(), // 分辨率的高,与视频源一致
0); // 音频通道,0表示无
// 文件格式
recorder.setFormat("mp4");
// 帧率与抓取器一致
recorder.setFrameRate(getFrameRate());
// 编码格式
recorder.setPixelFormat(AV_PIX_FMT_YUV420P);
// 编码器类型
recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
// 视频质量,0表示无损
recorder.setVideoQuality(0);
// 初始化
recorder.start();
}
- Next is the output method, one line is enough:
@Override
protected void output(Frame frame) throws Exception {
// 存盘
recorder.record(frame);
}
- The last thing to do before the program exits after the loop that processes the video is over is to close the frame grabber:
@Override
protected void releaseOutputResource() throws Exception {
recorder.close();
}
- At this point, the function of saving the camera video as an mp4 file has been developed, and then write the main method. Note that the parameter <font color="blue">30</font> means that the capture and recording operations are executed for 30 seconds. Note that this is the duration of the program execution, <font color="red"> is not the duration of the recorded video </font>:
public static void main(String[] args) {
// 录制30秒视频
new RecordCameraSaveMp4().action(30);
}
- Run the main method and wait until the console outputs the content of the red box in the figure below, indicating that the video recording is complete:
- Open the directory where the mp4 file is located, as shown in the figure below. The red box is the file and related information just generated. It can be seen that the resolution and frame rate are as expected:
- Open this file with VLC, as shown below, the playback is normal:
- So far, we have completed the function of saving video files. Thanks to the power of JavaCV, the whole process is so easy and pleasant. Next, please continue to pay attention to Xinchen Originals. The "JavaCV Camera Actual" series will present more rich application;
Source code download
- The complete source code of "Camera Combat of JavaCV" can be downloaded from GitHub. The address and link information are shown in the following table ( https://github.com/zq2599/blog_demos ):
- There are multiple folders in this git project. The source code of this article is in the <font color="blue">javacv-tutorials</font> folder, as shown in the red box below:
- There are multiple sub-projects in <font color="blue">javacv-tutorials</font>. The code of "JavaCV Camera Actual" series is in <font color="red"> simple-grab-push </font> Under the project:
You are not alone, Xinchen Original is with you all the way
https://github.com/zq2599/blog_demos
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。