-
Notifications
You must be signed in to change notification settings - Fork 155
/
KFFmpegCommandActivity.kt
585 lines (511 loc) · 21.5 KB
/
KFFmpegCommandActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package com.coder.ffmpegtest.ui
import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.coder.ffmpeg.annotation.Direction
import com.coder.ffmpeg.annotation.ImageFormat
import com.coder.ffmpeg.annotation.MediaAttribute
import com.coder.ffmpeg.annotation.Transpose
import com.coder.ffmpeg.call.CommonCallBack
import com.coder.ffmpeg.jni.FFmpegCommand
import com.coder.ffmpeg.utils.FFmpegUtils
import com.coder.ffmpegtest.R
import com.coder.ffmpegtest.model.CommandBean
import com.coder.ffmpegtest.ui.adapter.FFmpegCommandAdapter
import com.coder.ffmpegtest.ui.dialog.PromptDialog
import com.coder.ffmpegtest.ui.dialog.PromptDialog.OnPromptListener
import com.coder.ffmpegtest.utils.FileUtils
import com.coder.ffmpegtest.utils.ToastUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
import java.util.*
/**
*
* @author: AnJoiner
* @datetime: 20-1-23
*/
class KFFmpegCommandActivity : AppCompatActivity() {
private var mAudioPath: String? = null
private var mVideoPath: String? = null
private var targetPath: String? = null
private var mAudioBgPath: String? = null
private var mImagePath: String? = null
private var tvContent: TextView? = null
private var mRecyclerView: RecyclerView? = null
private var mAdapter: FFmpegCommandAdapter? = null
private var mErrorDialog: PromptDialog? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ffmpeg_command)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
100)
}
init()
}
private fun init() {
initView()
initData()
initListener()
}
private fun initView() {
mRecyclerView = findViewById(R.id.rv)
tvContent = findViewById(R.id.tv_content)
}
private fun initData() {
FileUtils.copy2Memory(this, "test.mp3")
FileUtils.copy2Memory(this, "test.mp4")
FileUtils.copy2Memory(this, "testbg.mp3")
FileUtils.copy2Memory(this, "water.png")
mAudioPath = File(externalCacheDir, "test.mp3").absolutePath
mVideoPath = File(externalCacheDir, "test.mp4").absolutePath
mAudioBgPath = File(externalCacheDir, "testbg.mp3").absolutePath
mImagePath = File(externalCacheDir, "water.png").absolutePath
val commands = this.resources.getStringArray(R.array.commands)
val beans: MutableList<CommandBean> = ArrayList()
for (i in commands.indices) {
beans.add(CommandBean(commands[i], i))
}
mAdapter = FFmpegCommandAdapter(beans)
mRecyclerView!!.layoutManager = GridLayoutManager(this, 3)
mRecyclerView!!.adapter = mAdapter
}
private fun initListener() {
mAdapter!!.setItemClickListener(object : FFmpegCommandAdapter.ItemClickListener {
override fun itemClick(id: Int) {
tvContent!!.text = ""
if (mErrorDialog == null) {
mErrorDialog = PromptDialog.newInstance("进度", "完成", "", "停止")
mErrorDialog?.setHasNegativeButton(false)
mErrorDialog?.setOnPromptListener(object : OnPromptListener {
override fun onPrompt(isPositive: Boolean) {
if (isPositive) FFmpegCommand.cancel()
}
})
}
when (id) {
0 -> transformAudio()
1 -> transformVideo()
2 -> cutAudio()
3 -> cutVideo()
4 -> concatAudio()
5 -> concatVideo()
6 -> extractAudio()
7 -> extractVideo()
8 -> mixAudioVideo()
9 -> screenShot()
10 -> video2Image()
11 -> video2Gif()
12 -> addWaterMark()
13 -> image2Video()
14 -> decodeAudio()
15 -> encodeAudio()
16 -> multiVideo()
17 -> reverseVideo()
18 -> picInPic()
19 -> mixAudio()
20 -> videoDoubleDown()
21 -> videoSpeed2()
22 -> denoiseVideo()
23 -> reduceAudio()
24 -> video2YUV()
25 -> yuv2H264()
26 -> fadeIn()
27 -> fadeOut()
28 -> bright()
29 -> contrast()
30 -> rotate()
31 -> videoScale()
32 -> frame2Image()
33 -> audio2Fdkaac()
34 -> audio2Mp3lame()
35 -> video2HLS()
36 -> hls2Video()
37 -> audio2Amr()
38 -> makeMuteAudio()
}
}
})
}
private fun transformAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.aac"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.transformAudio(mAudioPath, targetPath), callback("音频转码完成", targetPath))
}
}
private fun transformVideo() {
targetPath = externalCacheDir.toString() + File.separator + "target.avi"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.transformVideo(mVideoPath, targetPath), callback("视频转码完成", targetPath))
}
}
private fun cutAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.cutAudio(mAudioPath, 5, 10, targetPath), callback("音频剪切完成", targetPath))
}
}
private fun cutVideo() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.cutVideo(mVideoPath, 5, 10, targetPath), callback("视频剪切完成", targetPath))
}
}
private fun concatAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.concatAudio(mAudioPath, mAudioPath, targetPath), callback("音频拼接完成", targetPath))
}
}
private fun concatVideo() {
val path = FileUtils.createInputFile(this, mVideoPath!!, mVideoPath!!, mVideoPath!!)
//val path = FileUtils.createInputFile(this, mVideoPath, mVideoPath, mVideoPath)
if (TextUtils.isEmpty(path)) {
return
}
GlobalScope.launch {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
FFmpegCommand.runCmd(FFmpegUtils.concatVideo(path, targetPath), callback("视频拼接完成", targetPath))
}
}
/**
* 变更声音
*/
private fun reduceAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.changeVolume(mAudioBgPath, 0.5f, targetPath), callback("音频降音完成", targetPath))
}
}
private fun extractAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.aac"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.extractAudio(mVideoPath, targetPath), callback("抽取音频完成", targetPath))
}
}
private fun extractVideo() {
targetPath = externalCacheDir.toString() + File.separator + "out.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.extractVideo(mVideoPath, targetPath), callback("抽取视频完成", targetPath))
}
}
private fun mixAudioVideo() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
val video = externalCacheDir.toString() + File.separator + "out.mp4"
val audio = externalCacheDir.toString() + File.separator + "target.aac"
if (!File(video).exists()) {
ToastUtils.show("请先执行抽取视频")
return
}
if (!File(audio).exists()) {
ToastUtils.show("请先执行抽取音频")
return
}
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.mixAudioVideo(video, audio, targetPath), callback("音视频合成完成", targetPath))
}
}
private fun screenShot() {
targetPath = externalCacheDir.toString() + File.separator + "target.jpeg"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.screenShot(mVideoPath, targetPath), callback("视频截图完成", targetPath))
}
}
private fun video2Image() {
val dir = File(externalCacheDir, "images")
if (!dir.exists()) {
dir.mkdir()
} else {
val files = dir.listFiles()
for (file in files) {
file.delete()
}
}
targetPath = dir.absolutePath
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.video2Image(mVideoPath, targetPath,
ImageFormat.JPG), callback("视频截图完成", targetPath))
}
}
private fun video2Gif() {
targetPath = externalCacheDir.toString() + File.separator + "target.gif"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.video2Gif(mVideoPath, 0, 10, targetPath), callback("视频转Gif完成", targetPath))
}
}
private fun addWaterMark() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.addWaterMark(mVideoPath, mImagePath, targetPath), callback("添加视频水印完成", targetPath))
}
}
private fun image2Video() {
val dir = File(externalCacheDir, "images")
if (!dir.exists()) {
ToastUtils.show("请先执行视频转图片")
return
}
targetPath = externalCacheDir.toString() + File.separator + "images" + File.separator + "target" +
".mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.image2Video(dir.absolutePath,
ImageFormat.JPG, targetPath), callback("图片转视频完成", targetPath))
}
}
private fun decodeAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.pcm"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.decodeAudio(mAudioPath, targetPath, 44100, 2), callback("音频解码PCM完成", targetPath))
}
}
private fun encodeAudio() {
val pcm = externalCacheDir.toString() + File.separator + "target.pcm"
if (!File(pcm).exists()) {
ToastUtils.show("请先执行音频解码PCM")
return
}
targetPath = externalCacheDir.toString() + File.separator + "target.wav"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.encodeAudio(pcm, targetPath, 44100, 2), callback("音频编码完成", targetPath))
}
}
private fun multiVideo() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.multiVideo(mVideoPath, mVideoPath, targetPath,
Direction.LAYOUT_HORIZONTAL), callback("多画面拼接完成", targetPath))
}
}
private fun reverseVideo() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.reverseVideo(mVideoPath, targetPath), callback("反序播放完成", targetPath))
}
}
private fun picInPic() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.picInPicVideo(mVideoPath, mVideoPath, 100, 100,
targetPath), callback("画中画完成", targetPath))
}
}
private fun mixAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.mixAudio(mAudioPath, mAudioBgPath, targetPath), callback("音频混合完成", targetPath))
}
}
private fun videoDoubleDown() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.videoDoubleDown(mVideoPath, targetPath), callback("视频缩小一倍完成", targetPath))
}
}
private fun videoSpeed2() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.videoSpeed2(mVideoPath, targetPath), callback("视频倍速完成", targetPath))
}
}
private fun denoiseVideo() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.denoiseVideo(mVideoPath, targetPath), callback("视频降噪完成", targetPath))
}
}
private fun video2YUV() {
targetPath = externalCacheDir.toString() + File.separator + "target.yuv"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.decode2YUV(mVideoPath, targetPath), callback("视频解码YUV完成", targetPath))
}
}
private fun yuv2H264() {
targetPath = externalCacheDir.toString() + File.separator + "target.h264"
val video = externalCacheDir.toString() + File.separator + "target.yuv"
if (!File(video).exists()) {
ToastUtils.show("请先执行视频解码YUV")
return
}
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.yuv2H264(video, targetPath), callback("视频编码H264完成", targetPath))
}
}
private fun fadeIn() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.audioFadeIn(mAudioPath, targetPath), callback("音频淡入完成", targetPath))
}
}
private fun fadeOut() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.audioFadeOut(mAudioPath, targetPath, 34, 5), callback("音频淡出完成", targetPath))
}
}
private fun bright() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.videoBright(mVideoPath, targetPath, 0.25f), callback("视频提高亮度完成", targetPath))
}
}
private fun contrast() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.videoContrast(mVideoPath, targetPath, 1.5f), callback(
"视频修改对比度完成", targetPath))
}
}
private fun rotate() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.videoRotation(mVideoPath, targetPath,
Transpose.CLOCKWISE_ROTATION_90), callback("视频旋转完成", targetPath))
}
}
private fun videoScale() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.videoScale(mVideoPath, targetPath, 360, 640),
callback("视频缩放完成", targetPath))
}
}
private fun frame2Image() {
targetPath = externalCacheDir.toString() + File.separator + "target.png"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.frame2Image(mVideoPath, targetPath, "00:00:10.234"), callback("获取一帧图片成功", targetPath))
}
}
private fun audio2Fdkaac() {
targetPath = externalCacheDir.toString() + File.separator + "target.aac"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.audio2Fdkaac(mAudioPath, targetPath), callback("mp3转aac成功", targetPath))
}
}
private fun audio2Mp3lame() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
val aac = externalCacheDir.toString() + File.separator + "target.aac"
if (!File(aac).exists()) {
ToastUtils.show("请先执行音频转fdk_aac")
return
}
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.audio2Mp3lame(aac, targetPath), callback("acc转mp3成功", targetPath))
}
}
private fun video2HLS() {
val dir = File(externalCacheDir, "hls")
if (!dir.exists()) {
dir.mkdir()
} else {
val files = dir.listFiles()
for (file in files) {
file.delete()
}
}
targetPath = dir.toString() + File.separator + "target.m3u8"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.video2HLS(mVideoPath, targetPath, 10), callback("切片成功", targetPath))
}
}
private fun hls2Video() {
val dir = File(externalCacheDir, "hls")
if (!dir.exists()) {
ToastUtils.show("请先执行video->hls")
return
}
val videoIndexFile = File(dir, "target.m3u8")
if (!videoIndexFile.exists()) {
ToastUtils.show("请先执行video->hls")
return
}
targetPath = dir.toString() + File.separator + "target.mp4"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.hls2Video(videoIndexFile.absolutePath, targetPath), callback("合成切片成功", targetPath))
}
}
private fun audio2Amr() {
targetPath = externalCacheDir.toString() + File.separator + "target.amr"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.audio2Amr(mAudioPath, targetPath), callback("mp3转amr成功", targetPath))
}
}
private fun makeMuteAudio() {
targetPath = externalCacheDir.toString() + File.separator + "target.mp3"
GlobalScope.launch {
FFmpegCommand.runCmd(FFmpegUtils.makeMuteAudio(targetPath), callback("生成静音文件成功", targetPath))
}
}
private fun splitmute(){
GlobalScope.launch {
var commands = "ffmpeg -i %s -af pan=1c|c0=c0,silencedetect=n=-35dB:d=0.100 -f null /dev/null"
var cmd:Array<String?> = String.format(commands,mAudioPath).split(" ").toTypedArray()
FFmpegCommand.runCmd(cmd, callback("检测静音", targetPath))
}
}
// 推流
private fun rtmp(){
GlobalScope.launch {
// 需替换成你的推流地址
var cmd:Array<String?> = FFmpegUtils.rtmp(mVideoPath,"rtmp://192.168.2.101:1935/live/film")
FFmpegCommand.runCmd(cmd, callback("推流", targetPath))
}
}
private fun callback(msg: String, targetPath: String?): CommonCallBack? {
return object : CommonCallBack() {
override fun onStart() {
Log.d("FFmpegCmd", "onStart")
runOnUiThread {
mErrorDialog?.show(supportFragmentManager, "Dialog")
}
}
override fun onComplete() {
Log.d("FFmpegCmd", "onComplete")
runOnUiThread {
ToastUtils.show(msg)
mErrorDialog?.setContent(0)
mErrorDialog?.dismissAllowingStateLoss()
tvContent?.text = targetPath
}
}
override fun onCancel() {
runOnUiThread {
ToastUtils.show("用户取消")
mErrorDialog?.setContent(0)
}
Log.d("FFmpegCmd", "Cancel")
}
override fun onProgress(progress: Int, pts: Long) {
var duration :Int? = FFmpegCommand.getMediaInfo(mAudioPath,MediaAttribute.DURATION)
var progressN = pts/duration!!
Log.d("FFmpegCmd", progress.toString() + "")
runOnUiThread { mErrorDialog?.setContent(progress) }
}
override fun onError(errorCode: Int, errorMsg: String?) {
Log.d("FFmpegCmd", errorMsg+"")
runOnUiThread {
ToastUtils.show(errorMsg)
mErrorDialog?.setContent(0)
mErrorDialog?.dismissAllowingStateLoss()
}
}
}
}
companion object {
fun start(context: Context) {
val intent = Intent(context, KFFmpegCommandActivity::class.java)
context.startActivity(intent)
}
}
}