Skip to content

Commit

Permalink
update crop params
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghai committed Jul 7, 2022
1 parent 473c5fb commit 1432066
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:testOnly="false"
android:name=".App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
Expand Down
12 changes: 8 additions & 4 deletions ffmpeg/src/main/java/com/an/ffmpeg/code/VideoCrop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.an.ffmpeg.code

import android.app.Application
import android.content.Context
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -74,17 +75,20 @@ object VideoCrop {
start: Int,
duration: Int
) = flow {

val frameRate = 24
val width = 720
val height = 720
//-ss 0 -t 5 时间裁切
//-strict -2 -vf crop=500:500:0:100 尺寸裁切
val cmd = (context.applicationInfo.nativeLibraryDir
+ "/ffmpeg.so"
+ " -y -i "
+ "" + srcFilePath + "" //加引号避免名字有空格无法识别
+ " -ss " + start
+ " -t " + duration // + " -strict -2 -vf crop=" + width + ":" + height + ":" + x + ":" + y + " -preset fast "
+ " -t " + duration
+ " -vf scale=w=$width:h=$height:force_original_aspect_ratio=decrease "
// + " -c copy " //直接复制一段,不能与-r一起使用
+ " -r 24 " //帧数修改为24
+ " -r $frameRate " //帧数修改为24
+ "" + destFilePath + "") //加引号避免名字有空格无法识别

val resultCode = try {
Expand All @@ -94,6 +98,7 @@ object VideoCrop {
var count = 0
while (errorScanner.hasNextLine()) {
val line = errorScanner.nextLine()
Log.d(TAG, line)
++count
val fz = count
val fm = duration * 1000 / 100
Expand All @@ -106,7 +111,6 @@ object VideoCrop {
e.printStackTrace()
200
}

if (resultCode == 0) {
emit(VideoCropStatus.Loading(100))
emit(VideoCropStatus.Success(destFilePath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class VideoCropViewModel(app: Application) : AndroidViewModel(app) {
viewModelScope.launch(Dispatchers.IO) {
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
val outputName = "videoCrop_$timeStamp.mp4"
val destPath = File(getApplication<Application>().filesDir, outputName).absolutePath
val destPath = File(getApplication<Application>().filesDir, outputName).apply { createNewFile() }.absolutePath
val duration = endSec - startSec

VideoCrop.cropVideo(getApplication(), srcVideoPath, destPath, startSec, duration)
Expand Down
Binary file added ffmpeg/src/main/jniLibs/x86_64/ffmpeg.so
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official
android.injected.testOnly=false

0 comments on commit 1432066

Please sign in to comment.