简体   繁体   中英

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

You have to implement MediaRecorder.OnInfoListener to manually stop the recording at 10 seconds. Once its stopped, the MediaRecorder goes back to the initial state and the setup has to be done again to start back recording.

public class VideoCapture extends Activity implements MediaRecorder.OnInfoListener { 

   public void startVideoRecording() {
      // Normal MediaRecorder Setup
      recorder.setMaxDuration(10000); // 10 seconds
      recorder.setOnInfoListener(this); // very important
   }

   public void onInfo(MediaRecorder mrc, int mri, int extra) { 
      if (mri == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
         Log.v("VIDEOCAPTURE","10 seconds"); 
         mrc.stop();
      }
   }
}

Now, For the Progress bar you can use a Timer .

//fires once a second, decrease this to fire more frequently
private static final int TIMER_FREQ = 1000; 

final ProgressBar progressBar = new ProgressBar(this); //where this is a Context
progressBar.setMax(10000);

Timer progressBarAdvancer = new Timer();
progressBarAdvancer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            progressBar.setProgress(progressBar.getProgress() + TIMER_FREQ);
        }
    },
    0, //Delay before first execution
    TIMER_FREQ); 

By doing this, the progressBar operates on a separate thread from the recording, but will finish within the required 10 seconds. At this pount you can stop the recording and do the rest of the things.

Also, you can use the Video Trimmer based on "k4l-video-trimmer" library which handle various issues on the k4l-video-trimmer.

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
}

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