Merge pull request #222 from luqiming666/master

代码优化:equalizer、视频拼接、Probe等
master
xufuji456 2 years ago committed by GitHub
commit cb99f930be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      AndroidMedia/src/main/java/com/frank/androidmedia/controller/AudioEffectController.kt
  2. 2
      AndroidMedia/src/main/java/com/frank/androidmedia/listener/AudioEffectCallback.kt
  3. 39
      app/src/main/java/com/frank/ffmpeg/activity/AudioEffectActivity.kt
  4. 4
      app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt
  5. 13
      app/src/main/java/com/frank/ffmpeg/activity/ProbeFormatActivity.kt
  6. 9
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.kt
  7. 6
      app/src/main/java/com/frank/ffmpeg/adapter/EqualizerAdapter.kt
  8. 2
      app/src/main/java/com/frank/ffmpeg/listener/OnSeekBarListener.kt
  9. 11
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java

@ -91,7 +91,7 @@ open class AudioEffectController(audioEffectCallback: AudioEffectCallback) {
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, arg2: Int, arg3: Long) {
try {
mEqualizer!!.usePreset(arg2.toShort())
val seekBarList: List<SeekBar>? = mAudioEffectCallback?.getSeeBarList()
val seekBarList: List<SeekBar>? = mAudioEffectCallback?.getSeekBarList()
if (mBands > 0 && seekBarList != null && mEqualizer != null) {
for (band in 0 until mBands) {
seekBarList[band].progress = mEqualizer!!.getBandLevel(band.toShort()) - minEQLevel

@ -12,7 +12,7 @@ import java.util.ArrayList
*/
interface AudioEffectCallback {
fun getSeeBarList(): List<SeekBar>?
fun getSeekBarList(): List<SeekBar>?
fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList<Pair<*, *>>)

@ -15,7 +15,7 @@ import com.frank.androidmedia.controller.AudioEffectController
import com.frank.androidmedia.listener.AudioEffectCallback
import com.frank.ffmpeg.R
import com.frank.ffmpeg.adapter.EqualizerAdapter
import com.frank.ffmpeg.listener.OnSeeBarListener
import com.frank.ffmpeg.listener.OnSeekBarListener
import com.frank.ffmpeg.util.FileUtil
import com.frank.ffmpeg.view.VisualizerView
import java.io.IOException
@ -25,11 +25,11 @@ import java.util.ArrayList
* Audio effect: equalizer, enhancer, visualizer, bassBoost
* Created by frank on 2020/10/20.
*/
class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallback {
class AudioEffectActivity : BaseActivity(), OnSeekBarListener, AudioEffectCallback {
companion object {
private val audioPath = Environment.getExternalStorageDirectory().path + "/tiger.mp3"
private var audioPath = Environment.getExternalStorageDirectory().path + "/tiger.mp3"
}
private var mPlayer: MediaPlayer? = null
@ -48,12 +48,13 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
Manifest.permission.MODIFY_AUDIO_SETTINGS)
private val onPreparedListener = MediaPlayer.OnPreparedListener {
mAudioEffectController = AudioEffectController(this)
mAudioEffectController?.setupEqualizer(mPlayer!!.audioSessionId)
mAudioEffectController?.setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!)
mAudioEffectController?.setupBassBoost(mPlayer!!.audioSessionId, barBassBoost!!)
mAudioEffectController?.setLoudnessEnhancer(mPlayer!!.audioSessionId, barEnhancer!!)
mAudioEffectController?.setupVisualizer(mPlayer!!.audioSessionId)
mAudioEffectController = AudioEffectController(this).apply {
setupEqualizer(mPlayer!!.audioSessionId)
setupPresetStyle(this@AudioEffectActivity, spinnerStyle!!)
setupBassBoost(mPlayer!!.audioSessionId, barBassBoost!!)
setLoudnessEnhancer(mPlayer!!.audioSessionId, barEnhancer!!)
setupVisualizer(mPlayer!!.audioSessionId)
}
mPlayer!!.start()
}
@ -95,10 +96,11 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
return
}
try {
mPlayer = MediaPlayer()
mPlayer!!.setDataSource(audioPath)
mPlayer!!.setOnPreparedListener(onPreparedListener)
mPlayer!!.prepareAsync()
mPlayer = MediaPlayer().apply {
setDataSource(audioPath)
setOnPreparedListener(onPreparedListener)
prepareAsync()
}
} catch (e: IOException) {
Log.e("AudioEffect", "play error=$e")
}
@ -113,17 +115,18 @@ class AudioEffectActivity : BaseActivity(), OnSeeBarListener, AudioEffectCallbac
}
override fun onSelectedFile(filePath: String) {
audioPath = filePath
initPlayer()
}
override fun setEqualizerList(maxProgress: Int, equalizerList: ArrayList<Pair<*, *>>) {
if (equalizerAdapter != null) {
equalizerAdapter!!.setMaxProgress(maxProgress)
equalizerAdapter!!.setEqualizerList(equalizerList)
equalizerAdapter?.let {
it.setMaxProgress(maxProgress)
it.setEqualizerList(equalizerList)
}
}
override fun getSeeBarList(): List<SeekBar>? {
override fun getSeekBarList(): List<SeekBar>? {
return equalizerAdapter?.getSeekBarList()
}

@ -12,12 +12,12 @@ import androidx.recyclerview.widget.RecyclerView
import com.frank.ffmpeg.AudioPlayer
import com.frank.ffmpeg.R
import com.frank.ffmpeg.adapter.EqualizerAdapter
import com.frank.ffmpeg.listener.OnSeeBarListener
import com.frank.ffmpeg.listener.OnSeekBarListener
import com.frank.ffmpeg.view.VisualizerView
import java.lang.StringBuilder
import java.util.ArrayList
class EqualizerActivity : BaseActivity(), OnSeeBarListener {
class EqualizerActivity : BaseActivity(), OnSeekBarListener {
// unit: Hz gain:0-20
/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

@ -57,9 +57,8 @@ class ProbeFormatActivity : BaseActivity() {
MSG_FINISH -> {
progressProbe!!.visibility = View.GONE
layoutProbe!!.visibility = View.VISIBLE
val result = msg.obj?: msg.obj
if (result != null) {
val mediaInfo = JsonParseTool.stringFormat(result as MediaBean)
if (msg.obj != null) {
val mediaInfo = JsonParseTool.stringFormat(msg.obj as MediaBean)
if (!mediaInfo.isNullOrEmpty() && txtProbeFormat != null) {
txtProbeFormat!!.text = mediaInfo
}
@ -108,6 +107,7 @@ class ProbeFormatActivity : BaseActivity() {
when (view?.id) {
R.id.btn_probe_format -> {
findViewById<ImageView>(R.id.img_frame).setImageBitmap(null)
doHandleProbe(filePath)
}
R.id.btn_retrieve_format -> {
@ -188,7 +188,12 @@ class ProbeFormatActivity : BaseActivity() {
mHandler.obtainMessage(MSG_INFO, resultBuilder.toString()).sendToTarget()
// Retrieve frame with timeUs
val bitmap = retriever.getFrameAtTime(5 * 1000000)
val frameTime = if (duration != null) {
duration.toLong() / 3 * 1000
} else {
5 * 1000000
}
val bitmap = retriever.getFrameAtTime(frameTime)
// Retrieve audio thumbnail, if it has embedded
// val bitmap = retriever.audioThumbnail
if (bitmap != null) {

@ -377,15 +377,10 @@ class VideoHandleActivity : BaseActivity() {
Log.e(TAG, "width=$width--height=$height")
}
val transformCmd2 = FFmpegUtil.transformVideoWithEncode(appendPath, width, height, outputPath2)
val fileList = ArrayList<String>()
fileList.add(outputPath1)
fileList.add(outputPath2)
val fileList = listOf(outputPath1, outputPath2)
FileUtil.createListFile(listPath, fileList)
val jointVideoCmd = FFmpegUtil.jointVideo(listPath, targetPath)
val commandList = ArrayList<Array<String>>()
commandList.add(transformCmd1)
commandList.add(transformCmd2)
commandList.add(jointVideoCmd)
val commandList = listOf(transformCmd1, transformCmd2, jointVideoCmd)
ffmpegHandler!!.executeFFmpegCmds(commandList)
}

@ -10,7 +10,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.frank.ffmpeg.R
import com.frank.ffmpeg.listener.OnSeeBarListener
import com.frank.ffmpeg.listener.OnSeekBarListener
import java.util.ArrayList
@ -20,7 +20,7 @@ import java.util.ArrayList
* @desc Adapter of equalizer
*/
class EqualizerAdapter(private val context: Context, private val onSeeBarListener: OnSeeBarListener?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class EqualizerAdapter(private val context: Context, private val onSeekBarListener: OnSeekBarListener?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var equalizerList: List<Pair<*, *>>? = ArrayList()
private val seekBarList = ArrayList<SeekBar>()
@ -51,7 +51,7 @@ class EqualizerAdapter(private val context: Context, private val onSeeBarListene
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
onSeeBarListener?.onProgress(i, seekBar.progress)
onSeekBarListener?.onProgress(i, seekBar.progress)
}
})
}

@ -1,5 +1,5 @@
package com.frank.ffmpeg.listener
interface OnSeeBarListener {
interface OnSeekBarListener {
fun onProgress(index: Int, progress: Int)
}

@ -244,14 +244,9 @@ public class FFmpegUtil {
| 10b | 11b | 12b | 13b | 14b | 15b | 16b | 17b | 18b |
| 1480 | 2093 | 2960 | 4186 | 5920 | 8372 | 11840 | 16744 | 20000 |
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
StringBuilder builder = new StringBuilder();
for (String band:bandList) {
builder.append(band).append(":");
}
builder.deleteCharAt(builder.length() - 1);
// "ffmpeg -i %s -af superequalizer=%s -y %s"
String bands = String.join(":", bandList);
String equalizerCmd = "ffmpeg -i -af superequalizer=%s -y";
equalizerCmd = String.format(Locale.getDefault(), equalizerCmd, builder.toString());
equalizerCmd = String.format(Locale.getDefault(), equalizerCmd, bands);
return insert(equalizerCmd.split(" "), 2, inputPath, outputPath);
}
@ -359,7 +354,7 @@ public class FFmpegUtil {
*/
public static String[] jointVideo(String fileListPath, String outputPath) {
// ffmpeg -f concat -safe 0 -i %s -c copy %s
String jointVideoCmd = "ffmpeg -f concat -safe 0 -i file.txt -c copy %s";
String jointVideoCmd = "ffmpeg -f concat -safe 0 -i -c copy -y";
return insert(jointVideoCmd.split(" "), 6, fileListPath, outputPath);
}

Loading…
Cancel
Save