KBalazs
KBalazs

Reputation: 91

How to combine an AVI and a WAV file using only client side JAVA?

My program is doing a screen capture video, into an AVI file while also recording the microphone's voice into a WAV file. After it is finished, I would like to combine the two into a single file (so basically add the WAV file as the audio to the video).

I do the screen capturing with the following code(the ScreenRecorder class is from org.monte.screenrecorder ):

GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().
    getDefaultScreenDevice().getDefaultConfiguration();

Format fileFormat = new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI);
Format screenFormat = new Format(MediaTypeKey, MediaType.VIDEO, 
                    EncodingKey, ENCODING_AVI_MJPG,
                    CompressorNameKey, ENCODING_AVI_MJPG,
                    DepthKey, (int)24, 
                    FrameRateKey, Rational.valueOf(15),
                    QualityKey, 1.0f,
                    KeyFrameIntervalKey, (int) (15 * 60));
Format mouseFormat = new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,"black",
FrameRateKey, Rational.valueOf(30));
screenRecorder = new ScreenRecorder(gc, fileFormat, screenFormat, mouseFormat, null);   

For the audio recording I do it like this (so you can see all the formats, parameters etc.):

float sampleRate = 44100;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = true;    
AudioFormat format new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
    System.out.println("Line not supported");
    System.exit(0);
}
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();   // start capturing

System.out.println("Start capturing...");

AudioInputStream ais = new AudioInputStream(line);

System.out.println("Start recording...");

// start recording
AudioSystem.write(ais, fileType, wavFile);

I tried the following solutions, to no avail: JMF ( javax.media.Manager, it dies at createRealizedProcessor ) Xuggle ( com.xuggle.mediatool and com.xuggle.xuggler )

I'm pretty sure that they don't work because they don't like the input formats.

Yes, I can easily do this merge with FFMPEG from command line or on a server, but it is not good for me, I need a client side JAVA solution (can't really ask the users to install FFMPEG so I can call it with Runtime.getRuntime().exec or something).

Upvotes: 1

Views: 611

Answers (1)

halil
halil

Reputation: 800

I suggest you try vlcj, library used for similar purposes to yours.

http://caprica.github.io/vlcj/

You can try read and play two of them separately at the same time or try other approaches this library offers you. You can also do the recording part with this library too.

Upvotes: 1

Related Questions