Earn Online (2) English (10) Java (7) Money (1) Money Online (1) Script (2) Trending (7)

ffmpeg audio & video transcoding with java


We can transcode Audio & Video file with the help of an open source tool FFMPEG. To achieve this we can use Java & FFMPEG in combination to avoid FFMPEG low level study.
Parameter list for FFMPEG command :
To transcode audio or video files use below params -

String[] FFMPEG_PARAM_LIST = { "-ss", "-i", "-t", "-vn", "-vcodec", "-vpre", "-strict", "-b","-b:v", "-r", "-s", "-an", "-acodec", "-ab", "-ac", "-ar", "-vol", "-f", "-y", "-map_metadata" };

To create a preview from an audio or video file use below params -
String[] FFMPEG_PREVIEW_PARAM_LIST = { "-ss", "-t" };


Java implementation to achieve transcoding:

String[] ENCODING_PARAMS_MP4_320x240 = { "-i", "INPUTFILE", "-s", "320x240", "-vcodec", "mpeg4", "-acodec", "libfaac", "-b","120k", "-ac", "2", "-ar", "16000", "-r", "15", "-ab", "16000", "-f", "mp4", "-y", "OUTPUTFILE" };
Above values will create a MP4 file with 320x240 resolution with MPEG4 video codec and AAC audio codec.

String[] ENCODING_PARAMS_3GP_176x144 = { "-i", "INPUTFILE", "-s", "176x144", "-vcodec", "h263", "-acodec", "libopencore_amrnb", "-ac", "1", "-ar", "8000", "-r", "12", "-b", "112k", "-ab", "12.20k", "-f", "3gp", "-y", "OUTPUTFILE" };
Above values will create a 3GP file with 176x144 resolution with H263 video codec and AMR audio codec.

Execute FFMPEG Using Java
private void executFFMPEG(String params[]) throws Exception
{
  List executionList = new ArrayList();
  executionList.add(<Add FFMPEG Binary Path>);
  // Use above arrays to create executionList
  for (String param : params) {
    executionList.add(param);
  }
  String ffMPEGCommand = "";
  for (String command : executionList) {
    ffMPEGCommand = ffMPEGCommand + " " + command;
  }

  ProcessBuilder pb = new ProcessBuilder(executionList);
  pb.redirectErrorStream(true);
  Process process = pb.start();
  InputStreamReader isr = new InputStreamReader(process.getInputStream());
  BufferedReader br = new BufferedReader(isr);

  String lineRead;
  while ((lineRead = br.readLine()) != null) {
     //You can print command output here
  }
  process.waitFor();
}



ffmpeg audio & video transcoding with java ffmpeg audio & video transcoding with java Reviewed by Yogesh Choudhry on January 03, 2013 Rating: 5

Popular Posts