Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

While merge multiple videos how can we add transition between them? #20

Open
vandanabpatel opened this issue May 5, 2020 · 0 comments

Comments

@vandanabpatel
Copy link

Can you please help me out i wand to add transition between videos
i want to achive something like this
https://www.youtube.com/watch?v=INiRu2_XdJE
https://www.youtube.com/watch?v=V-lL1MTPkSQ

fun mergeWithFade() {

    if (videos == null || videos!!.isEmpty()) {
        callback!!.onFailure(IOException("File not exists"))
        return
    }

    for (v in videos!!) {
        if (!v.canRead()) {
            callback!!.onFailure(IOException("Can't read the file. Missing permission?"))
            return
        }
    }

    val outputLocation = getConvertedFile(outputPath, outputFileName)

    val inputCommand = arrayListOf<String>()

    //Add all paths
    for (i in videos!!) {
        inputCommand.add("-i")
        inputCommand.add(i.path)
    }
    //Apply filter graph
    inputCommand.add("-filter_complex")

    //Compose concatenation commands
    val stringBuilder = StringBuilder()

// stringBuilder.append("[1:v][0:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v0];");

    for (i in 0 until videos!!.size) {
        val fade = Constant.DURATION_FADE_TRANSITION;

        val mp = MediaPlayer.create(context, Uri.fromFile(videos!!.get(i)))
        val duration = mp.duration / 1000
        mp.release()
        val startDuration = duration - fade;

        stringBuilder.append("[$i:v]fade=type=out:duration=" + Constant.DURATION_FADE_TRANSITION + ":start_time=" + startDuration + ",setpts=PTS-STARTPTS,setsar=1,scale='min(480,iw)':min'(320,ih)':force_original_aspect_ratio=decrease,pad=480:320:(ow-iw)/2:(oh-ih)/2[v$i]; [$i:a]afade=type=out:duration=" + Constant.DURATION_FADE_TRANSITION + ":start_time=" + startDuration + ",asetpts=PTS-STARTPTS[a$i];");
    }

    for (i in 0 until videos!!.size) {
        stringBuilder.append("[v$i][a$i]")
    }

    // Concat command
    stringBuilder.append("concat=n=${videos!!.size}:v=1:a=1[v][a]")

    //Complete Command
    val cmd = arrayOf<String>(
            "-map",
            "[v]",
            "-map",
            "[a]",
            "-preset", //Presets can be ultrafast, superfast, veryfast, faster, fast, medium (default), slow and veryslow.
            "ultrafast", //Using a slower preset gives you better compression, or quality per file size.
            "-crf", //Constant Rate Factor
            "23", //Value from 0 to 51, 23 is default, Large Value for highest quality
            outputLocation.path,
            "-y" //Overwrite output files without asking
    )

    val finalCommand = (inputCommand + stringBuilder.toString() + cmd).toTypedArray()

    try {
        FFmpeg.getInstance(context).execute(finalCommand, object : ExecuteBinaryResponseHandler() {
            override fun onStart() {}

            override fun onProgress(message: String?) {
                callback!!.onProgress(message!!)
            }

            override fun onSuccess(message: String?) {
                refreshGallery(outputLocation.path, context)
                callback!!.onSuccess(outputLocation, OutputType.TYPE_VIDEO)

            }

            override fun onFailure(message: String?) {
                if (outputLocation.exists()) {
                    outputLocation.delete()
                }
                callback!!.onFailure(IOException(message))
            }

            override fun onFinish() {
                callback!!.onFinish()
            }
        })
    } catch (e: Exception) {
        callback!!.onFailure(e)
    } catch (e2: FFmpegCommandAlreadyRunningException) {
        callback!!.onNotAvailable(e2)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant