Search in sources :

Example 1 with Progress

use of org.fagu.fmv.ffmpeg.operation.Progress 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;
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Progress(org.fagu.fmv.ffmpeg.operation.Progress) VolumeDetect(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetect) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) OptionalInt(java.util.OptionalInt) CropDetect(org.fagu.fmv.ffmpeg.filter.impl.CropDetect) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) Stream(org.fagu.fmv.ffmpeg.metadatas.Stream) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) StringJoiner(java.util.StringJoiner)

Example 2 with Progress

use of org.fagu.fmv.ffmpeg.operation.Progress in project fmv by f-agu.

the class PreparedImages method makeVideo.

/**
 * @param imageFrameRate
 * @param destVideo
 * @param videoFrameRate
 * @throws IOException
 */
public void makeVideo(FrameRate imageFrameRate, File destVideo, FPS videoFrameRate) throws IOException {
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    // input
    File srcFiles = new File(parentFolder, "img_%0" + countLengthNumber() + "d." + extension);
    builder.addMediaInput(Image2Demuxer.from(srcFiles).frameRate(imageFrameRate));
    FilterComplex filter = FilterComplex.create(org.fagu.fmv.ffmpeg.filter.impl.FrameRate.to(videoFrameRate), Format.with(PixelFormat.YUV420P));
    builder.filter(filter);
    OutputProcessor outputProcessor = builder.addMediaOutputFile(destVideo);
    outputProcessor.codec(H264.findRecommanded());
    outputProcessor.overwrite();
    outputProcessor.map().allStreams().input(filter);
    FFExecutor<Object> executor = builder.build();
    System.out.println(executor.getCommandLine());
    int countEstimateFrames = (int) (files.size() * videoFrameRate.countFrameBySeconds() * imageFrameRate.invert().doubleValue());
    Progress progress = executor.getProgress();
    if (progress != null) {
        textProgressBar = FFMpegProgressBar.with(progress).byFrame(countEstimateFrames).build().makeBar("");
    }
    executor.execute();
}
Also used : FilterComplex(org.fagu.fmv.ffmpeg.filter.FilterComplex) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Progress(org.fagu.fmv.ffmpeg.operation.Progress) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) File(java.io.File)

Example 3 with Progress

use of org.fagu.fmv.ffmpeg.operation.Progress in project fmv by f-agu.

the class FFReducer method reduceAudio.

/**
 * @param metadatas
 * @param srcFile
 * @param destFile
 * @param bitRate
 * @param consolePrefixMessage
 * @param logger
 * @return
 * @throws IOException
 */
private boolean reduceAudio(MovieMetadatas metadatas, File srcFile, File destFile, String bitRate, String consolePrefixMessage, Logger logger) throws IOException {
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    builder.hideBanner();
    int sampleRate = Math.min(metadatas.getAudioStream().sampleRate().orElse(audioSampleRate), audioSampleRate);
    builder.addMediaInputFile(srcFile);
    FilterComplex filter = FilterComplex.create(ResampleAudio.build().frequency(sampleRate));
    builder.filter(filter);
    OutputProcessor outputProcessor = builder.addMediaOutputFile(destFile);
    outputProcessor.audioChannel(2);
    outputProcessor.audioBitRate(bitRate);
    outputProcessor.format(audioFormat);
    outputProcessor.overwrite();
    outputProcessor.map().allStreams().input(filter);
    FFExecutor<Object> executor = builder.build();
    executor.addListener(createLogFFExecListener(logger));
    Duration duration = metadatas.getAudioStream().duration().orElse(null);
    Progress progress = executor.getProgress();
    if (duration != null && progress != null) {
        textProgressBar = FFMpegProgressBar.with(progress).byDuration(duration).fileSize(srcFile.length()).build().makeBar(consolePrefixMessage);
    }
    executor.execute();
    return false;
}
Also used : FilterComplex(org.fagu.fmv.ffmpeg.filter.FilterComplex) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Progress(org.fagu.fmv.ffmpeg.operation.Progress) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) Duration(org.fagu.fmv.utils.time.Duration)

Example 4 with Progress

use of org.fagu.fmv.ffmpeg.operation.Progress 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
 */
