I am currently recording a video (webcam) and audio (mic) using JMF. I did not encounter any problem with the coding. But I have an issue when it come to the output. The video and audio is totally out of sync. There is a 1s time delay between the start of the video frame after the audio.
Anyone encountered this issue before?
Below is the code I used to capture the video.
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();