jave2 icon indicating copy to clipboard operation
jave2 copied to clipboard

It seems that filters cannot be used at the same time

Open AGPEIM opened this issue 4 years ago • 6 comments

i need to add 2 filters and i tried this:

videoAttributes.addFilter(scaleFilter);
videoAttributes.addFilter(subtitleFilter);

but only the below one works, Log says

[Thread-9] INFO ws.schild.jave.ConversionOutputAnalyzer - Unhandled message in step: 1 Line: 15 message: <Only '-vf ass='/Users/lius/Desktop/t_sub.ass'' read, ignoring remaining -vf options: Use ',' to separate filters>
[Thread-9] INFO ws.schild.jave.ConversionOutputAnalyzer - Unhandled message in step: 1 Line: 16 message: <Only '-af (null)' read, ignoring remaining -af options: Use ',' to separate filters>

I don’t know if the method I’m using is incorrect or it’s a bug

AGPEIM avatar May 05 '21 12:05 AGPEIM

Could you please provide me with a full example? Perhpas swapping the filter order changes behaviour?

a-schild avatar May 05 '21 13:05 a-schild

here's the full example

public void encodeVideo(){
        File subtitle=new File("/Users/lius/Desktop/1.ass");
        File video=new File("/Users/lius/Desktop/1.flv");
        File target=new File("/Users/lius/Desktop/1.mp4");

        Encoder encoder=new Encoder();
        EncodingAttributes attributes=new EncodingAttributes();
        AudioAttributes audioAttributes=new AudioAttributes();
        VideoAttributes videoAttributes=new VideoAttributes();
        audioAttributes.setCodec(AudioAttributes.DIRECT_STREAM_COPY);

        AssSubtitlesFilter filter=new AssSubtitlesFilter(subtitle);
        videoAttributes.addFilter(filter);

        VideoSize size=new VideoSize(1080,720);
        ScaleFilter scaleFilter=new ScaleFilter(size);
        videoAttributes.addFilter(scaleFilter);

        attributes.setAudioAttributes(audioAttributes);
        attributes.setVideoAttributes(videoAttributes);
        try {
            encoder.encode(new MultimediaObject(video),target,attributes);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }

i have tried swapping the filter order,only the below one works,for example,If the scaleFilter is added after, the video will only scaled without adding subtitles. If the SubtitleFilter is added later, the video will only add subtitles but not scaled.

AGPEIM avatar May 05 '21 13:05 AGPEIM

Thanks for testing this. Yep, it seems that we need to build another argument list, when multiple filters need to be applied in one pass.

https://stackoverflow.com/questions/6195872/applying-multiple-filters-at-once-with-ffmpeg

Something like this is required -vf "[in] scale=iw/2:ih/2, pad=iw+40:ih+40:10:10 [top]; movie=yourLogoOrVideo.pngOraviEtc, scale=iw/2:ih/2 , fade=out:400:40:alpha=1 [bottom]; [top][bottom] overlay=PaddingFromTop:PaddingFromLeft [out]"

At the moment each filter added ads an -vf .... to the command line, need to contact them together (and make sure the escaping of special chars still works)

a-schild avatar May 05 '21 15:05 a-schild

@mressler Hello Michael, have you time to look into this one? (I have not enough stream experience to do the modification)

In Encoder.java on lines 391+ we have this stream processing

          new SimpleArgument(ArgType.OUTFILE,
              ea -> ea.getVideoAttributes()
                      .map(VideoAttributes::getVideoFilters)
                      .map(Collection::stream)
                      .map(s -> s.flatMap(vf -> Stream.of("-vf", vf.getExpression())))
                      .orElseGet(Stream::empty)),

When two video filters are passed to the Encoder, then it generates this command arguments:

-vf filterExpression2 -vf FilterExpression2

But for ffmpeg, we need only one -vf argument, and then contact the expressions with "; " so it should generate

-vf filterExpression2; FilterExpression2

a-schild avatar May 06 '21 15:05 a-schild

Should be fixed in the 3.2.0-SNAPSHOT release. Could you please test it?

a-schild avatar May 20 '21 10:05 a-schild

Sorry, I missed the @mention. You likely want to set a complex filtergraph. That's what FFMPEG calls it in the documentation, and that's what I added early in 3.0.

But that's a good change to support multiple filters.

mressler avatar May 20 '21 10:05 mressler