最近在做一款产品,需要从视频中提取音频。查了查资料发现了有FFMPEG,MP4Parser,Android自带的解码器。首先FFMPEG比较复杂据说也慢,Android自带的Media Extractor类,不过该类用于4.3以上,适配性不好。最后选择了MP4Parser类。如何在as里面增加MP4Parse支持呢?如下所示:
1、配置gradle依赖库
compile 'org.mp4parser:streaming:1.9.27'
compile 'org.mp4parser:muxer:1.9.27'
2、实现视频提取音频
public static boolean extractMp3(String mp4FilePath,
String mp3FilePath) {
FileChannel fc = null;
Movie mp4 = null;
Movie mp3 = null;
try {
mp4 = MovieCreator.build(mp4FilePath);
List<Track> mp3TrackList = new ArrayList<Track>();
for (Track track : mp4.getTracks()) {
if (track.getHandler().equals("soun")) {
mp3TrackList.add(track);
}
}
mp3 = new Movie();
if (!mp3TrackList.isEmpty()) {
mp3.addTrack(new AppendTrack(
mp3TrackList.toArray(new Track[mp3TrackList.size()])));
}
Container container = new DefaultMp4Builder().build(mp3);
fc = new RandomAccessFile(mp3FilePath, "rw").getChannel();
container.writeContainer(fc);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fc) {
fc.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
ic_mp3.jpeg
MP4Parser方式实现视频提取音频讲解完毕,简筒们,编写代码试试吧!
网友评论