Search in sources :

Example 11 with VideoStream

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

the class FileCache method scaleForMake.

/**
 * @param inFile
 * @throws IOException
 */
private void scaleForMake(File inFile) throws IOException {
    OutputInfos outputInfos = project.getOutputInfos();
    Size makeSize = outputInfos.getSize();
    MovieMetadatas metadatas = MovieMetadatas.with(inFile).extract();
    VideoStream videoStream = metadatas.getVideoStream();
    if (videoStream == null) {
        return;
    }
    Size videoSize = videoStream.size();
    if (videoSize.equals(makeSize)) {
        return;
    }
    File makeTmpFile = File.createTempFile(inFile.getName() + "-", "." + FilenameUtils.getExtension(inFile.getName()), inFile.getParentFile());
    try {
        makeTmpFile.delete();
        if (!inFile.renameTo(makeTmpFile)) {
            throw new IOException("Unable to rename " + inFile + " to " + makeTmpFile);
        }
        File toFile = inFile;
        FFMPEGExecutorBuilder builder = FFUtils.builder(project);
        // input
        builder.addMediaInputFile(makeTmpFile);
        // filter
        builder.filter(Scale.to(makeSize, ScaleMode.fitToBox()));
        // output
        builder.addMediaOutputFile(toFile).format(outputInfos.getFormat()).overwrite();
        FFExecutor<Object> build = builder.build();
        build.execute();
    } finally {
        makeTmpFile.delete();
    }
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Size(org.fagu.fmv.utils.media.Size) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) OutputInfos(org.fagu.fmv.core.project.OutputInfos) IOException(java.io.IOException) File(java.io.File)

Example 12 with VideoStream

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

the class FFHelper method concatFade.

/**
 * @param in1VideoFile
 * @param in2VideoFile
 * @param fadeDuration
 * @param outFile
 * @throws IOException
 */
public static void concatFade(File in1VideoFile, File in2VideoFile, Duration fadeDuration, File outFile) throws IOException {
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    InputProcessor video1InputProcessor = builder.addMediaInputFile(in1VideoFile);
    InputProcessor video2InputProcessor = builder.addMediaInputFile(in2VideoFile);
    VideoStream videoStream1 = video1InputProcessor.getMovieMetadatas().getVideoStream();
    VideoStream videoStream2 = video2InputProcessor.getMovieMetadatas().getVideoStream();
    Optional<Duration> duration1 = videoStream1.duration();
    Time startTimeT1 = Time.valueOf(duration1.get().toSeconds() - fadeDuration.toSeconds());
    Duration duration0T1 = Duration.valueOf(startTimeT1.toSeconds());
    Optional<Duration> duration2 = videoStream2.duration();
    Time startTimeT2 = Time.valueOf(duration2.get().toSeconds() - fadeDuration.toSeconds());
    Duration durationT2END = Duration.valueOf(startTimeT2.toSeconds());
    // source 1: video
    NullSourceVideo nullSourceVideo1 = NullSourceVideo.build().size(videoStream1.size()).duration(durationT2END);
    Concat concat1V = Concat.create(builder, video1InputProcessor, FilterComplex.create(nullSourceVideo1)).countVideo(1).countAudio(0).countInputs(2);
    // source 1: audio
    AudioGenerator audioGenerator1 = AudioGenerator.build().silence().duration(durationT2END);
    Concat concat1A = Concat.create(builder, video1InputProcessor, FilterComplex.create(audioGenerator1)).countVideo(0).countAudio(1).countInputs(2);
    FilterComplex fadeAudio1 = FilterComplex.create(FadeAudio.out().startTime(startTimeT1).duration(fadeDuration)).addInput(concat1A);
    // source 2: video
    NullSourceVideo nullSourceVideo2 = NullSourceVideo.build().size(videoStream2.size()).duration(duration0T1);
    Concat concat2V = Concat.create(builder, FilterComplex.create(nullSourceVideo2), video2InputProcessor).countVideo(1).countAudio(0).countInputs(2);
    // source 2: audio
    AudioGenerator audioGenerator2 = AudioGenerator.build().silence().duration(duration0T1);
    Concat concat2A = Concat.create(builder, FilterComplex.create(audioGenerator2), video2InputProcessor).countVideo(0).countAudio(1).countInputs(2);
    FilterComplex fadeAudio2 = FilterComplex.create(FadeAudio.in().startTime(startTimeT1).duration(fadeDuration)).addInput(concat2A);
    // blend / merge video
    SetSAR setSAR = SetSAR.toRatio("1");
    Format formatRGBA = Format.with(PixelFormat.RGBA);
    FilterComplex vfc1 = FilterComplex.create(setSAR, formatRGBA).addInput(concat1V);
    FilterComplex vfc2 = FilterComplex.create(setSAR, formatRGBA).addInput(concat2V);
    Blend blend = Blend.build().mode(Mode.ADDITION).repeatLast(true).opacity(1).exprFade(startTimeT1, fadeDuration);
    Format formatYUV = Format.with(PixelFormat.YUVA422P10LE);
    FilterComplex vfcBlend = FilterComplex.create(blend, formatYUV).addInput(vfc1).addInput(vfc2);
    builder.filter(vfcBlend);
    // merge audio
    FilterComplex audioMix = AudioMix.build().duration(MixAudioDuration.SHORTEST).addInput(fadeAudio1).addInput(fadeAudio2);
    builder.filter(audioMix);
    // out
    builder.addMediaOutputFile(outFile).overwrite();
    FFExecutor<Object> executor = builder.build();
    executor.execute();
}
Also used : FilterComplex(org.fagu.fmv.ffmpeg.filter.FilterComplex) FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) SetSAR(org.fagu.fmv.ffmpeg.filter.impl.SetSAR) Blend(org.fagu.fmv.ffmpeg.filter.impl.Blend) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) Duration(org.fagu.fmv.utils.time.Duration) MixAudioDuration(org.fagu.fmv.ffmpeg.filter.impl.AudioMix.MixAudioDuration) Time(org.fagu.fmv.utils.time.Time) Concat(org.fagu.fmv.ffmpeg.filter.impl.Concat) NullSourceVideo(org.fagu.fmv.ffmpeg.filter.impl.NullSourceVideo) PixelFormat(org.fagu.fmv.ffmpeg.utils.PixelFormat) Format(org.fagu.fmv.ffmpeg.filter.impl.Format) AudioGenerator(org.fagu.fmv.ffmpeg.filter.impl.AudioGenerator)

