0
我想連接2個mp4音頻文件。我累了使用https://code.google.com/p/mp4parser/
我想實現的:我想連接f1和f2並將其保存到f2。如果f2不存在 - f2 = f1。基本上appand f1到f2。
然而,當我使用mergeSongs()
得到的音頻文件重複第一音頻文件(f1
)
常數如何連接mp4文件?
private static String mFileNameFromRec = null;
private static String mFileNameToUse = null;
mFileNameFromRec = context.getCacheDir().getAbsolutePath();
mFileNameFromRec += "/audiorecordtest.mp4";
mFileNameToUse = context.getCacheDir().getAbsolutePath();
mFileNameToUse += "/audioToUse.mp4";
我的方法是下列:
private void mergeSongs() throws Exception {
String f1 = mFileNameFromRec;
String f2 = mFileNameToUse;
File merge = new File(mFileNameToUse);
if (!merge.exists()) {
InputStream in = new FileInputStream(new File(f1));
OutputStream out = new FileOutputStream(new File(f2));
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Log.d("audio concatenation","was copied");
} else {
Movie[] inMovies = null;
inMovies = new Movie[] { MovieCreator.build(f1),
MovieCreator.build(f2) };
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
Log.d("audio concatenation","add audio track: "+ t.toString());
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks
.toArray(new Track[audioTracks.size()])));
}
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks
.toArray(new Track[videoTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
File newAudio = new File(mFileNameToUse);
FileOutputStream fOut = new FileOutputStream(newAudio);
FileChannel fc = fOut.getChannel();
// FileChannel fc = new RandomAccessFile(mFileNameToUse, "rw")
// .getChannel();
out.writeContainer(fc);
fc.close();
fOut.flush();
fOut.close();
Log.d("audio concatenation","was concated");
}
}
在Linux中使用本機工具帶命令行的/窗口,並調用從Java,不是最好的方式,但我懷疑你也會找到一個純粹的所有編碼的java impl – tgkprog
對不起忘了添加android標籤。 – Yarh