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

添加视频画面裁剪功能 #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class KFFmpegCommandActivity : AppCompatActivity() {
36 -> hls2Video()
37 -> audio2Amr()
38 -> makeMuteAudio()
39 -> cropVideoScreen()
}
}
})
Expand Down Expand Up @@ -530,6 +531,13 @@ class KFFmpegCommandActivity : AppCompatActivity() {
}
}

private fun cropVideoScreen() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.cropVideoScreen(mVideoPath, targetPath, 500, 500, 0, 100), callback("裁切视频画面成功", targetPath))
}
}

private fun callback(msg: String, targetPath: String?): CommonCallBack? {
return object : CommonCallBack() {
override fun onStart() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<item>合成hls-视频</item>
<item>音频转码amr</item>
<item>生成静音音频</item>
<item>视频画面裁切</item>
</string-array>

</resources>
16 changes: 16 additions & 0 deletions ffmpeg/src/main/java/com/coder/ffmpeg/utils/FFmpegUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -913,4 +913,20 @@ object FFmpegUtils {
command = String.format(command, src,url)
return command.split(" ").toTypedArray()
}

/**
* 使用ffmpeg进行视频画面裁切
* @param srcFile 源文件
* @param targetFile 目标文件
* @param width 输出视频宽度
* @param height 输出视频高度
* @param x 裁切视频的基准点x坐标
* @param y 裁切视频的基准点y坐标
*/
@JvmStatic
fun cropVideoScreen(srcFile: String?, targetFile: String?, width: Int, height: Int, x: Int, y: Int): Array<String?> {
var command = "ffmpeg -i %s -vf crop=%d:%d:%d:%d %s -y"
command = String.format(command, srcFile, width, height, x, y, targetFile)
return command.split(" ").toTypedArray()
}
}