简体   繁体   中英

android - ffmpeg overlay videos not working properly

What I'm trying to do: I have 2 videos (the first one is recorded in the app itself, the second was pre-recorded and it's shorter than the first). I'm trying to overlay it with the following FFMpeg command:

finalCommand = new String[]{"-y",
                "-i", vid1,
                "-i", vid2,
                "-i", aud1,
                "-i", aud2,
                "-strict", "experimental", "-filter_complex",
                        "[0:a] volume=0.2 [a8];"
                        +"[3:a] atrim=end="+String.valueOf(newTimePause)+" [a12];"
                        +"[3:a] atrim=start="+String.valueOf(newTimePause)+" [a14];"
                        +"[a14] adelay="+String.valueOf(newDelayInt)+"|"
                        +String.valueOf(newDelayInt)+" [a16];"
                        + "[a8][a12][a16][2:a:0] amix=inputs=4 [a];"
                        + "movie=" + watermark + " [watermark];"
                        + "[0:v][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [outv0];"
                        + "[1] fifo, scale=iw/2.5:ih/2.5 [vid2];"
                        + "[vid2]fifo, trim=end=80.5 [vid5];"
                        + "[vid2] fifo, trim=start=80.5 [vid6];"
                        + "[outv0][vid5] overlay=10:10:enable=\'between(t,0,84)\' [outv6];"
                        + "[outv6][vid6] overlay=10:10:enable=\'between(t,84,101)\' [outv]",
                "-map", "[outv]",
                "-map", "[a]",
                "-r", "30",
                "-b",
                "4000k",
                "-vcodec", "mpeg4",
                "-ab", "44100", "-ac", "2", "-ar", "44100",
                IOUtils.getFINALdir(this)+"/finalvid.mp4"
        };

So: vid1 is an mp4 video recorded in the app, watermark is a png image that should be overlayed during all vid1, and vid2 is the one pre-recorded. And the difference in length between vid1 and vid2 depends on the user. So I need to split vid2 at 80.5 seconds into 2 pieces, then overlay the first piece of vid2 from the beginning of vid1 to the time user clicked the button (here it's hardcoded as 84 for convenience, and I know that from 80.5 to 84 the last frame will remain overlaid - it's ok), then overlay the second piece from this "84" till the end. (it's almost the same for aud2 - the corresponding audio for vid2 - but aud2 works fine)

But with this piece of code it looks like it takes a lot of memory and lots of frames of overlaid vid2 just get lost - it just freezes. And the line + "[outv6][vid6] overlay=10:10:enable=\\'between(t,84,101)\\' [outv]", ruins everything so that from 84 till the end nothing is finally overlaid including the watermark (why so?)

And the other problem is this is too slow. Idk how to export a video of 720p or 1080p and that it be not too slow, not be of a big file size and not stop with OOM (now it's about 480p and takes about 50Mb - I want a larger resolution with a smaller size).

I think the whole command can be rewritten better, but how? What am I doing wrong and how to fix it all? Thanks in advance!

Try this for the video part:

                    + "movie=" + watermark + " [watermark];"
                    + "[1]scale=iw/2.5:ih/2.5,split[vid2a][vid2b];"
                    + "[vid2a]trim=end=80.5,fifo[vid2a-ol];"
                    + "[vid2b]trim=start=80.5,setpts=PTS+3.5/TB,fifo[vid2b-ol];"
                    + "[0][vid2a-ol]overlay=10:10:enable=\'between(t,0,84)\',fifo[outv1];"
                    + "[outv1][vid2b-ol]overlay=10:10:enable=\'between(t,84,101)\',fifo[outv12];",
                    + "[outv12][watermark]overlay=main_w-overlay_w-10:main_h-overlay_h-10 [outv0]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM