use of org.fagu.fmv.mymedia.logger.Logger 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.mymedia.logger.Logger in project fmv by f-agu.
the class Bootstrap method reduce.
/**
* @param rootFile
* @throws IOException
*/
public void reduce(File rootFile) throws IOException {
Logger logger = LoggerFactory.openLogger(LoggerFactory.getLogFile(rootFile, PROPERTY_LOG_FILE, PROPERTY_LOG_FILE_DEFAULT_NAME));
AppVersion.logMyVersion(logger::log);
logger.log("#################### Root: " + rootFile.getAbsolutePath());
//
Files.walk(rootFile.toPath()).filter(//
p -> p.toFile().isFile()).forEach(p -> {
logger.log("Reduce to 720p" + p);
String extension = FilenameUtils.getExtension(p.getName(p.getNameCount() - 1).toString()).toLowerCase();
if (!"mkv".equals(extension) && !"mp4".equals(extension)) {
return;
}
File srcFile = p.toFile();
try {
MovieMetadatas metadatas = MovieMetadatas.with(srcFile).extract();
if (!isUpperThan720p(metadatas, logger)) {
return;
}
File destFile = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcFile.getName()) + "-720p." + extension);
try (org.fagu.fmv.mymedia.reduce.to720p.FFReducer reducer = new org.fagu.fmv.mymedia.reduce.to720p.FFReducer()) {
String msg = LocalDateTime.now().format(DATE_TIME_FORMATTER) + ' ' + srcFile.getPath();
System.out.print(msg);
reducer.reduceVideo(metadatas, srcFile, metadatas, destFile, msg, logger);
}
} catch (IOException e) {
logger.log(e);
}
System.out.println();
});
System.out.println();
}
use of org.fagu.fmv.mymedia.logger.Logger in project fmv by f-agu.
the class Bootstrap method reduce.
/**
* @param rootFile
* @throws IOException
*/
public void reduce(File rootFile) throws IOException {
Logger logger = LoggerFactory.openLogger(LoggerFactory.getLogFile(rootFile, PROPERTY_LOG_FILE, PROPERTY_LOG_FILE_DEFAULT_NAME));
AppVersion.logMyVersion(logger::log);
logger.log("#################### Root: " + rootFile.getAbsolutePath());
MutableLong previousSize = new MutableLong();
MutableLong newSize = new MutableLong();
//
Files.walk(rootFile.toPath()).filter(//
p -> p.toFile().isFile()).forEach(p -> {
logger.log("Reduce " + p);
Supplier<Reducer> supplier = reducerMap.get(FilenameUtils.getExtension(p.getName(p.getNameCount() - 1).toString()).toLowerCase());
if (supplier != null) {
File srcFile = p.toFile();
Reduced reduced = null;
try (Reducer reducer = supplier.get()) {
logger.log("Reducer found: " + reducer.getName());
try {
String msg = LocalDateTime.now().format(DATE_TIME_FORMATTER) + ' ' + srcFile.getPath();
System.out.print(msg);
reduced = reducer.reduceMedia(srcFile, msg, logger);
} catch (Exception e) {
System.out.println();
e.printStackTrace();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
File destFile = reduced.getDestFile();
if (destFile != null && destFile.exists()) {
try {
boolean extensionChanged = !FilenameUtils.getExtension(srcFile.getName()).equalsIgnoreCase(FilenameUtils.getExtension(destFile.getName()));
if (reduced.isForceReplace() || (destFile.length() > 100 && (srcFile.length() > destFile.length() || extensionChanged))) {
String stringDiffSize = toStringDiffSize(srcFile.length(), destFile.length());
logger.log("Replace source by reduced: " + stringDiffSize + (reduced.isForceReplace() ? " [FORCED]" : ""));
System.out.print(" OK : " + stringDiffSize);
previousSize.add(srcFile.length());
newSize.add(destFile.length());
srcFile.delete();
String srcName = srcFile.getName();
String destExt = FilenameUtils.getExtension(destFile.getName());
if (!FilenameUtils.getExtension(srcName).equalsIgnoreCase(destExt)) {
srcFile = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcName) + "." + destExt);
}
if (!destFile.renameTo(srcFile)) {
System.err.print(" Rename failed: " + destFile);
logger.log("Rename failed: " + srcFile + " -> " + destFile);
}
} else {
logger.log("Revert");
System.out.print(" Revert");
if (!destFile.delete()) {
logger.log("Delete failed: " + destFile);
System.err.print(" Delete failed: " + destFile);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
System.out.println();
} else {
logger.log("Reducer not found");
}
});
System.out.println();
System.out.println(toStringDiffSize(previousSize.longValue(), newSize.longValue()));
}
use of org.fagu.fmv.mymedia.logger.Logger in project fmv by f-agu.
the class IMReducer method reduceMedia.
/**
* @see org.fagu.fmv.mymedia.reduce.Reducer#reduceMedia(java.io.File, String, Logger)
*/
@Override
public Reduced reduceMedia(File srcFile, String consolePrefixMessage, Logger logger) throws IOException {
File destFile = getTempFile(srcFile, format);
IMOperation op = new IMOperation();
op.image(srcFile, "[0]").autoOrient().quality(quality).image(destFile);
Soft convertSoft = Convert.search();
convertSoft.withParameters(op.toList()).logCommandLine(line -> logger.log("Exec: " + line)).execute();
return new Reduced(destFile, false);
}
use of org.fagu.fmv.mymedia.logger.Logger in project fmv by f-agu.
the class Rotate method main.
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File srcFile = new File("D:\\tmp\\Auvergne\\phone-vv\\VID_20170813_183349.mp4");
Rotation rotation = Rotation.R_90;
File dest = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcFile.getName()) + "-rotate-" + rotation + ".mp4");
FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
builder.hideBanner();
MovieMetadatas infos = builder.addMediaInputFile(srcFile).getMovieMetadatas();
int audioFrequency = FFMpegUtils.minAudioSampleRate(infos, DEFAULT_AUDIO_SAMPLE_RATE);
if (rotation != null && rotation != Rotation.R_0) {
builder.filter(org.fagu.fmv.ffmpeg.filter.impl.Rotate.create(rotation));
}
Logger logger = Loggers.systemOut();
Size newSize = FFReducer.applyScaleIfNecessary(builder, infos, MAX_SIZE, logger, rotation);
logger.log((newSize.isLandscape() ? "landscape" : newSize.isPortrait() ? "portrait" : "square"));
builder.filter(ResampleAudio.build().frequency(audioFrequency));
OutputProcessor outputProcessor = builder.addMediaOutputFile(dest);
outputProcessor.qualityScale(0);
Transpose.addMetadataRotate(outputProcessor, Rotation.R_0);
outputProcessor.format("mp4");
FFExecutor<Object> executor = builder.build();
logger.log(executor.getCommandLine());
}
Aggregations