Search in sources :

Example 1 with AudioStream

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

the class MockMovieMetadatas method audio.

/**
 * @return
 */
public MockAudioStream audio() {
    AudioStream audioStream = mock(AudioStream.class);
    doReturn(audioStream).when(movieMetadatas).getAudioStream();
    addStream(audioStream, Type.AUDIO, audioStreams);
    return new MockAudioStream(audioStream);
}
Also used : AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream)

Example 2 with AudioStream

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

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

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

the class FFReducer method reduceVideo.

/**
 * @param metadatas
 * @param srcFile
 * @param movieMetadatas
 * @param destFile
 * @param consolePrefixMessage
 * @param logger
 * @throws IOException
 */
private boolean reduceVideo(MovieMetadatas metadatas, File srcFile, File destFile, String consolePrefixMessage, Logger logger) throws IOException {
    AudioStream audioStream = metadatas.getAudioStream();
    boolean audioCodecCopy = audioStream.isCodec(Formats.AC3);
    FFMPEGExecutorBuilder builder = FFMPEGExecutorBuilder.create();
    builder.hideBanner();
    InputProcessor inputProcessor = builder.addMediaInputFile(srcFile);
    builder.filter(AutoRotate.create(metadatas));
    applyScaleIfNecessary(builder, metadatas, getMaxSize(), logger);
    VolumeDetect volumeDetect = null;
    if (!audioCodecCopy) {
        volumeDetect = VolumeDetect.build();
        builder.filter(volumeDetect);
    }
    CropDetect cropDetect = CropDetect.build();
    builder.filter(cropDetect);
    MovieMetadatas videoMetadatas = inputProcessor.getMovieMetadatas();
    Collection<AudioStream> audioStreams = StreamOrder.sort(videoMetadatas.getAudioStreams());
    OutputProcessor outputProcessor = builder.addMediaOutputFile(destFile);
    outputProcessor.qualityScale(0);
    // video
    for (Stream stream : videoMetadatas.getVideoStreams()) {
        logger.log("map[" + stream.index() + "] video: " + stream);
        outputProcessor.map().streams(stream).input(inputProcessor);
    }
    // audio
    for (Stream stream : audioStreams) {
        logger.log("map[" + stream.index() + "] audio: " + stream);
        outputProcessor.map().streams(stream).input(inputProcessor);
    }
    // subtitle
    Collection<SubtitleStream> subtitleStreams = StreamOrder.sort(videoMetadatas.getSubtitleStreams());
    for (Stream stream : subtitleStreams) {
        logger.log("map[" + stream.index() + "] subtitle: " + stream);
        outputProcessor.map().streams(stream).input(inputProcessor);
    }
    // other stream (Apple... again bullshit)
    // for (Stream stream : videoMetadatas.getStreams()) {
    // Type type = stream.type();
    // if (type != Type.AUDIO && type != Type.VIDEO && type != Type.SUBTITLE) {
    // logger.log("map other stream: " + stream);
    // outputProcessor.map().streams(stream).input(inputProcessor);
    // }
    // }
    // -------------------------- codec -------------------------
    outputProcessor.codec(H264.findRecommanded().strict(Strict.EXPERIMENTAL).quality(crf));
    // audio
    if (audioCodecCopy) {
        logger.log("Audio: AC3, copy");
        outputProcessor.codecCopy(Type.AUDIO);
    } else {
        logger.log("Audio: force AAC");
        outputProcessor.codecAutoSelectAAC();
    }
    // subtitle
    if (videoMetadatas.contains(Type.SUBTITLE)) {
        outputProcessor.codecCopy(Type.SUBTITLE);
    }
    outputProcessor.overwrite();
    FFExecutor<Object> executor = builder.build();
    executor.addListener(createLogFFExecListener(logger));
    executor.addListener(createCropDetectFFExecListener(logger, cropDetect, videoMetadatas));
    if (!audioCodecCopy) {
        executor.addListener(createVolumeDetectFFExecListener(logger, volumeDetect));
    }
    VideoStream videoStream = metadatas.getVideoStream();
    OptionalInt countEstimateFrames = videoStream.countEstimateFrames();
    Progress progress = executor.getProgress();
    if (countEstimateFrames.isPresent() && progress != null) {
        textProgressBar = FFMpegProgressBar.with(progress).byFrame(countEstimateFrames.getAsInt()).fileSize(srcFile.length()).build().makeBar(consolePrefixMessage);
    } else {
        StringJoiner joiner = new StringJoiner(", ");
        if (progress == null) {
            joiner.add("progress not found");
        }
        if (!countEstimateFrames.isPresent()) {
            joiner.add("nb frames nout found");
        }
        logger.log("No progress bar: " + joiner.toString());
    }
    executor.execute();
    Optional<String> codecName = videoStream.codecName();
    if (codecName.isPresent() && codecName.get().equalsIgnoreCase(Formats.HEVC.getName())) {
        // h265
        return true;
    }
    return false;
}
Also used : FFMPEGExecutorBuilder(org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder) Progress(org.fagu.fmv.ffmpeg.operation.Progress) VolumeDetect(org.fagu.fmv.ffmpeg.filter.impl.VolumeDetect) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) InputProcessor(org.fagu.fmv.ffmpeg.operation.InputProcessor) OptionalInt(java.util.OptionalInt) CropDetect(org.fagu.fmv.ffmpeg.filter.impl.CropDetect) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) MovieMetadatas(org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas) OutputProcessor(org.fagu.fmv.ffmpeg.operation.OutputProcessor) Stream(org.fagu.fmv.ffmpeg.metadatas.Stream) SubtitleStream(org.fagu.fmv.ffmpeg.metadatas.SubtitleStream) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream) AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) StringJoiner(java.util.StringJoiner)

