Search in sources :

Example 21 with MovieMetadatas

use of org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas 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)

Example 22 with MovieMetadatas

use of org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas 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 23 with MovieMetadatas

use of org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas 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)

Example 24 with MovieMetadatas

use of org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas in project fmv by f-agu.

the class Bootstrap method findMovie.

// ---------------------------------------------------
/**
 */
private MovieFinder findMovie(File saveFile, File... srcFiles) throws IOException {
    MovieFinder movieFinder = new MovieFinder(saveFile);
    movieFinder.addInfoFile(new VolumeInfoFile());
    movieFinder.addListener(new FileFinderListener<Movie>() {

        private int count;

        /**
         * @see org.fagu.fmv.utils.file.FileFinderListener#eventFindPre(org.fagu.fmv.utils.file.FileFinder.FileFound)
         */
        @Override
        public void eventFindPre(FileFound fileFound) {
            System.out.println(count + ": " + fileFound.getFileFound().getName());
            ++count;
        }

        /**
         * @see org.fagu.fmv.utils.file.FileFinderListener#eventFindPost(FileFound, java.lang.Object)
         */
        @Override
        public void eventFindPost(FileFound fileFound, FileFinder<Movie>.InfosFile infosFile) {
            Movie movie = infosFile.getMain();
            MovieMetadatas videoMetadatas = movie.getMetadatas();
            // Format format = videoMetadatas.getFormat();
            VideoStream videoStream = videoMetadatas.getVideoStream();
            Rotation rotate = videoStream.rotate();
            // System.out.println(count + ": " + file.getName() + " " + infos); // .getFormat().creationDate()
            System.out.println("   " + rotate + ", " + videoStream.handlerName());
        }
    });
    for (File srcFile : srcFiles) {
        movieFinder.find(srcFile);
    }
    return movieFinder;
}
Also used : VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) FileFinder(org.fagu.fmv.utils.file.FileFinder) Rotation(org.fagu.fmv.utils.media.Rotation) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) MovieFinder(org.fagu.fmv.mymedia.file.MovieFinder) FileFound(org.fagu.fmv.utils.file.FileFinder.FileFound) File(java.io.File)

Example 25 with MovieMetadatas

use of org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas in project fmv by f-agu.

the class ConsoleOutput method forOutput.

/**
 * @param fileSource
 * @param fromFile
 * @return
 */
public static String forOutput(FileSource fileSource, File fromFile) {
    StringBuilder buf = new StringBuilder(100);
    buf.append(StringUtils.rightPad(Integer.toString(fileSource.getNumber()), 3)).append(' ').append(' ');
    FileType fileType = fileSource.getFileType();
    if (fileType != null) {
        buf.append(fileType.name().charAt(0)).append(' ');
    }
    String meta = null;
    if (fileSource.isImage()) {
        ImageMetadatas imageMetadatas = fileSource.getImageMetadatas();
        if (imageMetadatas != null) {
            meta = imageMetadatas.getDimension().toString();
        }
    }
    if (fileSource.isAudioOrVideo()) {
        MovieMetadatas videoMetadatas = fileSource.getVideoMetadatas();
        if (videoMetadatas != null) {
            AudioStream audioStream = videoMetadatas.getAudioStream();
            VideoStream videoStream = videoMetadatas.getVideoStream();
            if (videoStream != null) {
                Optional<Duration> duration = videoStream.duration();
                String sd = duration.isPresent() ? duration.get().toString() : "";
                meta = videoStream.size().toString() + " " + sd;
            } else if (audioStream != null) {
                Optional<Duration> duration = audioStream.duration();
                meta = duration.isPresent() ? duration.get().toString() : "";
            }
        }
    }
    String subPath = null;
    if (fromFile != null) {
        subPath = StringUtils.substringAfter(fileSource.getFile().getPath(), fromFile.getPath() + File.separator);
    } else {
        subPath = fileSource.getFile().getName();
    }
    buf.append(StringUtils.rightPad(meta, 25)).append(' ').append(' ').append(subPath);
    return buf.toString();
}
Also used : ImageMetadatas(org.fagu.fmv.im.ImageMetadatas) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) Optional(java.util.Optional) FileType(org.fagu.fmv.media.FileType) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) Duration(org.fagu.fmv.utils.time.Duration)

Aggregations

MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)31 File (java.io.File)17 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)17 VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)14 InputProcessor (org.fagu.fmv.ffmpeg.operation.InputProcessor)13 OutputProcessor (org.fagu.fmv.ffmpeg.operation.OutputProcessor)12 Stream (org.fagu.fmv.ffmpeg.metadatas.Stream)8 IOException (java.io.IOException)6 AudioStream (org.fagu.fmv.ffmpeg.metadatas.AudioStream)6 OptionalInt (java.util.OptionalInt)5 Size (org.fagu.fmv.utils.media.Size)5 StringJoiner (java.util.StringJoiner)4 SubtitleStream (org.fagu.fmv.ffmpeg.metadatas.SubtitleStream)4 Progress (org.fagu.fmv.ffmpeg.operation.Progress)4 Type (org.fagu.fmv.ffmpeg.operation.Type)4 Logger (org.fagu.fmv.mymedia.logger.Logger)4 Rotation (org.fagu.fmv.utils.media.Rotation)4 Optional (java.util.Optional)3 FilenameUtils (org.apache.commons.io.FilenameUtils)3 FFExecutor (org.fagu.fmv.ffmpeg.executor.FFExecutor)3