Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine an image into audio file and make a video file in android programmatically

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?

like image 797
Jagdish Avatar asked Nov 19 '13 12:11

Jagdish


2 Answers

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

like image 146
sham.y Avatar answered Sep 17 '22 13:09

sham.y


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

FFmpeg on Android

like image 27
Caner Avatar answered Sep 16 '22 13:09

Caner