Example 5 with AudioStream

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

the class FFReducer method needToReduceAudio.

/**
 * @param metadatas
 * @param srcFile
 * @return
 */
private boolean needToReduceAudio(MovieMetadatas metadatas, File srcFile) {
    AudioStream audioStream = metadatas.getAudioStream();
    String extension = FilenameUtils.getExtension(srcFile.getName());
    if (!audioFormat.equalsIgnoreCase(extension) || audioStream.bitRate().getAsInt() > 128000) {
        return true;
    }
    VideoStream videoStream = metadatas.getVideoStream();
    if (videoStream != null && Decoders.MJPEG.getName().equals(videoStream.codecName().get())) {
        // has cover
        return true;
    }
    return false;
}
Also used : AudioStream(org.fagu.fmv.ffmpeg.metadatas.AudioStream) VideoStream(org.fagu.fmv.ffmpeg.metadatas.VideoStream)

Aggregations

AudioStream (org.fagu.fmv.ffmpeg.metadatas.AudioStream)9 MovieMetadatas (org.fagu.fmv.ffmpeg.metadatas.MovieMetadatas)5 VideoStream (org.fagu.fmv.ffmpeg.metadatas.VideoStream)5 Stream (org.fagu.fmv.ffmpeg.metadatas.Stream)4 OptionalInt (java.util.OptionalInt)3 StringJoiner (java.util.StringJoiner)3 FFMPEGExecutorBuilder (org.fagu.fmv.ffmpeg.executor.FFMPEGExecutorBuilder)3 SubtitleStream (org.fagu.fmv.ffmpeg.metadatas.SubtitleStream)3 InputProcessor (org.fagu.fmv.ffmpeg.operation.InputProcessor)3 OutputProcessor (org.fagu.fmv.ffmpeg.operation.OutputProcessor)3 File (java.io.File)2 Progress (org.fagu.fmv.ffmpeg.operation.Progress)2 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 CropDetect (org.fagu.fmv.ffmpeg.filter.impl.CropDetect)1 VolumeDetect (org.fagu.fmv.ffmpeg.filter.impl.VolumeDetect)1 Type (org.fagu.fmv.ffmpeg.operation.Type)1 ImageMetadatas (org.fagu.fmv.im.ImageMetadatas)1 FileType (org.fagu.fmv.media.FileType)1 Size (org.fagu.fmv.utils.media.Size)1