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

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

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

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

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

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

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

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

Loading…
Cancel
Save