Search in sources :

Example 1 with Size

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

the class Bootstrap method newProject.

/**
 * @param printStream
 * @return
 */
private static Project newProject(PrintStream printStream) {
    try (Scanner scanner = new Scanner(new UnclosedInputStream(System.in))) {
        // name
        printStream.print("Name: ");
        String name = scanner.nextLine();
        // save file
        final File defaultSaveFile = new File(name);
        printStream.print("SaveFolder: [" + defaultSaveFile.getAbsolutePath() + "] ");
        File saveFile = null;
        String ssavefile = scanner.nextLine();
        if (StringUtils.isBlank(ssavefile)) {
            saveFile = defaultSaveFile;
        } else {
            saveFile = new File(ssavefile);
        }
        saveFile = new File(saveFile, name + ".fmv");
        saveFile.getParentFile().mkdirs();
        // format
        final String defaultFormat = "mp4";
        printStream.print("Format: [" + defaultFormat + "] ");
        String format = scanner.nextLine();
        if (StringUtils.isBlank(format)) {
            format = defaultFormat;
        }
        // size
        final Size defaultSize = Size.HD720;
        printStream.print("Size: [" + defaultSize + "] ");
        Size size = null;
        while (size == null) {
            String ssize = scanner.nextLine();
            if (StringUtils.isBlank(ssize)) {
                size = defaultSize;
            } else {
                try {
                    size = Size.parse(ssize);
                } catch (IllegalArgumentException e) {
                    printStream.println("Don't understand: " + ssize);
                }
            }
        }
        // rate
        final FrameRate defaultRate = FrameRate.valueOf(30, 1);
        printStream.print("Rate: [" + defaultRate + "] ");
        FrameRate frameRate = null;
        while (frameRate == null) {
            String srate = scanner.nextLine();
            if (StringUtils.isBlank(srate)) {
                frameRate = defaultRate;
            } else {
                try {
                    frameRate = FrameRate.parse(srate);
                } catch (IllegalArgumentException e) {
                    printStream.println("Don't understand: " + srate);
                }
            }
        }
        OutputInfos outputInfos = new OutputInfos();
        outputInfos.setSize(size);
        outputInfos.setFrameRate(frameRate);
        // TODO
        outputInfos.setAudioSampling(44100);
        outputInfos.setFormat(format);
        Project project = new Project(saveFile, outputInfos);
        project.setName(name);
        try {
            project.save();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return project;
    }
}
Also used : Scanner(java.util.Scanner) Project(org.fagu.fmv.core.project.Project) FrameRate(org.fagu.fmv.ffmpeg.utils.FrameRate) UnclosedInputStream(org.fagu.fmv.utils.io.UnclosedInputStream) Size(org.fagu.fmv.utils.media.Size) OutputInfos(org.fagu.fmv.core.project.OutputInfos) IOException(java.io.IOException) File(java.io.File)

Example 2 with Size

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

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

the class Movie method getDevice.

/**
 * @see org.fagu.fmv.media.Media#getDevice()
 */
@Override
public String getDevice() {
    List<String> keys = new ArrayList<>();
    VideoStream videoStream = videoMetadatas.getVideoStream();
    if (videoStream != null) {
        String handlerName = videoStream.handlerName().orElse(null);
        if (StringUtils.isNotEmpty(handlerName)) {
            keys.add(handlerName);
        }
        Size size = videoStream.size();
        if (size != null) {
            keys.add(size.toString());
        }
    }
    AudioStream audioStream = videoMetadatas.getAudioStream();
    if (audioStream != null) {
        String handlerName = audioStream.handlerName().orElse(null);
        if (StringUtils.isNotEmpty(handlerName)) {
            keys.add(handlerName);
        }
        OptionalInt sampleRate = audioStream.sampleRate();
        if (sampleRate.isPresent()) {
            keys.add(sampleRate.getAsInt() + "Hz");
        }
    }
    return keys.stream().collect(Collectors.joining(" "));
}
Also used : AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) Size(org.fagu.fmv.utils.media.Size) ArrayList(java.util.ArrayList) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) OptionalInt(java.util.OptionalInt)

Example 4 with Size

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

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

the class Bootstrap method isUpperThan720p.

/**
 * @param metadatas
 * @param logger
 * @return
 */
private boolean isUpperThan720p(MovieMetadatas metadatas, Logger logger) {
    if (!metadatas.contains(Type.VIDEO)) {
        logger.log("It's not a video");
        return false;
    }
    VideoStream videoStream = metadatas.getVideoStream();
    Size size = videoStream.size();
    if (size.getHeight() > Size.HD720.getHeight()) {
        logger.log("It's upper than 720p: " + size);
        return true;
    }
    logger.log("It's not upper than 720p: " + size);
    return false;
}
Also used : Size(org.fagu.fmv.utils.media.Size) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream)

Aggregations

Size (org.fagu.fmv.utils.media.Size)12 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)7 File (java.io.File)5 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)5 VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)5 IOException (java.io.IOException)4 Rotation (org.fagu.fmv.utils.media.Rotation)4 OutputInfos (org.fagu.fmv.core.project.OutputInfos)3 InputProcessor (org.fagu.fmv.ffmpeg.operation.InputProcessor)3 OutputProcessor (org.fagu.fmv.ffmpeg.operation.OutputProcessor)3 Logger (org.fagu.fmv.mymedia.logger.Logger)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Optional (java.util.Optional)2 OptionalInt (java.util.OptionalInt)2 FilenameUtils (org.apache.commons.io.FilenameUtils)2 FFExecutor (org.fagu.fmv.ffmpeg.executor.FFExecutor)2 AutoRotate (org.fagu.fmv.ffmpeg.filter.impl.AutoRotate)2 ResampleAudio (org.fagu.fmv.ffmpeg.filter.impl.ResampleAudio)2 Scale (org.fagu.fmv.ffmpeg.filter.impl.Scale)2