KotlinFFMpeg icon indicating copy to clipboard operation
KotlinFFMpeg copied to clipboard

Question: How to merge audio&video files (mux) so that the audio will loop if needed?

Open AndroidDeveloperLB opened this issue 6 years ago • 2 comments

Using this library (or others), is it possible to merge audio&video together, so that the output video will be of the same duration as the input video, and have the audio loop if it's shorter than the video?

I've asked about it here too: https://stackoverflow.com/q/54769976/878126

AndroidDeveloperLB avatar Feb 24 '19 14:02 AndroidDeveloperLB

I tried to use this: https://superuser.com/a/1319950/152400

by writing this in AudioVideoMerger class:

val cmd = arrayOf("-i", video!!.path, "-filter_complex", "amovie=${audio!!.path}:loop=0,asetpts=N/SR/TB[aud];[0:a][aud]amix[a]", "-map", "0:v", "-map", "[a]", "-c:v", "copy", "-c:a", "aac", "-b:a", "256k", "-shortest", outputLocation.path)

It worked, but the audio of the video file remained, so both are playing. Also, it's quite slow in making the output file... :(

AndroidDeveloperLB avatar Feb 24 '19 15:02 AndroidDeveloperLB

Never mind, got it:

    val cmd = arrayOf("-i", video!!.absolutePath, "-stream_loop", "-1", "-i", audio!!.absolutePath, "-c:v", "copy","-c:a","copy", "-map", "0:v:0", "-map", "1:a:0", "-shortest", outputLocation.absolutePath)

It sometimes work really well, but for some reason the sample project can't play the output file. In some other cases, the output video gets to be as short as the audio...

How come?

I tried this too, but it didn't help:

    val cmd = arrayOf("-i", video!!.absolutePath, "-stream_loop", "-1", "-i", audio!!.absolutePath, "-c:v", "copy", "-c:a", "copy", "-map", "0:v:0", "-map", "1:a:0", "-vprofile", "baseline",
            "-movflags", "faststart", "-shortest", outputLocation.absolutePath)

If I remove the "-c:a","copy" , it will succeed playing, but also will take a long time to make the file...

AndroidDeveloperLB avatar Feb 25 '19 09:02 AndroidDeveloperLB