Search code examples
javaandroidvideomp4trim

Android video trimming library


I am doing Video Triming and using k4l-video-trimmer library. I am getting an issue. I have downloaded the latest code and integrate it on Android Studio. When i select a video, k4l-video-trimmer successfully prepared the video and correctly shows video info and snapshots. I have set the max duration to 10 sec but when move the progressbar to crop the video at specific duration, the cropping duration which is showing on screen like (01:21 sec - 01:31 sec) for 10 sec will change to (01:21 sec - 01:36 sec) becomes 15 sec duration that is an issue and when I crop the video, it will crop it for 23 sec. I don't know how to resolve this issue. Please help me to resolve this issue


Solution

  • You can use mobile-ffmpeg Supports API Level 16+

    fun scaleVideo(path: String, destinationFilePath: String) {
        _loaderVisisble.value = true
        viewModelScope.launch {
            val cmd = arrayOf(
                "-i",
                path,
                "-vf",
                "scale=576:1024:force_original_aspect_ratio=decrease",
                destinationFilePath
            )
            Log.v("str_Cmd", cmd.toString() + "")
            val status = executeCommand(cmd)
            when (status) {
                FFmpeg.RETURN_CODE_SUCCESS -> {
                    _loaderVisisble.value = false
                    val mergedFile = File(destinationFilePath)
                    Log.v(
                        "target_file_size",
                        (mergedFile.length() / 1024).toString().toInt().toString() + ""
                    )
                    onVideoScaleListener.postValue(destinationFilePath)
                }
                FFmpeg.RETURN_CODE_CANCEL -> {
                    _loaderVisisble.value = false
                }
                else -> {
                    _loaderVisisble.value = false
                }
            }
        }
    }
    
    
    private suspend fun executeCommand(cmd: Array<String>): Int {
        var status = -1
        withContext(Dispatchers.Default) {
            val rc = FFmpeg.execute(cmd)
            when (rc) {
                FFmpeg.RETURN_CODE_SUCCESS -> {
                    Log.i(
                        Config.TAG,
                        "Command execution completed successfully."
                    )
                }
                FFmpeg.RETURN_CODE_CANCEL -> {
                    Log.i(
                        Config.TAG,
                        "Command execution cancelled by user."
                    )
                }
                else -> {
                    Log.i(
                        Config.TAG,
                        String.format(
                            "Command execution failed with rc=%d and the output below.",
                            rc
                        )
                    )
                }
            }
            status = rc
        }
        return status
    }