7

I want to create a video in Android with some images and an audio file. I searched for that and found useful libraries for that are opencv, javacv and ffmpeg. I used that libraries and followed the process give on the link - https://github.com/bytedeco/javacv

I want to know for the process

  1. Is Native Development Kit is necessary for the task. Is it necessary even if we have .so files with us, Since I achieved the task with using the resources I stated above by simply putting the .so files in the armeabi folder of the libs directory.
  2. I created the video but the requirement is that the video should be added with the sound
  3. Sound has to be recorded at run-time by the user and images has to be come from bitmaps which are also to be created at run-time.
  • yes you need to build the project using NDKbuild from command line that will generate the .so file in libs folder – Biraj Zalavadia May 21 '14 at 06:18
  • no need of ndk, you can use with out ndk build, see javacv as mentioned by @V.P. below.I resolved my problem. For audio recording, better record the audio and after saving, give the saved file url to the audio path..see my answer here http://stackoverflow.com/questions/20071981/combine-an-image-into-audio-file-and-make-a-video-file-in-android-programmatical/23887715#23887715 – shyam.y May 27 '14 at 11:33

1 Answers1

6

1). Please download this zip file from drive: https://drive.google.com/file/d/0B71R0Zw0m1zQM3RVOWNWM2poVHc/edit?usp=sharing

2) unzip the downloaded zip and put all file same like given libs/armeabi folder given below

enter image description here

3) use below code in your activty

please import follwing file.

 import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
    import com.googlecode.javacv.cpp.avcodec;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;

and actual code here

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

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

                File folder = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                String path = folder.getAbsolutePath() + "/Camera";
                ArrayList<String> paths = (ArrayList<String>) getListOfFiles(
                        path, "jpg");
                FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(path
                        + "/" + "test.mp4", 400, 400);
                videoPath = path + "/" + "test.mp4";
                try {
                    //recorder.setVideoCodec(5);
                    recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
                    //recorder.setFormat("3gp");
                    recorder.setFormat("mp4");
                    recorder.setFrameRate(frameRate);
                    recorder.setVideoBitrate(30);
                    startTime = System.currentTimeMillis();
                    recorder.start();
                    for (int i = 0; i <paths.size(); i++) {
                        IplImage image = cvLoadImage(path + "/" + paths.get(i));
                        long t = 3000 * (System.currentTimeMillis() - startTime);
                        if (t > recorder.getTimestamp()) {
                            recorder.setTimestamp(t);
                            recorder.record(image);
                        }
                    }
                    System.out.println("Total Time:- " + recorder.getTimestamp());
                    recorder.stop();
                } 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/mp4");
                startActivity(intent);
                Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT)
                        .show();
            };
        }.execute();

please free to asked if you have any query.

V.P.
  • 592
  • 8
  • 20
  • hi can you paste the full code, I want cvLoadImage() method code.Thanks – shyam.y May 22 '14 at 15:35
  • @sham.y please look my edited answer. may be you help :) – V.P. May 23 '14 at 10:13
  • Hi from where did you get OpenCV related jar files.If possible will you give that URL.In my jar files there is no cvLoadImage(). – shyam.y May 23 '14 at 10:43
  • please use this :- it's official link https://code.google.com/p/javacv/downloads/list – V.P. May 23 '14 at 10:46
  • hey my heartful thanks to you, your jar files and sample code helped me a lot..I just stucked with this problem with 2 days..thanks for the URL..But I have one more prob, how to combine audio with the images. – shyam.y May 23 '14 at 15:39
  • 1
    @sham.y : i can't implement before but please use this link https://groups.google.com/forum/#!topic/javacv/TdppyNYxeFU may be help you...... :) – V.P. May 26 '14 at 05:27
  • Hi, thanks for the URL, I resolved my problem.there is a problem in javacv 0.7 version .jar files.later I used javacv 0.8 version and CvloadImage issue also got fixed, package got changed to org.bytedeco.javacpp.opencv_highgui.cvLoadImage – shyam.y May 27 '14 at 10:27
  • hello @sham.y can you please send me your email or linkedIn profile on my mail id – V.P. May 27 '14 at 11:36
  • @V.P.: I want to add a time limit to a particular frame. Can we able to do that with this api. – Shashank_Itmaster Jun 09 '14 at 01:18
  • @Shashank_Itmaster : please change your code here long t = 3000 * (System.currentTimeMillis() - startTime); if (t > recorder.getTimestamp()) { recorder.setTimestamp(t); recorder.record(image); } – V.P. Jun 09 '14 at 06:56
  • @V.P. Now I wish to perform some video editing tasks on this video and apply filters like Sepia, Gray-scale etc to the video file. can we do this using javacv? – Smit Patel Jul 09 '14 at 05:36
  • @V.P can you upload a sample working code with this library ? Many people will be happy for that. Thanks – ralphgabb Apr 07 '15 at 05:54
  • You should really call `cvReleaseImage` after you've recorded the image if you don't want the memory to increase. – Pete Apr 11 '15 at 13:05
  • i got an error in **recorder.record(image);** Error:(69, 37) error: no suitable method found for record(IplImage) method FFmpegFrameRecorder.record(AVFrame) is not applicable (actual argument IplImage cannot be converted to AVFrame by method invocation conversion) method FFmpegFrameRecorder.record(Frame) is not applicable (actual argument IplImage cannot be converted to Frame by method invocation conversion) method FrameRecorder.record(Frame) is not applicable (actual argument IplImage cannot be converted to Frame by method invocation conversion) – Nikhil Borad Jul 17 '15 at 06:16
  • If someone is finding it hard to implement it in Android Studio, please use http://javacv4android.blogspot.ae/2014/02/how-to-import-javacv-libraries-to.html to know more. – San Oct 12 '15 at 09:42
  • @V.P, I am not able to play the video that I am generating using the above code, it says "Cant play this video" and the size is 258bytes for 2 images and I tried with 4 images and even then the size was same. Any idea what can be causing the issue ? I am able to fetch the file names right and pass it inside but the video is not proper. Any help would be much appreciated cause I've been sitting on this for 2 days now !! – San Oct 12 '15 at 10:19
  • I am unable to import FFmpegFrameRecorder class – Sukhbir Apr 29 '16 at 10:46