use of org.fagu.fmv.ffmpeg.filter.impl.CropDetect in project fmv by f-agu.
the class FFReducer method createCropDetectFFExecListener.
/**
* @param logger
* @param cropDetect
* @param videoMetadatas
* @return
*/
private FFExecListener createCropDetectFFExecListener(Logger logger, CropDetect cropDetect, MovieMetadatas videoMetadatas) {
return new FFExecListener() {
/**
* @see org.fagu.fmv.soft.exec.FMVExecListener#eventPostExecute(org.fagu.fmv.soft.exec.FMVExecutor,
* org.apache.commons.exec.CommandLine, java.util.Map, org.apache.commons.exec.ExecuteResultHandler)
*/
@Override
public void eventPostExecute(FMVExecutor fmvExecutor, CommandLine command, Map environment, ExecuteResultHandler handler) {
CropDetection cropDetection = cropDetect.getCropSizeDetected();
SortedSet<CropSize> orderedCropSizes = cropDetection.getOrderedCropSizes();
if (!orderedCropSizes.isEmpty()) {
CropSize first = orderedCropSizes.first();
Size size = first.toSize();
if (!videoMetadatas.getVideoStreams().stream().anyMatch(s -> size.equals(s.size()))) {
logger.log("CropDetect: " + cropDetection.getTotalCount() + " lines parsed");
orderedCropSizes.stream().limit(10).forEach(cs -> logger.log("CropDetect: " + cs));
logger.log("CropDetect: Add crop filter: " + first.toCrop());
}
}
}
};
}
use of org.fagu.fmv.ffmpeg.filter.impl.CropDetect in project fmv by f-agu.
the class FFReducer method reduceVideo.
/**
* @param metadatas
* @param srcFile
* @param movieMetadatas
* @param destFile
* @param consolePrefixMessage
* @param logger
* @throws IOException
*/
private boolean reduceVideo(MovieMetadatas metadatas, File srcFile, File destFile, String consolePrefixMessage, Logger logger) throws IOException {
AudioStream audioStream = metadatas.getAudioStream();
boolean audioCodecCopy = audioStream.isCodec(Formats.AC3);
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.hideBanner();
InputProcessor inputProcessor = builder.addMediaInputFile(srcFile);
builder.filter(AutoRotate.create(metadatas));
applyScaleIfNecessary(builder, metadatas, getMaxSize(), logger);
VolumeDetect volumeDetect = null;
if (!audioCodecCopy) {
volumeDetect = VolumeDetect.build();
builder.filter(volumeDetect);
}
CropDetect cropDetect = CropDetect.build();
builder.filter(cropDetect);
MovieMetadatas videoMetadatas = inputProcessor.getMovieMetadatas();
Collection<AudioStream> audioStreams = StreamOrder.sort(videoMetadatas.getAudioStreams());
OutputProcessor outputProcessor = builder.addMediaOutputFile(destFile);
outputProcessor.qualityScale(0);
// video
for (Stream stream : videoMetadatas.getVideoStreams()) {
logger.log("map[" + stream.index() + "] video: " + stream);
outputProcessor.map().streams(stream).input(inputProcessor);
}
// audio
for (Stream stream : audioStreams) {
logger.log("map[" + stream.index() + "] audio: " + stream);
outputProcessor.map().streams(stream).input(inputProcessor);
}
// subtitle
Collection<SubtitleStream> subtitleStreams = StreamOrder.sort(videoMetadatas.getSubtitleStreams());
for (Stream stream : subtitleStreams) {
logger.log("map[" + stream.index() + "] subtitle: " + stream);
outputProcessor.map().streams(stream).input(inputProcessor);
}
// other stream (Apple... again bullshit)
// for (Stream stream : videoMetadatas.getStreams()) {
// Type type = stream.type();
// if (type != Type.AUDIO && type != Type.VIDEO && type != Type.SUBTITLE) {
// logger.log("map other stream: " + stream);
// outputProcessor.map().streams(stream).input(inputProcessor);
// }
// }
// -------------------------- codec -------------------------
outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(crf));
// audio
if (audioCodecCopy) {
logger.log("Audio: AC3, copy");
outputProcessor.codecCopy(Type.AUDIO);
} else {
logger.log("Audio: force AAC");
outputProcessor.codecAutoSelectAAC();
}
// subtitle
if (videoMetadatas.contains(Type.SUBTITLE)) {
outputProcessor.codecCopy(Type.SUBTITLE);
}
outputProcessor.overwrite();
FFExecutor<Object> executor = builder.build();
executor.addListener(createLogFFExecListener(logger));
executor.addListener(createCropDetectFFExecListener(logger, cropDetect, videoMetadatas));
if (!audioCodecCopy) {
executor.addListener(createVolumeDetectFFExecListener(logger, volumeDetect));
}
VideoStream videoStream = metadatas.getVideoStream();
OptionalInt countEstimateFrames = videoStream.countEstimateFrames();
Progress progress = executor.getProgress();
if (countEstimateFrames.isPresent() && progress != null) {
textProgressBar = FFMpegProgressBar.with(progress).byFrame(countEstimateFrames.getAsInt()).fileSize(srcFile.length()).build().makeBar(consolePrefixMessage);
} else {
StringJoiner joiner = new StringJoiner(", ");
if (progress == null) {
joiner.add("progress not found");
}
if (!countEstimateFrames.isPresent()) {
joiner.add("nb frames nout found");
}
logger.log("No progress bar: " + joiner.toString());
}
executor.execute();
Optional<String> codecName = videoStream.codecName();
if (codecName.isPresent() && codecName.get().equalsIgnoreCase(Formats.HEVC.getName())) {
// h265
return true;
}
return false;
}
use of org.fagu.fmv.ffmpeg.filter.impl.CropDetect in project fmv by f-agu.
the class FFHelper method cropDetect.
/**
* @param inFile
* @throws IOException
*/
public static CropDetection cropDetect(File inFile) throws IOException {
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.addMediaInputFile(inFile).timeSeek(Time.parse("36:05")).duration(Duration.valueOf(3));
CropDetect cropDetect = CropDetect.build();
builder.filter(cropDetect);
builder.addMediaOutput(NullMuxer.build()).overwrite();
FFExecutor<Object> executor = builder.build();
System.out.println(executor.getCommandLine());
executor.execute();
return cropDetect.getCropSizeDetected();
}
Aggregations