How to combine an audio file with a new video file? is this possible in android?

I successfully retrieved a video from a sequence of images using javacv in android.now I have a problem with how to combine audio with this newly created video. Is this possible in the integration of Android and javacv?

Here is my code, String path ="/mnt/sdcard/Video_images"; File folder = new File(path); File[] listOfFiles = folder.listFiles(); if(listOfFiles.length>0) { iplimage = new opencv_core.IplImage[listOfFiles.length]; for (int j = 0; j < listOfFiles.length; j++) { String files=""; if (listOfFiles[j].isFile()) { files = listOfFiles[j].getName(); System.out.println(" j " +j + listOfFiles[j]); } String[] tokens = files.split("\\.(?=[^\\.]+$)"); String name=tokens[0]; Toast.makeText(getBaseContext(), "size"+listOfFiles.length, Toast.LENGTH_SHORT).show(); iplimage[j]=cvLoadImage("/mnt/sdcard/Video_images/"+name+".jpg"); } } // FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/mnt/sdcard/Video_images /output"+System.currentTimeMillis()+".mp4",200,150); try { recorder.setVideoCodec(a); //CODEC_ID_MPEG4 //CODEC_ID_MPEG1VIDEO recorder.setFrameRate(24); recorder.setPixelFormat(PIX_FMT_YUV420P); //PIX_FMT_YUV420P recorder.start(); for (int i=0;i<iplimage.length;i++) { recorder.record(iplimage[i]); } recorder.stop(); } catch (Exception e){ e.printStackTrace(); } 

in this code, how to merge my audio file?

+4
source share
2 answers

In Android, there is nothing to merge video files. You will need to find a Java JAR that can handle this for you if you need to do this on the device.

0
source

This should be possible with the new version of javacv, which can be downloaded from here .

Here is the idea of ​​your code if you want to combine audio with video while creating mp4:

  FFmpegFrameGrabber grabber1 = new FFmpegFrameGrabber("song.mp3"); grabber1.start(); FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("/mnt/sdcard/Video_images/output"+System.currentTimeMillis()+".mp4",200,150, grabber1.getAudioChannels()); try { recorder.setVideoCodec(a); //CODEC_ID_MPEG4 //CODEC_ID_MPEG1VIDEO recorder.setFrameRate(24); recorder.setPixelFormat(PIX_FMT_YUV420P); //PIX_FMT_YUV420P recorder.start(); com.googlecode.javacv.Frame frame1 = new com.googlecode.javacv.Frame(); for (int i=0;i<iplimage.length;i++) { frame1 = grabber1.grabFrame(); recorder.record(frame1); recorder.record(iplimage[i]); } recorder.stop(); grabber1.stop(); 

This may not be 100% correct, but it should be a good starting place. Of course, you want to make sure your frame1 is not null before trying to write it.

0
source

All Articles