从android中的视频中提取MP3格式的音频

时间:2016-12-01 10:41:46

标签: android android-studio audio

我想将视频文件转换为android中的mp3文件。

我使用以下代码将视频转换为MP3:

File source = new File(sourceFile);
File target = new File(destFile);
audioAttributes = new AudioAttributes();

audioAttributes.setCodec("libmp3lame");
audioAttributes.setBitRate(new Integer(128000));
audioAttributes.setChannels(new Integer(2));
audioAttributes.setSamplingRate(new Integer(44100));

EncodingAttributes encodingAttributes = new EncodingAttributes();
encodingAttributes.setFormat("mp3");
encodingAttributes.setAudioAttributes(audioAttributes);

Encoder encoder = new Encoder();

try {
    encoder.encode(source, target, encodingAttributes);
} catch (EncoderException e) {
    e.printStackTrace();
}

但它不起作用。这是我得到的错误:

java.io.IOException: Error running exec(). Command: [/bin/chmod, 755, /data/data/com.example.videotomp3_demo/cache/jave-1/ffmpeg] Working Directory: null Environment: null
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.ProcessManager.exec(ProcessManager.java:211)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.Runtime.exec(Runtime.java:173)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.Runtime.exec(Runtime.java:128)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at it.sauronsoftware.jave.DefaultFFMPEGLocator.<init>(DefaultFFMPEGLocator.java:85)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at it.sauronsoftware.jave.Encoder.<init>(Encoder.java:111)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at com.example.videotomp3_demo.MainActivity.onCreate(MainActivity.java:50)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.Activity.performCreate(Activity.java:6005)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:176)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:111)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.os.Looper.loop(Looper.java:194)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5576)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err: Caused by: java.io.IOException: No such file or directory
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.ProcessManager.exec(Native Method)
12-01 15:19:04.116 19702-19702/com.example.videotomp3_demo W/System.err:     at java.lang.ProcessManager.exec(ProcessManager.java:209)

有什么可能出错的想法吗?

2 个答案:

答案 0 :(得分:2)

FFmpeg 正在增加应用程序的大小。经过大量的调试和研究,我创建了一个使用Android默认声音Apis(MediaExtractor&MediaMuxer)的要点。您可以从给定的URL进行检查:

https://gist.github.com/ArsalRaza/132a6e99d59aa80b9861ae368bc786d0

这可以非常简单地完成以下提到的操作

从视频中提取音频:

new AudioExtractor().genVideoUsingMuxer(videoFile, originalAudio, -1, -1, true, false);

从视频中提取视频(静音):

new AudioExtractor().genVideoUsingMuxer(videoFile, originalAudio, -1, -1, false, true);

从头开始裁剪视频:

new AudioExtractor().genVideoUsingMuxer(videoFile, originalAudio, timeFromWhereToStart, -1, true, true);

从结尾裁剪视频:

new AudioExtractor().genVideoUsingMuxer(videoFile, originalAudio, -1, timeFromWhereToEnd, true, true);

答案 1 :(得分:0)

使用FFMPEG for android 你需要实现一个方法(我认为它已存在于ffmpeg库中) 你可以使用以下命令提取音频

ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac