简体   繁体   中英

Looping video doesn't repeat audio FFMpeg

I try to overlay two videos. The first one is a background video with the audio track. For this, I use the following command

 command = arrayOf("-i", background, "-i", video,
       "-c:v", "libx264", "-preset", "ultrafast", "-filter_complex",
       "[1]crop=iw:ih/2:0:0[rgb];[1]crop=iw:ih/2:0:ih/2[alp];" +
        "[rgb][alp]alphamerge[va];[0]scale=$sw:$sh[bs];" +
        "[bs]loop=10:1000:0[bsl];[bsl][va]overlay=x=$startX:y=$startY:shortest=1",
        outPutPath)

But video plays the first cycle with audio, all the next cycles without. How to make video looping with audio?

The following command allows me to repeat audio.

 arrayOf("-i", A.mp4, "-i", B.mp4,
     "-c:v", "libx264", "-preset", "ultrafast", "-filter_complex",
     "amovie=$A.mp4:loop=3,asetpts=N/SAMPLE_RATE/TB[a];" +
     [0]scale=$videoWidth:$videoHeight[bs];[bs]loop=10:1000:0,setpts=N/FRAME_RATE/TB[bsl];" +
     "[bsl][1]overlay=x=$startX:y=$startY:shortest=1[v]", "-map",
     "[v]", "-map", "[a]", "-shortest", outPutPath)

In this case, video repeats two times and stuck but audio still playing. How do I trim the audio by length of the video?

Main issue is that you need to add aloop and -map the appropriate filter outputs:

command = arrayOf("-i", background, "-i", video,
       "-c:v", "libx264", "-preset", "ultrafast", "-filter_complex",
       "[1]crop=iw:ih/2:0:0[rgb];[1]crop=iw:ih/2:0:ih/2[alp];" +
        "[rgb][alp]alphamerge[va];[0]scale=$sw:$sh[bs];" +
        "[bs]loop=10:1000:0,setpts=N/FRAME_RATE/TB[bsl];[1:a]aloop=10:1000:0,asetpts=N/SAMPLE_RATE/TB[a];[bsl][va]overlay=x=$startX:y=$startY:shortest=1[v]",
        "-map", "[v]", "-map", "[a]",
        outPutPath)

You will need to adjust the size parameter in the aloop filter. 1000 is just a placeholder. See the filter documentation.

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