Example 13 with VideoStream

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

the class FFHelper method extractThumbnails2JPEGS.

/**
 * @param inFile
 * @param outFolder
 * @param countFrame
 * @return
 * @throws IOException
 */
public static ExtractThumbnail extractThumbnails2JPEGS(File inFile, File outFolder, int countFrame) throws IOException {
    // extract images
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    // global
    builder.hideBanner();
    builder.logLevel(LogLevel.INFO);
    // input
    InputProcessor inputProcessor = builder.addMediaInputFile(inFile);
    MovieMetadatas videoMetadatas = inputProcessor.getMovieMetadatas();
    VideoStream videoStream = videoMetadatas.getVideoStreams().get(0);
    if (!outFolder.exists()) {
        outFolder.mkdirs();
    }
    // filter
    SelectVideo selectVideo = SelectVideo.build().countFrame(videoStream, countFrame);
    builder.filter(selectVideo);
    // output
    builder.mux(Image2Muxer.to(new File(outFolder, "out%05d.jpg"))).videoSync(VSync.PASSTHROUGH).overwrite();
    FFExecutor<Object> executor = builder.build();
    executor.execute();
    return ExtractThumbnail.find(outFolder, "out\\d+\\.jpg");
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) SelectVideo(org.fagu.fmv.ffmpeg.filter.impl.SelectVideo) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) File(java.io.File)

Example 14 with VideoStream

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

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

VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)21 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)11 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)8 InputProcessor (org.fagu.fmv.ffmpeg.operation.InputProcessor)8 File (java.io.File)6 AudioStream (org.fagu.fmv.ffmpeg.metadatas.AudioStream)5 Duration (org.fagu.fmv.utils.time.Duration)5 OptionalInt (java.util.OptionalInt)4 FilterComplex (org.fagu.fmv.ffmpeg.filter.FilterComplex)4 AudioGenerator (org.fagu.fmv.ffmpeg.filter.impl.AudioGenerator)4 MixAudioDuration (org.fagu.fmv.ffmpeg.filter.impl.AudioMix.MixAudioDuration)4 Blend (org.fagu.fmv.ffmpeg.filter.impl.Blend)4 Concat (org.fagu.fmv.ffmpeg.filter.impl.Concat)4 Format (org.fagu.fmv.ffmpeg.filter.impl.Format)4 NullSourceVideo (org.fagu.fmv.ffmpeg.filter.impl.NullSourceVideo)4 SetSAR (org.fagu.fmv.ffmpeg.filter.impl.SetSAR)4 OutputProcessor (org.fagu.fmv.ffmpeg.operation.OutputProcessor)4 PixelFormat (org.fagu.fmv.ffmpeg.utils.PixelFormat)4 Size (org.fagu.fmv.utils.media.Size)4 Time (org.fagu.fmv.utils.time.Time)4