ca.carleton.gcrc.olkit.multimedia.ffmpeg.FFmpegProcessorDefault Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nunaliit2-multimedia Show documentation
Show all versions of nunaliit2-multimedia Show documentation
Library that converts multimedia files using command
line tools such as avconv and ImageMagick
package ca.carleton.gcrc.olkit.multimedia.ffmpeg;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ca.carleton.gcrc.olkit.multimedia.converter.MultimediaConversionProgress;
public class FFmpegProcessorDefault implements FFmpegProcessor {
static final protected Logger logger = LoggerFactory.getLogger(FFmpegProcessorDefault.class);
static private Pattern patternTime = Pattern.compile("^\\s*frame=.*time=\\s*(\\d+\\.\\d*)");
static public String ffmpegInfoCommand = "avprobe %1$s";
static public String ffmpegConvertVideoCommand = "avconv -i %1$s -y -acodec libvo_aacenc -ab 48000 -ac 2 -vcodec libx264 -b 128000 -s 320x240 -threads 0 -f mp4 %2$s";
static public String ffmpegConvertAudioCommand = "avconv -i %1$s -y -acodec libmp3lame -ab 48000 -ac 2 -threads 0 -f mp3 %2$s";
static public String ffmpegCreateThumbnailCommand = "avconv -y -ss 00:00:05 -i %1$s -s %3$dx%4$d -r 1 -vframes 1 -f image2 %2$s";
static String[] breakUpCommand(String command){
String[] commandTokens = command.split(" ");
int count = 0;
for(String token : commandTokens){
if( token.length() > 0 ) ++count;
}
String[] tokens = new String[count];
int index = 0;
for(String token : commandTokens){
if( token.length() > 0 ) {
tokens[index] = token;
++index;
}
}
return tokens;
}
private MultimediaConversionProgress progressTracker;
public FFmpegProcessorDefault() {
}
public FFmpegProcessorDefault(MultimediaConversionProgress progressTracker) {
this.progressTracker = progressTracker;
}
@Override
public FFmpegMediaInfo getMediaInfo(File mediafile) throws Exception {
FFmpegMediaInfoImpl info = new FFmpegMediaInfoImpl(mediafile);
Runtime rt = Runtime.getRuntime();
StringWriter sw = new StringWriter();
try {
String[] tokens = breakUpCommand(ffmpegInfoCommand);
for(int i=0; i 100 ) {
percentInt = 100;
}
progressTracker.updateProgress(percentInt);
}
}
line = bufReader.readLine();
}
} catch (IOException e) {
throw new Exception("Error while converting video: "+sw.toString(),e);
}
}
@Override
public void convertAudio(File inputFile, File outputFile) throws Exception {
FFmpegMediaInfo inputVideo = getMediaInfo(inputFile);
convertAudio(inputVideo, outputFile);
}
@Override
public void convertAudio(FFmpegMediaInfo inputVideo, File outputFile) throws Exception {
Runtime rt = Runtime.getRuntime();
StringWriter sw = new StringWriter();
try {
String[] tokens = breakUpCommand(ffmpegConvertAudioCommand);
for(int i=0; i 100 ) {
percentInt = 100;
}
progressTracker.updateProgress(percentInt);
}
}
line = bufReader.readLine();
}
} catch (IOException e) {
throw new Exception("Error while converting audio :"+sw.toString(),e);
}
}
@Override
public void createThumbnail(File inputFile, File outputFile, int maxWidth, int maxHeight) throws Exception {
FFmpegMediaInfo inputVideo = getMediaInfo(inputFile);
createThumbnail(inputVideo, outputFile, maxWidth, maxHeight);
}
@Override
public void createThumbnail(FFmpegMediaInfo inputVideo, File outputFile, int maxWidth, int maxHeight) throws Exception {
// Compute width and height, preserving aspect ratio
int width = maxWidth;
int height = maxHeight;
if( null != inputVideo.getWidth()
&& null != inputVideo.getHeight() ){
long inputHeight = inputVideo.getHeight();
long inputWidth = inputVideo.getWidth();
float ratio = (float)inputHeight / (float)inputWidth;
height = Math.round(width * ratio);
if( height > maxHeight ){
height = maxHeight;
width = (int)(height / ratio);
}
}
Runtime rt = Runtime.getRuntime();
StringWriter sw = new StringWriter();
try {
String[] tokens = breakUpCommand(ffmpegCreateThumbnailCommand);
for(int i=0; i