From 0d7d2907cf40c30aefa4a6449e7c6ac8cff4e372 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Sun, 18 Oct 2020 16:21:30 +0800 Subject: [PATCH] change audio speed --- .../ffmpeg/activity/AudioHandleActivity.java | 8 +++++++- .../java/com/frank/ffmpeg/util/FFmpegUtil.java | 16 ++++++++++++++++ .../main/res/layout/activity_audio_handle.xml | 8 ++++++++ app/src/main/res/values-en/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java index 0757f9f..91a67e1 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java @@ -108,7 +108,8 @@ public class AudioHandleActivity extends BaseActivity { R.id.btn_play_audio, R.id.btn_play_opensl, R.id.btn_audio_encode, - R.id.btn_pcm_concat + R.id.btn_pcm_concat, + R.id.btn_audio_speed ); } @@ -208,6 +209,11 @@ public class AudioHandleActivity extends BaseActivity { FileUtil.concatFile(srcPCM, appendPCM, concatPCM); mHandler.obtainMessage(MSG_FINISH).sendToTarget(); return; + case R.id.btn_audio_speed://change audio speed + float speed = 2.0f;//from 0.5 to 2.0 + String speedPath = PATH + File.separator + "speed.mp3"; + commandLine = FFmpegUtil.changeAudioSpeed(srcFile, speedPath, speed); + break; default: break; } diff --git a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java index 1b92c66..79145f6 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -520,6 +520,22 @@ public class FFmpegUtil { return speedCmd.split(" "); } + /** + * Changing the speed of playing, speed range at 0.5-2 in audio. + * @param inputFile the inputFile of normal speed + * @param outputFile the outputFile which you want to change speed + * @param speed speed of playing + * @return change speed success or not + */ + public static String[] changeAudioSpeed(String inputFile, String outputFile, float speed) { + if (speed > 2 || speed < 0.5) { + throw new IllegalArgumentException("audio speed range is from 0.5 to 2"); + } + String speedCmd = "ffmpeg -i %s -filter_complex atempo=%.2f %s"; + speedCmd = String.format(Locale.getDefault(), speedCmd, inputFile, speed, outputFile); + return speedCmd.split(" "); + } + /** * Rebuild the keyframe index of FLV, make it seekable * @param inputFile inputFile diff --git a/app/src/main/res/layout/activity_audio_handle.xml b/app/src/main/res/layout/activity_audio_handle.xml index 70f5397..898ec92 100644 --- a/app/src/main/res/layout/activity_audio_handle.xml +++ b/app/src/main/res/layout/activity_audio_handle.xml @@ -66,6 +66,14 @@ android:layout_height="wrap_content" android:text="@string/pcm_concat" android:layout_marginTop="10dp" /> + +