Search in sources :

Example 1 with Logger

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());
                }
            }
        }
    };
}
Also used : FFExecListener(org.fagu.fmv.ffmpeg.executor.FFExecListener) Arrays(java.util.Arrays) H264(org.fagu.fmv.ffmpeg.coder.H264) SortedSet(java.util.SortedSet) Stream(org.fagu.fmv.ffmpeg.metadatas.Stream) Duration(org.fagu.fmv.utils.time.Duration) CommandLine(org.apache.commons.exec.CommandLine) StringUtils(org.apache.commons.lang3.StringUtils) Decoders(org.fagu.fmv.ffmpeg.coder.Decoders) Size(org.fagu.fmv.utils.media.Size) ScaleMode(org.fagu.fmv.ffmpeg.filter.impl.ScaleMode) Map(java.util.Map) Formats(org.fagu.fmv.ffmpeg.format.Formats) VolumeDetected(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected) FilterComplex(org.fagu.fmv.ffmpeg.filter.FilterComplex) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) CommandLineUtils(org.fagu.fmv.soft.exec.CommandLineUtils) Rotation(org.fagu.fmv.utils.media.Rotation) Scale(org.fagu.fmv.ffmpeg.filter.impl.Scale) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) Collection(java.util.Collection) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) List(java.util.List) Optional(java.util.Optional) ResampleAudio(org.fagu.fmv.ffmpeg.filter.impl.ResampleAudio) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) TextProgressBar(org.fagu.fmv.textprogressbar.TextProgressBar) Libx264NotDisibleBy2FFExecFallback(org.fagu.fmv.ffmpeg.executor.fallback.Libx264NotDisibleBy2FFExecFallback) FilenameUtils(org.apache.commons.io.FilenameUtils) FFExecFallback(org.fagu.fmv.ffmpeg.executor.FFExecFallback) CropDetection(org.fagu.fmv.ffmpeg.filter.impl.CropDetection) Strict(org.fagu.fmv.ffmpeg.flags.Strict) FFExecListener(org.fagu.fmv.ffmpeg.executor.FFExecListener) OptionalInt(java.util.OptionalInt) ArrayList(java.util.ArrayList) AutoRotate(org.fagu.fmv.ffmpeg.filter.impl.AutoRotate) StreamOrder(org.fagu.fmv.mymedia.movie.StreamOrder) StringTokenizer(java.util.StringTokenizer) CropSize(org.fagu.fmv.ffmpeg.filter.impl.CropDetection.CropSize) Type(org.fagu.fmv.ffmpeg.operation.Type) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Loggers(org.fagu.fmv.mymedia.logger.Loggers) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) FMVExecutor(org.fagu.fmv.soft.exec.FMVExecutor) IOException(java.io.IOException) Progress(org.fagu.fmv.ffmpeg.operation.Progress) FrameRate(org.fagu.fmv.ffmpeg.utils.FrameRate) FMVExecuteException(org.fagu.fmv.soft.exec.exception.FMVExecuteException) ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) CropDetect(org.fagu.fmv.ffmpeg.filter.impl.CropDetect) File(java.io.File) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) FFMpegProgressBar(org.fagu.fmv.ffmpeg.progressbar.FFMpegProgressBar) Logger(org.fagu.fmv.mymedia.logger.Logger) StringJoiner(java.util.StringJoiner) VolumeDetect(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetect) ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) CropSize(org.fagu.fmv.ffmpeg.filter.impl.CropDetection.CropSize) CommandLine(org.apache.commons.exec.CommandLine) Size(org.fagu.fmv.utils.media.Size) CropSize(org.fagu.fmv.ffmpeg.filter.impl.CropDetection.CropSize) Map(java.util.Map) CropDetection(org.fagu.fmv.ffmpeg.filter.impl.CropDetection) FMVExecutor(org.fagu.fmv.soft.exec.FMVExecutor)

Example 2 with Logger

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();
}
Also used : Arrays(java.util.Arrays) Files(java.nio.file.Files) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) File(java.io.File) Size(org.fagu.fmv.utils.media.Size) AppVersion(org.fagu.fmv.mymedia.utils.AppVersion) Logger(org.fagu.fmv.mymedia.logger.Logger) DateTimeFormatter(java.time.format.DateTimeFormatter) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) Type(org.fagu.fmv.ffmpeg.operation.Type) FilenameUtils(org.apache.commons.io.FilenameUtils) LoggerFactory(org.fagu.fmv.mymedia.logger.LoggerFactory) IOException(java.io.IOException) Logger(org.fagu.fmv.mymedia.logger.Logger) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) File(java.io.File)

Example 3 with Logger

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()));
}
Also used : Arrays(java.util.Arrays) Files(java.nio.file.Files) Collection(java.util.Collection) LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) ByteSize.formatSize(org.fagu.fmv.utils.ByteSize.formatSize) Supplier(java.util.function.Supplier) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet) AppVersion(org.fagu.fmv.mymedia.utils.AppVersion) Logger(org.fagu.fmv.mymedia.logger.Logger) MutableLong(org.apache.commons.lang3.mutable.MutableLong) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) FilenameUtils(org.apache.commons.io.FilenameUtils) LoggerFactory(org.fagu.fmv.mymedia.logger.LoggerFactory) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Logger(org.fagu.fmv.mymedia.logger.Logger) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MutableLong(org.apache.commons.lang3.mutable.MutableLong) File(java.io.File)

Example 4 with Logger

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);
}
Also used : Soft(org.fagu.fmv.soft.Soft) Convert(org.fagu.fmv.im.soft.Convert) IMOperation(org.fagu.fmv.im.IMOperation) Logger(org.fagu.fmv.mymedia.logger.Logger) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) File(java.io.File) Loggers(org.fagu.fmv.mymedia.logger.Loggers) IMOperation(org.fagu.fmv.im.IMOperation) File(java.io.File) Soft(org.fagu.fmv.soft.Soft)

Example 5 with Logger

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());
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) Size(org.fagu.fmv.utils.media.Size) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) Logger(org.fagu.fmv.mymedia.logger.Logger) File(java.io.File) Rotation(org.fagu.fmv.utils.media.Rotation)

Aggregations

File (java.io.File)5 Logger (org.fagu.fmv.mymedia.logger.Logger)5 IOException (java.io.IOException)4 Arrays (java.util.Arrays)3 FilenameUtils (org.apache.commons.io.FilenameUtils)3 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)3 Size (org.fagu.fmv.utils.media.Size)3 Files (java.nio.file.Files)2 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Collection (java.util.Collection)2 Map (java.util.Map)2 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)2 VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)2 Type (org.fagu.fmv.ffmpeg.operation.Type)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1