I want convert mp4 video to mp3 audio file in Android platform? How can I do it? Actually I test some JAR. Firstly, the JAAD lib be used to test.
import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;
import net.sourceforge.jaad.aac.Decoder;
import net.sourceforge.jaad.aac.SampleBuffer;
import net.sourceforge.jaad.mp4.MP4Container;
import net.sourceforge.jaad.mp4.api.AudioTrack;
import net.sourceforge.jaad.mp4.api.Frame;
import net.sourceforge.jaad.mp4.api.Movie;
import net.sourceforge.jaad.mp4.api.Track;
import net.sourceforge.jaad.util.wav.WaveFileWriter;
public class Main2 {
public static void main(String[] args) {
System.out.println("dfd");
try {
decodeMP4("C:/a/input.mp4","./out.wav");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("over");
}
private static void decodeMP4(String in, String out) throws Exception {
WaveFileWriter wav = null;
try {
final MP4Container cont = new MP4Container(new RandomAccessFile(in, "r"));
final Movie movie = cont.getMovie();
final List<Track> tracks = movie.getTracks(AudioTrack.AudioCodec.AAC);
if(tracks.isEmpty()) throw new Exception("movie does not contain any AAC track");
final AudioTrack track = (AudioTrack) tracks.get(0);
wav = new WaveFileWriter(new File(out), track.getSampleRate(), track.getChannelCount(), track.getSampleSize());
final Decoder dec = new Decoder(track.getDecoderSpecificInfo());
Frame frame;
final SampleBuffer buf = new SampleBuffer();
while(track.hasMoreFrames()) {
frame = track.readNextFrame();
dec.decodeFrame(frame.getData(), buf);
wav.write(buf.getData());
}
}
finally {
if(wav!=null) wav.close();
}
}
}
but it throws an error
"java.lang.ClassCastException: net.sourceforge.jaad.mp4.boxes.impl.PixelAspectRatioBox cannot be cast to net.sourceforge.jaad.mp4.boxes.impl.sampleentries.codec.CodecSpecificBox
at net.sourceforge.jaad.mp4.api.VideoTrack.<init>(VideoTrack.java:62)
at net.sourceforge.jaad.mp4.api.Movie.createTrack(Movie.java:65)
at net.sourceforge.jaad.mp4.api.Movie.<init>(Movie.java:46)
at net.sourceforge.jaad.mp4.MP4Container.getMovie(MP4Container.java:134)
at Main2.decodeMP4(Main2.java:30)
at Main2.main(Main2.java:18)"
This question is a little too complex. I know how to implement it. here is the code to extract audio data from video file ,but it's PCM data ,not mp3:
For example ,to extract audio data from Video file video.mp4,and save the extracted date into file audio.pcm,write code like this:
If you need mp3 data,you can use lame mp3 lib to encode PCM data to MP3 format.I have all the code. but it is not convinent to paste all of them here.