Search in sources :

Example 1 with FFExecutor

use of org.fagu.fmv.ffmpeg.executor.FFExecutor in project fmv by f-agu.

the class ChannelLayout method runnable.

/**
 * @return
 */
private static Runnable runnable() {
    return () -> {
        LinesFFMPEGOperation operation = new LinesFFMPEGOperation();
        operation.addParameter("-layouts");
        try {
            FFExecutor<List<String>> executor = new FFExecutor<>(operation);
            Consumer<ChannelLayoutHelp> cacheConsumer = HELP_CACHE.consumer();
            AvailableHelp<ChannelLayoutHelp> availableHelp = AvailableHelp.create();
            Reader individualReader = l -> {
                if (StringUtils.isBlank(l)) {
                    return false;
                }
                String name = StringUtils.substringBefore(l, " ").trim();
                ChannelLayoutHelp channelLayoutHelp = new ChannelLayoutHelp(name, true);
                channelLayoutHelp.text = StringUtils.substringAfter(l, " ").trim();
                cacheConsumer.accept(channelLayoutHelp);
                return true;
            };
            Reader standardReader = l -> {
                if (StringUtils.isBlank(l)) {
                    return false;
                }
                String name = StringUtils.substringBefore(l, " ").trim();
                ChannelLayoutHelp channelLayoutHelp = new ChannelLayoutHelp(name, false);
                channelLayoutHelp.text = StringUtils.substringAfter(l, " ").trim();
                cacheConsumer.accept(channelLayoutHelp);
                return true;
            };
            availableHelp.title().unreadLine().reader(individualReader).unreadLines(3).reader(standardReader).parse(executor.execute().getResult());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}
Also used : Consumer(java.util.function.Consumer) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) Reader(org.fagu.fmv.ffmpeg.utils.AvailableHelp.Reader) LinesFFMPEGOperation(org.fagu.fmv.ffmpeg.operation.LinesFFMPEGOperation) IOException(java.io.IOException)

Example 2 with FFExecutor

use of org.fagu.fmv.ffmpeg.executor.FFExecutor in project fmv by f-agu.

the class FullHelpExtract method extract.

/**
 * @return
 */
public static Map<String, Group> extract() {
    LinesFFMPEGOperation operation = new LinesFFMPEGOperation();
    operation.addParameter("-h", "full");
    try {
        FFExecutor<List<String>> executor = new FFExecutor<>(operation);
        Executed<List<String>> execute = executor.execute();
        Pattern paramPattern = Pattern.compile("(\\s+)([\\-\\w]+)\\s+(?:\\<(\\w+)\\>\\s+)?([EDFVASXR\\.]{8})(.*)");
        int countEmpty = 0;
        String currentTitle = null;
        Group currentGroup = null;
        Param currentParam = null;
        Map<String, Group> groupMap = new HashMap<>(256);
        boolean start = false;
        for (String line : execute.getResult()) {
            boolean isEmpty = "".equals(line);
            if (isEmpty) {
                ++countEmpty;
                if (!start && countEmpty == 2) {
                    start = true;
                }
                continue;
            } else {
                countEmpty = 0;
            }
            if (!start) {
                continue;
            }
            if (line.charAt(0) != ' ') {
                currentTitle = StringUtils.substringBefore(line, " AVOptions:");
                currentGroup = null;
                continue;
            }
            // 
            Matcher matcher = paramPattern.matcher(line);
            if (matcher.matches()) {
                String paramName = matcher.group(2);
                String paramType = matcher.group(3);
                Flags paramFlags = Flags.parse(matcher.group(4));
                String desc = matcher.group(5);
                if (desc != null) {
                    desc = desc.trim();
                }
                if (currentGroup == null) {
                    currentGroup = new Group(currentTitle, desc);
                    groupMap.put(currentTitle, currentGroup);
                }
                if (matcher.group(1).length() == 2) {
                    currentParam = new Param(paramName, ParamType.valueOf(paramType.toUpperCase()), paramFlags, desc);
                    currentGroup.addParam(currentParam);
                } else {
                    currentParam.addValue(new ParamValue(paramName, paramFlags, desc));
                }
            } else {
                System.out.println(line);
            }
        }
        return groupMap;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) LinesFFMPEGOperation(org.fagu.fmv.ffmpeg.operation.LinesFFMPEGOperation) IOException(java.io.IOException) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) List(java.util.List)

Example 3 with FFExecutor

use of org.fagu.fmv.ffmpeg.executor.FFExecutor in project fmv by f-agu.

the class PixelFormat method runnable.

/**
 * @return
 */
private static Runnable runnable() {
    return () -> {
        LinesFFMPEGOperation operation = new LinesFFMPEGOperation();
        operation.addParameter("-pix_fmts");
        try {
            FFExecutor<List<String>> executor = new FFExecutor<>(operation);
            Consumer<PixelFormatHelp> cacheConsumer = HELP_CACHE.consumer();
            final Pattern pattern = Pattern.compile("(\\d+)\\s+(\\d+)");
            Function<String, PixelFormatHelp> factory = PixelFormatHelp::new;
            Consumer<PixelFormatHelp> consumer = help -> {
                Matcher matcher = pattern.matcher(help.getText());
                if (matcher.matches()) {
                    help.nbComponents = Integer.parseInt(matcher.group(1));
                    help.bitsPerPixel = Integer.parseInt(matcher.group(2));
                    cacheConsumer.accept(help);
                } else {
                    throw new RuntimeException("PixelFormat description unparsable: " + help.getText());
                }
            };
            AvailableHelp<PixelFormatHelp> availableHelp = AvailableHelp.create();
            availableHelp.title().legend().unreadLines(2).values(factory, consumer).parse(executor.execute().getResult());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}
Also used : Pattern(java.util.regex.Pattern) Function(java.util.function.Function) Consumer(java.util.function.Consumer) Matcher(java.util.regex.Matcher) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) LinesFFMPEGOperation(org.fagu.fmv.ffmpeg.operation.LinesFFMPEGOperation) IOException(java.io.IOException)

Example 4 with FFExecutor

use of org.fagu.fmv.ffmpeg.executor.FFExecutor 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 5 with FFExecutor

use of org.fagu.fmv.ffmpeg.executor.FFExecutor in project fmv by f-agu.

the class Filters method runnable.

/**
 * @return
 */
private static Runnable runnable() {
    return () -> {
        LinesFFMPEGOperation operation = new LinesFFMPEGOperation();
        operation.addParameter("-filters");
        try {
            FFExecutor<List<String>> executor = new FFExecutor<>(operation);
            Consumer<FilterHelp> cacheConsumer = HELP_CACHE.consumer();
            final Pattern PATTERN = Pattern.compile("([AVN\\|]+)-\\>([AVN\\|]+)\\s+(\\w+.*)");
            Function<String, FilterHelp> factory = name -> new FilterHelp(name);
            Consumer<FilterHelp> consumer = help -> {
                Matcher matcher = PATTERN.matcher(help.getText());
                if (matcher.matches()) {
                    help.inputType = IOType.parse(matcher.group(1));
                    help.outputType = IOType.parse(matcher.group(2));
                    help.description = matcher.group(3);
                    cacheConsumer.accept(help);
                } else {
                    throw new RuntimeException("Filter description unparsable: " + help.getText());
                }
            };
            AvailableHelp<FilterHelp> availableHelp = AvailableHelp.create();
            availableHelp.title().legend(true).values(factory, consumer).parse(executor.execute().getResult());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    };
}
Also used : Pattern(java.util.regex.Pattern) Function(java.util.function.Function) Consumer(java.util.function.Consumer) AvailableHelp(org.fagu.fmv.ffmpeg.utils.AvailableHelp) Matcher(java.util.regex.Matcher) FFExecutor(org.fagu.fmv.ffmpeg.executor.FFExecutor) LinesFFMPEGOperation(org.fagu.fmv.ffmpeg.operation.LinesFFMPEGOperation) IOException(java.io.IOException)

Aggregations

FFExecutor (org.fagu.fmv.ffmpeg.executor.FFExecutor)8 IOException (java.io.IOException)6 LinesFFMPEGOperation (org.fagu.fmv.ffmpeg.operation.LinesFFMPEGOperation)5 Consumer (java.util.function.Consumer)4 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 HashMap (java.util.HashMap)2 Function (java.util.function.Function)2 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)2 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)2 MediaInput (org.fagu.fmv.ffmpeg.operation.MediaInput)2 File (java.io.File)1 PrintStream (java.io.PrintStream)1 List (java.util.List)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 FFExecutorFactory (org.fagu.fmv.ffmpeg.executor.FFExecutorFactory)1 FilterNaming (org.fagu.fmv.ffmpeg.filter.FilterNaming)1