ffmpeg audio & video transcoding with java
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.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
Reviewed by Yogesh Choudhry
on
January 03, 2013
Rating: