Reputation: 4607
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 - f2 =f1. Basicly appand f1 to f2.
However, when I use mergeSongs()
resulting audio file repeats first audio file(f1
)
constants
private static String mFileNameFromRec = null;
private static String mFileNameToUse = null;
mFileNameFromRec = context.getCacheDir().getAbsolutePath();
mFileNameFromRec += "/audiorecordtest.mp4";
mFileNameToUse = context.getCacheDir().getAbsolutePath();
mFileNameToUse += "/audioToUse.mp4";
My method is following:
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");
}
}
Upvotes: 0
Views: 2112
Reputation: 4607
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
private void mergeSongs() throws Exception {
File merge = new File(mFileNameToUse);
String f1 = mFileNameFromRec;
String f2 = 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;
inMovies = new Movie[] { MovieCreator.build(f2),
MovieCreator.build(f1), };
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")) {
audioTracks.add(t);
}
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
}
}
}
Movie result = new Movie();
if (videoTracks.size() > 0) {
result.addTrack(new AppendTrack(videoTracks
.toArray(new Track[videoTracks.size()])));
}
if (audioTracks.size() > 0) {
result.addTrack(new AppendTrack(audioTracks
.toArray(new Track[audioTracks.size()])));
}
Container out = new DefaultMp4Builder().build(result);
RandomAccessFile ram = new RandomAccessFile(String.format(context
.getCacheDir() + "/output.mp4"), "rw");
FileChannel fc = 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();
String mergedFilepath = String.format(context.getCacheDir()
+ "/output.mp4");
copy(new File(mergedFilepath), new File(mFileNameToUse));
Toast.makeText(getApplicationContext(), "success",
Toast.LENGTH_SHORT).show();
}
}
Upvotes: 1