如何使用Webcam Capture API从网络摄像头视频捕获(图像+音频)?

时间:2019-08-23 20:25:22

标签: java audio video webcam webcam-capture

我已经看到了一些有关使用此API(网络摄像头捕获)的视频捕获的代码,但仅限于视频图像捕获,即无音频的视频(例如,下面记录的Encoder.java示例文件)。我正在开始一个需要此功能的项目,我认为此API非常轻巧且有用。有人可以通过在此类中添加代码(或显示一个新代码)来帮助我,以便它可以同时捕获视频和音频并写入同一文件吗?

public class Encoder {
    public static void main(String[] args) throws Throwable {
        File file = new File("output.ts");
        IMediaWriter writer = ToolFactory.makeWriter(file.getName());
        Dimension size = WebcamResolution.QVGA.getSize();

        writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, size.width, size.height);

        Webcam webcam = Webcam.getDefault();
        webcam.setViewSize(size);
        webcam.open(true);

        long start = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            System.out.println("Capture frame " + i);
            BufferedImage image = ConverterFactory.convertToType(webcam.getImage(), BufferedImage.TYPE_3BYTE_BGR);
            IConverter converter = ConverterFactory.createConverter(image, IPixelFormat.Type.YUV420P);
            IVideoPicture frame = converter.toPicture(image, (System.currentTimeMillis() - start) * 1000);
            frame.setKeyFrame(i == 0);
            frame.setQuality(0);

            writer.encodeVideo(0, frame);
            // 10 FPS
            Thread.sleep(100);
        }
        writer.close();

        System.out.println("Video recorded in file: " + file.getAbsolutePath());
    }
}

0 个答案:

没有答案