public void reduceVideo(MovieMetadatas metadatas, File srcFile, MovieMetadatas movieMetadatas, File destFile, String consolePrefixMessage, Logger logger) throws IOException {
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    builder.hideBanner();
    InputProcessor inputProcessor = builder.addMediaInputFile(srcFile);
    builder.filter(AutoRotate.create(movieMetadatas));
    org.fagu.fmv.mymedia.reduce.FFReducer.applyScaleIfNecessary(builder, movieMetadatas, Size.HD720, logger);
    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);
    }
    // -------------------------- codec -------------------------
    outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(23));
    // subtitle
    if (videoMetadatas.contains(Type.AUDIO)) {
        outputProcessor.codecCopy(Type.AUDIO);
    }
    // subtitle
    if (videoMetadatas.contains(Type.SUBTITLE)) {
        outputProcessor.codecCopy(Type.SUBTITLE);
    }
    outputProcessor.overwrite();
    FFExecutor<Object> executor = builder.build();
    executor.addListener(org.fagu.fmv.mymedia.reduce.FFReducer.createLogFFExecListener(logger));
    OptionalInt countEstimateFrames = metadatas.getVideoStream().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();
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Progress(org.fagu.fmv.ffmpeg.operation.Progress) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) OptionalInt(java.util.OptionalInt) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) Stream(org.fagu.fmv.ffmpeg.metadatas.Stream) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) StringJoiner(java.util.StringJoiner)

Example 5 with Progress

use of org.fagu.fmv.ffmpeg.operation.Progress in project fmv by f-agu.

the class VolumeInfoFile method toLine.

/**
 * @see org.fagu.fmv.mymedia.file.InfoFile#toLine(org.fagu.fmv.utils.file.FileFinder.FileFound,
 *      org.fagu.fmv.media.Media)
 */
@Override
public String toLine(FileFound fileFound, FileFinder<Media>.InfosFile infosFile) throws IOException {
    Media main = infosFile.getMain();
    MovieMetadatas metadatas = (MovieMetadatas) main.getMetadatas();
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    builder.addMediaInputFile(fileFound.getFileFound());
    VolumeDetect volumeDetect = VolumeDetect.build();
    builder.filter(volumeDetect);
    builder.addMediaOutput(NullMuxer.build()).overwrite();
    FFExecutor<Object> executor = builder.build();
    if (metadatas != null) {
        OptionalInt countEstimateFrames = metadatas.getVideoStream().countEstimateFrames();
        Progress progress = executor.getProgress();
        if (countEstimateFrames.isPresent() && progress != null) {
            try (TextProgressBar bar = FFMpegProgressBar.with(progress).byFrame(countEstimateFrames.getAsInt()).build().makeBar("Detect volume")) {
                executor.execute();
            }
            System.out.println();
        }
    } else {
        executor.execute();
    }
    VolumeDetected volumeDetected = volumeDetect.getDetected();
    return volumeDetected.toString();
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Progress(org.fagu.fmv.ffmpeg.operation.Progress) VolumeDetect(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetect) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) Media(org.fagu.fmv.media.Media) OptionalInt(java.util.OptionalInt) TextProgressBar(org.fagu.fmv.textprogressbar.TextProgressBar) VolumeDetected(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected)

Aggregations

FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)5 Progress (org.fagu.fmv.ffmpeg.operation.Progress)5 OutputProcessor (org.fagu.fmv.ffmpeg.operation.OutputProcessor)4 OptionalInt (java.util.OptionalInt)3 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)3 StringJoiner (java.util.StringJoiner)2 FilterComplex (org.fagu.fmv.ffmpeg.filter.FilterComplex)2 VolumeDetect (org.fagu.fmv.ffmpeg.filter.impl.VolumeDetect)2 AudioStream (org.fagu.fmv.ffmpeg.metadatas.AudioStream)2 Stream (org.fagu.fmv.ffmpeg.metadatas.Stream)2 SubtitleStream (org.fagu.fmv.ffmpeg.metadatas.SubtitleStream)2 InputProcessor (org.fagu.fmv.ffmpeg.operation.InputProcessor)2 File (java.io.File)1 CropDetect (org.fagu.fmv.ffmpeg.filter.impl.CropDetect)1 VolumeDetected (org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected)1 VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)1 Media (org.fagu.fmv.media.Media)1 TextProgressBar (org.fagu.fmv.textprogressbar.TextProgressBar)1 Duration (org.fagu.fmv.utils.time.Duration)1