3

我目前正在使用 JMF 录制视频(网络摄像头)和音频(麦克风)。我没有遇到任何编码问题。但是在输出方面我有一个问题。视频和音频完全不同步。在音频之后的视频帧开始之间有 1 秒的时间延迟。

以前有人遇到过这个问题吗?

下面是我用来捕捉视频的代码。

    MediaLocator videoMediaLocator = captureVideoDevice.getLocator();
    DataSource videoDataSource = null;
    try {
        videoDataSource = javax.media.Manager.createDataSource(videoMediaLocator);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoDataSourceException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    MediaLocator audioMediaLocator = captureAudioDevice.getLocator();
    DataSource audioDataSource = null;
    try {
        audioDataSource = javax.media.Manager.createDataSource(audioMediaLocator);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoDataSourceException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    DataSource mixedDataSource = null;
    DataSource dArray[] = new DataSource[2];

    dArray[0] = videoDataSource;
    dArray[1] = audioDataSource;
    try {
        mixedDataSource = javax.media.Manager.createMergingDataSource(dArray);
    } catch (IncompatibleSourceException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    FileTypeDescriptor outputType = new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME);
    Format outputFormat[] = new Format[2];
    VideoFormat videoFormat = new VideoFormat(VideoFormat.MJPG);
    outputFormat[0] = videoFormat;
    outputFormat[1] = new AudioFormat(AudioFormat.IMA4, 44100, 16, 1);
    ProcessorModel processorModel = new ProcessorModel(mixedDataSource, outputFormat, outputType);
    Processor processor = null;
    try {
        processor = Manager.createRealizedProcessor(processorModel);
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoProcessorException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (CannotRealizeException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    DataSource source = processor.getDataOutput();
    TrackControl [] tracks = processor.getTrackControls();
    Format format = tracks[0].getFormat(); 
    float frameRate = ((VideoFormat)format).getFrameRate();
    MediaLocator dest = new MediaLocator("file:c:\\test.mov");

    DataSink dataSink = null;
    try {
        dataSink = Manager.createDataSink(source, dest);
    } catch (NoDataSinkException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        dataSink.open();
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        dataSink.start();
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    processor.start();
    System.out.println("starting capturing ...");
    try { Thread.currentThread().sleep(30000); } catch (InterruptedException ie) {} 
    System.out.println("... capturing done");

    processor.stop();
    processor.close();
    dataSink.close();
4

0 回答 0