How To Dub A Video With Audio In Android
I want to make a dubbing app in Android. Flow of the app is: Get video and audio from the gallery. Reduce the original sound of Video file. And mix (Dub) the selected audio on th
Solution 1:
even i was looking for the same to dub my video with an audio using mediaMuxer, MediaMuxer was a little difficult concept for me to understand as i am beginner . i ended up refering this github code. https://github.com/tqnst/MP4ParserMergeAudioVideo it was my saviour. really thanx to that person. i just picked up the code i wanted from it, i.e dubbing a video with the audio i specify.
here is my code i used in my project below
privatevoidmergeAudioVideo(String originalVideoPath,String AudioPath,String OutPutVideoPath) {
// TODO Auto-generated method stubMovievideo=null;
try {
newMovieCreator();
video = MovieCreator.build(originalVideoPath);
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Movieaudio=null;
try {
newMovieCreator();
audio = MovieCreator.build(AudioPath);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
List<Track> videoTracks = newLinkedList<Track>();
for (Track t : video.getTracks()) {
if (t.getHandler().equals("vide")) {
videoTracks.add(t);
//seperate the video from the orginal video
}
}
TrackaudioTrack= audio.getTracks().get(0);// get your audio track to dub the videoMovieresult=newMovie();
result.addTrack(videoTracks.get(0)); // add the video seprated from the originals
result.addTrack(audioTrack); //add the track to be put in resul videoContainerout=newDefaultMp4Builder().build(result);
FileOutputStreamfos=null;
try {
fos = newFileOutputStream(OutPutVideoPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedWritableFileByteChannelbyteBufferByteChannel=newBufferedWritableFileByteChannel(fos);
try {
out.writeContainer(byteBufferByteChannel);
byteBufferByteChannel.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
and here is the BufferedWritableFileByteChannel class to write the outputVideo data to the directory.
publicclassBufferedWritableFileByteChannelimplementsWritableByteChannel {
privatestaticfinalintBUFFER_CAPACITY=1000000;
privatebooleanisOpen=true;
privatefinal OutputStream outputStream;
privatefinal ByteBuffer byteBuffer;
privatefinalbyte[] rawBuffer = newbyte[BUFFER_CAPACITY];
publicBufferedWritableFileByteChannel(OutputStream outputStream) {
this.outputStream = outputStream;
this.byteBuffer = ByteBuffer.wrap(rawBuffer);
}
@Overridepublicintwrite(ByteBuffer inputBuffer)throws IOException {
intinputBytes= inputBuffer.remaining();
if (inputBytes > byteBuffer.remaining()) {
dumpToFile();
byteBuffer.clear();
if (inputBytes > byteBuffer.remaining()) {
thrownewBufferOverflowException();
}
}
byteBuffer.put(inputBuffer);
return inputBytes;
}
@OverridepublicbooleanisOpen() {
return isOpen;
}
@Overridepublicvoidclose()throws IOException {
dumpToFile();
isOpen = false;
}
privatevoiddumpToFile() {
try {
outputStream.write(rawBuffer, 0, byteBuffer.position());
} catch (IOException e) {
thrownewRuntimeException(e);
}
}
}
and dont forget to add the libraries in your project.
this may not be the exact answer to your question. but atleast it will able to shed some light on the probable solution.
Post a Comment for "How To Dub A Video With Audio In Android"