How To Concat Mp4 Files?
I want to concat 2 mp4 audio files. I tired to use https://code.google.com/p/mp4parser/ What I want to achieve: i want to concat f1 and f2 and save it to f2.If f2 does not exists -
Solution 1:
And there is working solution: use library https://code.google.com/p/mp4parser/
download jars
isoparser-1.0.1.jar
aspectjrt-1.7.4.jar
constants
mFileNameFromRec = context.getCacheDir().getAbsolutePath();
mFileNameFromRec += "/audiorecordtest.mp4";
mFileNameToUse = context.getCacheDir().getAbsolutePath();
mFileNameToUse += "/audioToUse.mp4";
code
privatevoidmergeSongs()throws Exception {
Filemerge=newFile(mFileNameToUse);
Stringf1= mFileNameFromRec;
Stringf2= mFileNameToUse;
if (!merge.exists()) {
InputStreamin=newFileInputStream(newFile(f1));
OutputStreamout=newFileOutputStream(newFile(f2));
// Transfer bytes from in to outbyte[] buf = newbyte[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;
inMovies = newMovie[] { MovieCreator.build(f2),
MovieCreator.build(f1), };
List<Track> videoTracks = newLinkedList<Track>();
List<Track> audioTracks = newLinkedList<Track>();
for (Movie m : inMovies) {
for (Track t : m.getTracks()) {
if (t.getHandler().equals("soun")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movieresult=newMovie();
if (videoTracks.size() > 0) {
result.addTrack(newAppendTrack(videoTracks
.toArray(newTrack[videoTracks.size()])));
}
if (audioTracks.size() > 0) {
result.addTrack(newAppendTrack(audioTracks
.toArray(newTrack[audioTracks.size()])));
}
Containerout=newDefaultMp4Builder().build(result);
RandomAccessFileram=newRandomAccessFile(String.format(context
.getCacheDir() + "/output.mp4"), "rw");
FileChannelfc= ram.getChannel();
out.writeContainer(fc);
ram.close();
fc.close();
// IsoFile out = (IsoFile) new DefaultMp4Builder().build(result);// FileOutputStream fos = new FileOutputStream(new File(// String.format(context.getCacheDir() + "/output.mp4")));// out.getBox(fos.getChannel());// fos.close();StringmergedFilepath= String.format(context.getCacheDir()
+ "/output.mp4");
copy(newFile(mergedFilepath), newFile(mFileNameToUse));
Toast.makeText(getApplicationContext(), "success",
Toast.LENGTH_SHORT).show();
}
}
Post a Comment for "How To Concat Mp4 Files?"