Core Java
Imal Perera  

Power Of ffmpeg

Spread the love

In a previous post I have demoed how to use this wonderful html5 getUserMedia spec to record audio and video directly in client side without get involved the server. but problem with that post is script is only effective in webkit browser like Chrome. and the next issue is recording happens in two separate  files,  video and audio so if we need to do a playback then either we’ll have to play them individually and write some javascript in the client side to do synchronization between audio and video playback. or we’ll have to merge the audio file and the video file in server side.

So in this post i’m explaining usage of ffmpeg  which is a complete, cross-platform solution to record, convert and stream audio and video [link] to merge .webm and wav file we generated

Ok. Then lets Start 🙂

1. Download ffmpeg from here

2. Copy the archive and extract it to the disc

3. Set path in Environment variables to <ffmpegfolder>/bin

if you have not set Environment variables in windows before please follow this simple tutorial  here 

4. now open CMD and type ffmpeg  if you are getting lot of lines in cmd you are good to go  😆

Using ffmpeg.exe to merge video and audio files

now since we have installed ffmpeg properly we can merge two video and audio files using following command

ffmpeg -i video.webm -i audio.wav -map 0:0 -map 1:0 outputaudiovideo.webm

Great… it is working!!! but wait we need to get it done through our program right not typing to CMD

Now lets find more about javas Runtime class. this class is having a static method called getRuntime() which gives a reference to the Runtime object so by this we can ask  java’s Current Runtime Environment to run external program by calling exe() method

 

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

public class MergeVideoAudio {

	public static void main(String[] args) {
		MergeVideoAudio.executeMergeCommand("/video.webm", "/audio.wav", "output.webm");
	}

	public static synchronized String executeMergeCommand(String inputWEBM,String inputWav, String outputWEBM) {
		System.out.println("---------------- Video Audio Merging started new---------------");
		long time = new Date().getTime();
		StringBuffer output = new StringBuffer();

		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("ffmpeg -i "+inputWEBM+" -i "+inputWav+" -map 0:0 -map 1:0 "+outputWEBM);

			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;

			while ((line = br.readLine()) != null){
				output.append(line);
			}

			System.out.println(output);
			proc.waitFor();
			long time2 = new Date().getTime();
			System.out.println("---Merge Completed in "+((time2-time)/1000)+ "seconds--  \n");
		} catch (Exception e) {
			e.printStackTrace();
		}
		String outdata = output.toString();
		System.out.println("---------------- Video Audio Merging finished---------------");
		return outdata;

	}

}

Remember to handle streams unless program might go into a deadlock so it might hang unexpectedly

[oa_social_link]

Leave A Comment