简体   繁体   中英

how to merge audio file with new video file?is it possible in android?

i successfully got video from sequence of images using javacv in android.now i have problem that is how to merge audio to that newly created video.is it possible in android or javacv integration?

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?

There is nothing in Android for merging video files. You will need to find some Java JAR that can handle this for you, if you need to do it on the device.

It should be possible with the newest version of javacv, which can be downloaded from here .

Here is an idea of what your code would like if you wanted to merge the audio with the video while you create your 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 should be a good starting place. Of course you want to make sure that your frame1 isn't null before you try to record it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM