extract audio in MP3 format from video in android

与世无争的帅哥 提交于 2020-03-28 06:41:08

问题


I want to convert a video file to mp3 file in android.

I am using the below code to convert video to 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();
}

But it is not working. Here is the error that I'm getting:

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)

Any idea on what could be wrong?


回答1:


Use FFMPEG for android U need to implement a method (i think its present in the ffmpeg library already) and u can use the following command to extract audio

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



回答2:


FFmpeg is increasing the size of the application. After a lot of debugging and research I have created a gist which uses Android's default sound APIs (MediaExtractor & MediaMuxer). You can check this from the given URL.

This can do the below mentioned operations in a very simple way,

Extract Audio From Video:

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

Extract Video From Video (Mute):

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

Crop Video From Start:

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

Crop Video From End:

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

(where videoFile is an input file path and originalAudio is the output file path)



来源:https://stackoverflow.com/questions/60572380/how-can-i-extract-the-audio-out-of-a-video-file-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!