java录屏h264流半秒
时间: 2023-11-10 20:04:15 浏览: 144
android录屏 h264裸流
您可以使用JavaCV库来录制屏幕并生成H264流。以下是一个示例代码片段:
```
import org.bytedeco.javacv.*;
import java.awt.*;
public class ScreenRecorder {
public static void main(String[] args) throws Exception {
// 设置录制器参数
FrameGrabber grabber = new Java2DFrameGrabber(
Screen.getPrimaryDisplay().getID());
grabber.setFormat("avfoundation");
grabber.setImageWidth(640);
grabber.setImageHeight(480);
grabber.start();
// 设置编码器参数
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(
"output.mp4", grabber.getImageWidth(), grabber.getImageHeight());
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("mp4");
recorder.setFrameRate(30);
recorder.start();
// 录制屏幕
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 500) {
recorder.record(grabber.grab());
}
// 停止录制
recorder.stop();
grabber.stop();
}
}
```
阅读全文