Search in sources :

Example 1 with Rotation

use of org.fagu.fmv.utils.media.Rotation in project fmv by f-agu.

the class MovieScriptConverter method convert.

/**
 * @see org.fagu.fmv.mymedia.classify.Converter#convert(org.fagu.fmv.media.Media,
 *      org.fagu.fmv.utils.file.FileFinder.InfosFile, java.io.File, org.fagu.fmv.mymedia.classify.ConverterListener)
 */
@Override
public void convert(Movie srcMedia, FileFinder<Movie>.InfosFile infosFile, File destFile, ConverterListener<Movie> listener) throws IOException {
    openScript();
    File srcFile = srcMedia.getFile();
    MovieMetadatas infos = srcMedia.getMetadatas();
    int audioFrequency = FFMpegUtils.minAudioSampleRate(infos, DEFAULT_AUDIO_SAMPLE_RATE);
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    builder.hideBanner();
    builder.addMediaInputFile(srcFile).setMovieMetadatas(infos);
    Rotation rotation = rotateMap.get(srcFile.getName());
    if (rotation != null) {
        if (rotation != Rotation.R_0) {
            builder.filter(Rotate.create(rotation));
        }
    } else {
        builder.filter(AutoRotate.create(infos));
    }
    Size newSize = FFReducer.applyScaleIfNecessary(builder, infos, getMaxSize(), getScaleLogger(), rotation);
    writeLabel();
    script.println("rem " + (newSize.isLandscape() ? "landscape" : newSize.isPortrait() ? "portrait" : "square"));
    builder.filter(ResampleAudio.build().frequency(audioFrequency));
    Optional<VolumeDetected> findFirst = infosFile.getInfos().stream().filter(o -> o instanceof VolumeDetected).map(o -> (VolumeDetected) o).findFirst();
    if (findFirst.isPresent()) {
        VolumeDetected volumeDetected = findFirst.get();
        builder.filter(Volume.build().increaseToMax(volumeDetected));
    }
    File dest = new File(destFile.getParentFile(), FilenameUtils.getBaseName(destFile.getName()) + ".mp4");
    OutputProcessor outputProcessor = builder.addMediaOutputFile(dest);
    outputProcessor.qualityScale(0);
    Transpose.addMetadataRotate(outputProcessor, Rotation.R_0);
    outputProcessor.format("mp4");
    // outputProcessor.overwrite();
    FFExecutor<Object> executor = builder.build();
    try {
        script.println("if exist \"" + dest.getPath() + "\" goto :movie_" + currentVideo);
        script.println("echo.");
        script.println("echo Frame: " + infos.getVideoStream().countEstimateFrames().getAsInt());
        script.println(executor.getCommandLine());
        script.println();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : FFReducer(org.fagu.fmv.mymedia.reduce.FFReducer) HashMap(java.util.HashMap) Size(org.fagu.fmv.utils.media.Size) AutoRotate(org.fagu.fmv.ffmpeg.filter.impl.AutoRotate) ConverterListener(org.fagu.fmv.mymedia.classify.ConverterListener) Map(java.util.Map) VolumeDetected(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) PrintStream(java.io.PrintStream) Rotation(org.fagu.fmv.utils.media.Rotation) Rotate(org.fagu.fmv.ffmpeg.filter.impl.Rotate) FileFinder(org.fagu.fmv.utils.file.FileFinder) FFMpegUtils(org.fagu.fmv.ffmpeg.FFMpegUtils) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) IOException(java.io.IOException) File(java.io.File) Volume(org.fagu.fmv.ffmpeg.filter.impl.Volume) Transpose(org.fagu.fmv.ffmpeg.filter.impl.Transpose) Logger(org.fagu.fmv.mymedia.logger.Logger) Optional(java.util.Optional) ResampleAudio(org.fagu.fmv.ffmpeg.filter.impl.ResampleAudio) Converter(org.fagu.fmv.mymedia.classify.Converter) FilenameUtils(org.apache.commons.io.FilenameUtils) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Size(org.fagu.fmv.utils.media.Size) IOException(java.io.IOException) Rotation(org.fagu.fmv.utils.media.Rotation) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) File(java.io.File) VolumeDetected(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetected)

Example 2 with Rotation

use of org.fagu.fmv.utils.media.Rotation in project fmv by f-agu.

the class FFReducer method applyScaleIfNecessary.

/**
 * @param builder
 * @param movieMetadatas
 * @param maxSize
 * @param logger
 * @param rotation
 * @return
 */
public static Size applyScaleIfNecessary(FFMPEGExecutorBuilder builder, MovieMetadatas movieMetadatas, Size maxSize, Logger logger, Rotation inRotation) {
    VideoStream videoStream = movieMetadatas.getVideoStream();
    Size size = videoStream.size();
    Rotation rotation = inRotation;
    if (rotation == null) {
        rotation = videoStream.rotate();
    }
    if (rotation != null) {
        size = rotation.resize(size);
    }
    if (size.getWidth() <= maxSize.getWidth() && size.getHeight() <= maxSize.getHeight()) {
        return size;
    }
    StringBuilder log = new StringBuilder();
    log.append("Need to resize ").append(size);
    if (rotation != null) {
        log.append(" (rotation of ").append(rotation.getValue()).append(')');
    }
    size = size.fitAndKeepRatioTo(maxSize);
    if (rotation != null && !AutoRotate.isAutoRotateObsolete()) {
        size = rotation.resize(size);
    }
    // fix [libx264 @ 037f8b00] height not divisible by 2 (1078x607)
    size = Libx264NotDisibleBy2FFExecFallback.resize(size);
    log.append(" to under ").append(maxSize).append(": ").append(size);
    logger.log(log.toString());
    // .forceOriginalAspectRatio(ForceOriginalAspectRatio.DECREASE)
    builder.filter(Scale.to(size, ScaleMode.fitToBoxKeepAspectRatio()));
    return size;
}
Also used : Size(org.fagu.fmv.utils.media.Size) CropSize(org.fagu.fmv.ffmpeg.filter.impl.CropDetection.CropSize) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) Rotation(org.fagu.fmv.utils.media.Rotation)

Example 3 with Rotation

use of org.fagu.fmv.utils.media.Rotation 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 4 with Rotation

use of org.fagu.fmv.utils.media.Rotation 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)

Aggregations

Rotation (org.fagu.fmv.utils.media.Rotation)4 File (java.io.File)3 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)3 Size (org.fagu.fmv.utils.media.Size)3 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)2 VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)2 OutputProcessor (org.fagu.fmv.ffmpeg.operation.OutputProcessor)2 Logger (org.fagu.fmv.mymedia.logger.Logger)2 FileFinder (org.fagu.fmv.utils.file.FileFinder)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 FilenameUtils (org.apache.commons.io.FilenameUtils)1 FFMpegUtils (org.fagu.fmv.ffmpeg.FFMpegUtils)1 FFExecutor (org.fagu.fmv.ffmpeg.executor.FFExecutor)1 AutoRotate (org.fagu.fmv.ffmpeg.filter.impl.AutoRotate)1 CropSize (org.fagu.fmv.ffmpeg.filter.impl.CropDetection.CropSize)1 ResampleAudio (org.fagu.fmv.ffmpeg.filter.impl.ResampleAudio)1