14

I gone through the tutorial of ffmpeg library to combine the image into audio file.

This is looking very complex and getting error with environment value is null.

I researched a lot but didn't find any solution.

Is there any way in Android to merge an image and an audio file to make a video?

Jagdish
  • 2,238
  • 4
  • 19
  • 50

2 Answers2

9

I didnt compiled FFMPEG, simply I used javacv for android and resolved my problem to combine audio and image and make it as video file. Steps: First Follow the steps provided here and then place the below code in your activity and make sure that to change the path of input files and output file.

 new AsyncTask<Void, Void, Void>() {
            ProgressDialog dialog;
            FFmpegFrameRecorder recorder;
            protected void onPreExecute() {
                dialog = new ProgressDialog(RecordActivity.this);
                dialog.setMessage("Genrating video, Please wait.........");
                dialog.setCancelable(false);
                dialog.show();
            };

            @Override
            protected Void doInBackground(Void... arg0) {

                File folder = Environment.getExternalStorageDirectory();
                String path = folder.getAbsolutePath() + "/DCIM/Camera";
               // ArrayList<String> paths = (ArrayList<String>) getListOfFiles(path, "jpg");
                long millis = System.currentTimeMillis();

                videoPath = path + "/" + "test_sham_"+millis+".3gp";

                try {



                //audio grabber
                FrameGrabber grabber2 = new FFmpegFrameGrabber(folder.getAbsolutePath()+"/Samsung/Music/Over_the_horizon.mp3"); 

                //video grabber
                FrameGrabber grabber1 = new FFmpegFrameGrabber(path+"/20140527_133034.jpg"); 
                    grabber1.start();

                    grabber2.start();

                  recorder = new FFmpegFrameRecorder(path
                          + "/" + "test_sham_"+millis+".3gp",  grabber1.getImageWidth(), grabber1.getImageHeight(),2);

                    //recorder.setVideoCodec(5);
                    recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
                     // recorder.setVideoCodec(avcodec.AV_CODEC_ID_MP4ALS);

                    recorder.setFormat("3gp");
                  //  recorder.setFormat("mp4");
                    recorder.setFrameRate(frameRate);
                    recorder.setSampleRate(grabber2.getSampleRate());
                    recorder.setVideoBitrate(30);
                    startTime = System.currentTimeMillis();
                    recorder.start();

                    Frame frame1, frame2 = null; 

                    while ((frame1 = grabber1.grabFrame()) != null || 

                          (frame2 = grabber2.grabFrame()) != null) { 

                        recorder.record(frame1); 

                        recorder.record(frame2); 

                    } 

                    recorder.stop(); 

                    grabber1.stop(); 

                    grabber2.stop(); 




                    System.out.println("Total Time:- " + recorder.getTimestamp());

                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }

            protected void onPostExecute(Void result) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_VIEW); 
                intent.setDataAndType(Uri.parse(videoPath), "video/3gp");
                startActivity(intent);
                Toast.makeText(RecordActivity.this, "Done:::"+videoPath, Toast.LENGTH_SHORT)
                        .show();
            };
        }.execute();

For references:ref1, ref2

Community
  • 1
  • 1
sham.y
  • 1,131
  • 13
  • 15
  • I tried your solution, but here i am getting " org.bytedeco.javacv.FrameRecorder$Exception: avio_open error() error -13" exception. Can you please help me in resolving this exception. – amit singh Jun 30 '14 at 12:43
  • The above problem is resolved, now i am using this- "recorder.setFormat("rtsp");", instead of setting format to "mp4". But now i am facing a new issue. In the while loop, i am getting this- "Fatal signal 8 (SIGFPE) at 0x000035be (code=-6), thread 13772 (AsyncTask #1)". Please help me if you have solution to this. – amit singh Jun 30 '14 at 13:56
  • HI I think there might be issue with the jar files you are using, what version jar files are you using.Please use above mentioned jar files, definitely the above references will help. – sham.y Jul 01 '14 at 14:24
  • Currently I am using "JavaCV 0.8 binary package", I hope this is the latest package !! I am still stuck in this problem. – amit singh Jul 02 '14 at 06:57
  • This example had some issues. But it was a good guide to achieving what I needed. One of the issues was that the recording took forever to finish. – Darush Aug 28 '19 at 08:30
1

ffmpeg does not come with android phones. You need to compile it yourself for android and deploy on the device.

FFmpeg on Android

Caner
  • 49,709
  • 33
  • 153
  • 169
  • https://play.google.com/store/apps/details?id=com.netcompss.ffmpeg4android not available any more on Google play, please update your answer – Kirtikumar A. Jan 08 '20 at 07:10