Richard
Richard

Reputation: 1117

Changing Audio Volume using FFmpeg Android

As I have understood, the only way to change FFmpeg volume is to do it throught a command line.

This is what should change the volume of the audio :

ffmpeg -i input.wav -filter:a "volume=1.5" output.wav

Now I have used FFmpeg with command line before and it looked like this and gave me no errors:

    String[] cmd = { "-i" , pcmtowavTempFile.toString(), "-i", mp3towavTempFile.toString(), "-filter_complex", "amix=inputs=2:duration=first:dropout_transition=3", combinedwavTempFile.toString()};
    ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
        @Override
        public void onSuccess(String message) {                
            super.onSuccess(message);
            encodeWavToAAC();
        }
        @Override
        public void onFailure(String message) {
            super.onFailure(message);
            onError(message);
        }
    });

But If I try to do it with audio volume in the same method, it just ignores it :

    String[] cmd = { "-i" , pcmtowavTempFile.toString(),  "-filter:a", "volume=1.3", pcmtowavTempFile.toString()};
    ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
        @Override
        public void onSuccess(String message) {
            super.onSuccess(message);
        }
        @Override
        public void onFailure(String message) {
            super.onFailure(message);

        }
    });

I get neither, no success or error message with the last volume change method.

Since the volume is there between " ", I tried adding this :

String[] cmd = { "-i" , pcmtowavTempFile.toString(),  "-filter:a", "\"volume=1.3\"", pcmtowavTempFile.toString()};

But the app started crashing after adding this line.

Upvotes: 0

Views: 1626

Answers (2)

Richard
Richard

Reputation: 1117

The error here was that FFmpeg does NOT perform in-place editing which means that I can not overwrite the same File. I tried overwriting by adding command "-y" but that did not also work because of that same rule.

So the solution was to create a new File as output File.

Upvotes: 0

Rami Jemli
Rami Jemli

Reputation: 2641

Please try using this library instead with the same command. It uses the latest FFmpeg.
https://github.com/bravobit/FFmpeg-Android

FFmpeg ffmpeg = FFmpeg.getInstance(context);
if (ffmpeg.isSupported()) {
   // to execute "ffmpeg -version" command you just need to pass "-version"
   String[] cmd= {"-i", pcmtowavTempFile.toString(), "-af", "volume=1.3", pcmtowavTempFile.toString()};
   ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

        @Override
        public void onStart() {}

        @Override
        public void onProgress(String message) {}

        @Override
        public void onFailure(String message) {}

        @Override
        public void onSuccess(String message) {}

        @Override
        public void onFinish() {}

    });
} else {
  // ffmpeg is not supported
}

Upvotes: 1

Related Questions