从网络摄像头java录制音频和视频

Posted

技术标签:

【中文标题】从网络摄像头java录制音频和视频【英文标题】:Record audio and video from webcam java 【发布时间】:2015-09-22 16:59:47 【问题描述】:

这是一个使用 jxcapture API 录制音频和视频的程序。运行时出现错误。谁能帮我解决这个问题。

   import com.teamdev.jxcapture.Codec;
   import com.teamdev.jxcapture.EncodingParameters;
   import com.teamdev.jxcapture.VideoCapture;
   import com.teamdev.jxcapture.audio.AudioCodec;
   import com.teamdev.jxcapture.audio.AudioEncodingParameters;
   import com.teamdev.jxcapture.audio.Audiosource;
   import com.teamdev.jxcapture.video.VideoFormat;
   import com.teamdev.jxcapture.video.VideoSource;

   import java.io.File;
   import java.util.List;


 public class CaptureVideoFromWebCameraWithAudio 

public static void main(String[] args) throws Exception 
    VideoCapture videoCapture = VideoCapture.create(VideoFormat.MP4);

    List<VideoSource> availableVideoSources = VideoSource.getAvailable();
    System.out.println("availableVideoSources = " + availableVideoSources);

    if (availableVideoSources.isEmpty()) 
        throw new IllegalStateException("No external video sources available");
    

    VideoSource webCamera = availableVideoSources.get(0);
    System.out.println("webCamera = " + webCamera);

    videoCapture.setVideoSource(webCamera);

    List<Codec> videoCodecs = videoCapture.getVideoCodecs();
    Codec videoCodec = videoCodecs.get(2);
    System.out.println("videoCodec = " + videoCodec);

    EncodingParameters encodingParameters = new EncodingParameters(new File("WebCamera.MP4"));
    encodingParameters.setBitrate(500000);
    encodingParameters.setFramerate(10);
    encodingParameters.setKeyFrameInterval(1);
    encodingParameters.setCodec(videoCodec);

    System.out.println("Available audio recording sources:");
    List<AudioSource> audioSources = AudioSource.getAvailable();
    for (AudioSource audioSource : audioSources) 
        System.out.println("audioSource = " + audioSource);
    
    if (audioSources.isEmpty()) 
        System.err.println("No audio sources available");
     else 
        AudioSource audioSource = audioSources.get(0);
        System.out.println("Selected audio source = " + audioSource);
        videoCapture.setAudioSource(audioSource);

        List<AudioCodec> audioCodecs = videoCapture.getAudioCodecs();
        if (audioSources.isEmpty()) 
            System.err.println("No audio codecs available");
         else 
            System.out.println("Available audio codecs:");
            for (AudioCodec audioCodec : audioCodecs) 
                System.out.println("audioCodec = " + audioCodec);
            

            // Enable and configure audio encoding
            AudioEncodingParameters audioEncoding = new AudioEncodingParameters();

            AudioCodec audioCodec = audioCodecs.get(0);
            System.out.println("Selected audio codec = " + audioCodec);
            audioEncoding.setCodec(audioCodec);

            encodingParameters.setAudioEncoding(audioEncoding);
        
    
    System.out.println("encodingParameters = " + encodingParameters);

    videoCapture.start(encodingParameters);
    System.out.println("Recording started. Press 'Enter' to pause.");
    System.in.read();
    videoCapture.pause();
    System.out.println("Recording started. Press 'Enter' to resume.");
    System.in.read();
    videoCapture.start();
    System.out.println("Recording started. Press 'Enter' to stop.");
    System.in.read();
    videoCapture.stop();
    System.out.println("Done.");
  
  

错误

Run:
Exception in thread "main" java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory 
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
Java Result: 1 

【问题讨论】:

【参考方案1】:

虽然您很久以前就问过这个问题,但请尝试将此 maven 依赖项添加到您的项目中:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

如果你不使用 maven,请从这里获取 jar:https://www.slf4j.org/download.html

【讨论】:

添加 slf4j 实现之一(如简单或 logback)可能很有用。

以上是关于从网络摄像头java录制音频和视频的主要内容,如果未能解决你的问题,请参考以下文章

如何从相机(或网络摄像头)在 python 中捕获视频(和音频)

如何使用 Java 从网络摄像头获取视频和音频流?

如何录制两个音频输入和一个视频输入?

Gstreamer 录制带音频的视频

使用 C# Accord .Net 从网络摄像头录制视频

如何使用 webRTC 和基于服务器的对等连接录制网络摄像头和音频