Reputation:
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?
Upvotes: 1
Views: 6101
Reputation: 434
Instead of using ffmpeg, use android MediaExtractor & MediaMuxer. This works for all android versions including android 11.
public class AudioUtils {
private static final String TAG = "AudioUtils";
private static final int DEFAULT_BUFFER_SIZE = 1 * 1024 * 1024;
public void genVideoUsingMuxer(FileDescriptor srcPath, FileDescriptor dstPathFd, String dstPath, int startMs, int endMs, boolean useAudio, boolean useVideo) throws IOException {
// Set up MediaExtractor to read from the source.
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource(srcPath);
int trackCount = extractor.getTrackCount();
// Set up MediaMuxer for the destination.
MediaMuxer muxer;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
muxer = new MediaMuxer(dstPathFd, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} else {
muxer = new MediaMuxer(dstPath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
}
// Set up the tracks and retrieve the max buffer size for selected
// tracks.
HashMap<Integer, Integer> indexMap = new HashMap<Integer, Integer>(trackCount);
int bufferSize = -1;
for (int i = 0; i < trackCount; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
boolean selectCurrentTrack = false;
if (mime.startsWith("audio/") && useAudio) {
selectCurrentTrack = true;
} else if (mime.startsWith("video/") && useVideo) {
selectCurrentTrack = true;
}
if (selectCurrentTrack) {
extractor.selectTrack(i);
int dstIndex = muxer.addTrack(format);
indexMap.put(i, dstIndex);
if (format.containsKey(MediaFormat.KEY_MAX_INPUT_SIZE)) {
int newSize = format.getInteger(MediaFormat.KEY_MAX_INPUT_SIZE);
bufferSize = newSize > bufferSize ? newSize : bufferSize;
}
}
}
if (bufferSize < 0) {
bufferSize = DEFAULT_BUFFER_SIZE;
}
// Set up the orientation and starting time for extractor.
MediaMetadataRetriever retrieverSrc = new MediaMetadataRetriever();
retrieverSrc.setDataSource(srcPath);
String degreesString = retrieverSrc.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
if (degreesString != null) {
int degrees = Integer.parseInt(degreesString);
if (degrees >= 0) {
muxer.setOrientationHint(degrees);
}
}
if (startMs > 0) {
extractor.seekTo(startMs * 1000, MediaExtractor.SEEK_TO_CLOSEST_SYNC);
}
// Copy the samples from MediaExtractor to MediaMuxer. We will loop
// for copying each sample and stop when we get to the end of the source
// file or exceed the end time of the trimming.
int offset = 0;
int trackIndex = -1;
ByteBuffer dstBuf = ByteBuffer.allocate(bufferSize);
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
muxer.start();
while (true) {
bufferInfo.offset = offset;
bufferInfo.size = extractor.readSampleData(dstBuf, offset);
if (bufferInfo.size < 0) {
Log.d(TAG, "Saw input EOS.");
bufferInfo.size = 0;
break;
} else {
bufferInfo.presentationTimeUs = extractor.getSampleTime();
if (endMs > 0 && bufferInfo.presentationTimeUs > (endMs * 1000)) {
Log.d(TAG, "The current sample is over the trim end time.");
break;
} else {
bufferInfo.flags = extractor.getSampleFlags();
trackIndex = extractor.getSampleTrackIndex();
muxer.writeSampleData(indexMap.get(trackIndex), dstBuf, bufferInfo);
extractor.advance();
}
}
}
muxer.stop();
muxer.release();
return;
}
}
This is how you call the method.
new AudioUtils().genVideoUsingMuxer(inputVideoFileDescriptor, outputAudioFileDescriptor, destPath, -1, -1, true, false);
EXAMPLE:
String destPath = Environment.getExternalStoragePublicDirectory("") + "/AudFolder2/asd.mp3"
try {
FileDescriptor outputAudioFileDescriptor = getActivity().getContentResolver().openFileDescriptor(outputAudioUri, "w").getFileDescriptor();
FileDescriptor inputVideoFileDescriptor = getActivity().getContentResolver().openFileDescriptor(Uri.parse(inputVideoUri), "r").getFileDescriptor();
new AudioUtils().genVideoUsingMuxer(pfdVideo.getFileDescriptor(), pfdAudio.getFileDescriptor(), destPath, -1, -1, true, false);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "saveAudioToDevice: ERROR = "+ e.getMessage() );
}
Upvotes: 0
Reputation: 2982
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)
Upvotes: 9