简体   繁体   English

ffmpeg叠加视频无法正常工作

[英]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). 我想做的是:我有2个视频(第一个视频记录在应用程序本身中,第二个视频是预先录制的,并且比第一个短)。 I'm trying to overlay it with the following FFMpeg command: 我正在尝试使用以下FFMpeg命令覆盖它:

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. 因此:vid1是在应用程序中录制的mp4视频,水印是应该在所有vid1期间叠加的png图像,而vid2是预先录制的一个。 And the difference in length between vid1 and vid2 depends on the user. vid1和vid2之间的长度差异取决于用户。 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. 因此,我需要在80.5秒处将vid2分成2部分,然后将第一部分vid2从vid1的开始到用户单击按钮的时间覆盖起来(为方便起见,这里将其硬编码为84 ,我知道从80.5到84最后一帧将保持覆盖状态-可以),然后从“ 84”开始覆盖第二块直到结束。 (it's almost the same for aud2 - the corresponding audio for vid2 - but aud2 works fine) (与aud2几乎相同-vid2的相应音频-但aud2正常工作)

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. 但是,使用这段代码似乎会占用大量内存,并且丢失了许多覆盖vid2的帧-它只是冻结了。 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?) + "[outv6][vid6] overlay=10:10:enable=\\'between(t,84,101)\\' [outv]",破坏了所有内容,因此从84到最后,包括水印在内的所有内容都没有被覆盖 (为什么这样?)

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). IDK如何导出720p或1080p的视频,并且它不会太慢,文件大小不会太大且不能以OOM停止(现在大约为480p且大约需要50Mb-我想要更大的分辨率和更小的尺寸) 。

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]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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