Search in sources :

Example 1 with MovieMetadatas

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

the class OutputProcessor method mapStreams.

/**
 * @param predicate
 * @param forInputProcessor
 * @return
 * @throws IOException
 */
public OutputProcessor mapStreams(Predicate<Stream> predicate, InputProcessor forInputProcessor) throws IOException {
    Map myMap = map();
    MovieMetadatas movieMetadatas = forInputProcessor.getMovieMetadatas();
    for (Stream stream : movieMetadatas.getStreams()) {
        if (predicate.test(stream)) {
            myMap.streams(stream).input(forInputProcessor);
        }
    }
    return this;
}
Also used : MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) Stream(org.fagu.fmv.ffmpeg.metadatas.Stream)

Example 2 with MovieMetadatas

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

the class AutoRotateTestCase method test.

@Test
public void test() {
    MovieMetadatas movieMetadatas = mock(MovieMetadatas.class);
    VideoStream videoStream = mock(VideoStream.class);
    doReturn(videoStream).when(movieMetadatas).getVideoStream();
    doReturn(Optional.of("90")).when(videoStream).tag("rotate");
    AutoRotate.create(movieMetadatas);
}
Also used : MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) Test(org.junit.Test)

Example 3 with MovieMetadatas

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

the class Reduce method doIt.

/**
 * @param sourceFile
 * @throws IOException
 */
private static void doIt(File sourceFile) throws IOException {
    String extension = FilenameUtils.getExtension(sourceFile.getName()).toLowerCase();
    if (!"mkv".equals(extension)) {
        extension = "mp4";
    }
    File destinationFile = new File(sourceFile.getParentFile(), FilenameUtils.getBaseName(sourceFile.getName()) + "-new." + extension);
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    builder.hideBanner();
    InputProcessor inputProcessor = builder.addMediaInputFile(sourceFile);
    MovieMetadatas videoMetadatas = inputProcessor.getMovieMetadatas();
    boolean doVideo = !videoMetadatas.getVideoStream().isTreatedByFMV();
    boolean doAudio = doVideo;
    Collection<AudioStream> audioStreams = StreamOrder.sort(videoMetadatas.getAudioStreams());
    for (AudioStream audioStream : audioStreams) {
        if ("vorbis".equals(audioStream.codecName().get())) {
            doAudio |= true;
            break;
        }
        if ("aac".equals(audioStream.codecName().get())) {
            doAudio = false;
            break;
        }
    }
    if (!doVideo && !doAudio) {
        return;
    }
    OutputProcessor outputProcessor = builder.addMediaOutputFile(destinationFile);
    outputProcessor.qualityScale(0);
    // video
    for (Stream stream : videoMetadatas.getVideoStreams()) {
        outputProcessor.map().streams(stream).input(inputProcessor);
    }
    // audio
    for (Stream stream : audioStreams) {
        outputProcessor.map().streams(stream).input(inputProcessor);
    }
    // subtitle
    Collection<SubtitleStream> subtitleStreams = StreamOrder.sort(videoMetadatas.getSubtitleStreams());
    for (Stream stream : subtitleStreams) {
        outputProcessor.map().streams(stream).input(inputProcessor);
    }
    // other stream
    for (Stream stream : videoMetadatas.getStreams()) {
        Type type = stream.type();
        if (type != Type.AUDIO && type != Type.VIDEO && type != Type.SUBTITLE) {
            outputProcessor.map().streams(stream).input(inputProcessor);
        }
    }
    // ------------------------ disposition default ------------------------
    // 
    int count = 0;
    for (Stream stream : audioStreams) {
        boolean beDefault = count == 1;
        if (stream.isDefaultStream() != beDefault) {
            outputProcessor.metadataStream(Type.AUDIO, count, "disposition:default", beDefault ? "1" : "0");
        }
        ++count;
    }
    count = 0;
    for (Stream stream : subtitleStreams) {
        boolean beDefault = count == 1;
        if (stream.isDefaultStream() != beDefault) {
            outputProcessor.metadataStream(Type.SUBTITLE, count, "disposition:default", beDefault ? "1" : "0");
        }
        ++count;
    }
    // video
    if (doVideo) {
        outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(23));
    } else {
        outputProcessor.codecCopy(Type.VIDEO);
    }
    // audio
    if (doAudio) {
        outputProcessor.codecAutoSelectAAC();
    } else {
        outputProcessor.codecCopy(Type.AUDIO);
    }
    // subtitle
    if (videoMetadatas.contains(Type.SUBTITLE)) {
        outputProcessor.codecCopy(Type.SUBTITLE);
    }
    // outputProcessor.overwrite();
    FFExecutor<Object> executor = builder.build();
    System.out.println(executor.getCommandLine());
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) Type(org.fagu.fmv.ffmpeg.operation.Type) 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) File(java.io.File)

Example 4 with MovieMetadatas

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

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

the class MovieFinder method flushToMap.

// *****************************************
/**
 * @see org.fagu.fmv.utils.file.FileFinder#flushToMap(java.util.List, java.util.function.Consumer)
 */
@Override
protected Future<Map<FileFound, InfosFile>> flushToMap(List<FileFound> buffer, Consumer<List<FileFound>> consumer) {
    Map<FileFound, InfosFile> outMap = new LinkedHashMap<>(buffer.size());
    for (FileFound fileFound : buffer) {
        File file = fileFound.getFileFound();
        try {
            MovieMetadatas movieMetadatas = FFHelper.videoMetadatas(file);
            Map<FileFound, InfosFile> flushMap = Collections.singletonMap(fileFound, new InfosFile(new Movie(file, movieMetadatas)));
            outMap.putAll(flushMap);
            if (consumer != null) {
                consumer.accept(Collections.singletonList(fileFound));
            }
            afterFlush(flushMap);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return new DoneFuture<>(outMap);
}
Also used : Movie(org.fagu.fmv.mymedia.classify.movie.Movie) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) DoneFuture(org.fagu.fmv.utils.file.DoneFuture) IOException(java.io.IOException) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

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