简体   繁体   中英

How to create the video from images located in SD card in Android using JavaCV/OpenCV

Sorry for bad English...:(

I want to create video from images located on SD Card and also each frame/image stay on screen min 1 sec or any value(like 2,3,4 sec).

I am using the javacv/opencv library for that.

I have use the below code but not work for me i get error given below.

I have put my method and error log also, please help me as soon as possible. Thanks in advance

public void createVideo()
    {
          String path ="/mnt/sdcard/images/";
          File folder = new File(path);
              File[]  listOfFiles = folder.listFiles(); 
              IplImage[]  iplimage = (IplImage[]) new IplImage[listOfFiles.length];
              if(listOfFiles.length>0)
              {
                 for (int j = 0; j < listOfFiles.length; j++)
                 {
                     String files="";      
                     if (listOfFiles[j].isFile())
                     {
                         files = listOfFiles[j].getName();
                     }  
                      String[] tokens = files.split("\\.(?=[^\\.]+$)");
                      String name=tokens[0];
                      System.out.println(" j " + name);
                      iplimage[j]=cvLoadImage("/mnt/sdcard/images/"+name+".jpg");
                 }

              }

              File videopath = new File(path);
              videopath.mkdirs();
              FFmpegFrameRecorder recorder = new  FFmpegFrameRecorder(path+"video"+System.currentTimeMillis()+".mp4",320,480);

               try {
                   recorder.setVideoCodec(13); //CODEC_ID_MPEG4                           
                   //CODEC_ID_MPEG1VIDEO
                   recorder.setVideoBitrate(50);
                   recorder.setFrameRate(10);                    
                   recorder.setPixelFormat(2); //PIX_FMT_YUV420P
                   recorder.start();
                   int x = 0;
                   int y = 0;
                  for (int i=0;i< 300 && x<listOfFiles.length;i++)
                     {
                       recorder.record(iplimage[x]);
                      if (i>(y+10)) {
                          y=y+1;
                          x++;
                      }
                     }
                   recorder.stop();
                  }
               catch (Exception e){
                   e.printStackTrace();
                 }
    }

But i get this one error please

04-09 12:36:26.130: W/System.err(10201): com.googlecode.javacv.FrameRecorder$Exception: avcodec_open2() error -22: Could not open video codec.
04-09 12:36:26.131: W/System.err(10201):     at com.googlecode.javacv.FFmpegFrameRecorder.startUnsafe(FFmpegFrameRecorder.java:492)
04-09 12:36:26.131: W/System.err(10201):     at com.googlecode.javacv.FFmpegFrameRecorder.start(FFmpegFrameRecorder.java:267)
04-09 12:36:26.131: W/System.err(10201):     at com.example.imagetovideo.MainActivity.createVideo(MainActivity.java:107)
04-09 12:36:26.131: W/System.err(10201):     at com.example.imagetovideo.MainActivity$1.doInBackground(MainActivity.java:57)
04-09 12:36:26.131: W/System.err(10201):     at com.example.imagetovideo.MainActivity$1.doInBackground(MainActivity.java:1)
04-09 12:36:26.131: W/System.err(10201):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-09 12:36:26.131: W/System.err(10201):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-09 12:36:26.131: W/System.err(10201):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-09 12:36:26.131: W/System.err(10201):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-09 12:36:26.131: W/System.err(10201):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-09 12:36:26.131: W/System.err(10201):     at java.lang.Thread.run(Thread.java:841)

please help me!!!!:) Thank in advance

// mp4 video

This is working code. I am using this code to make video from images which are located in sdcard.

private class VideoCreate extends AsyncTask<Void, Void, Void> {

    String Videopath;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        System.out.println("VideoCreate onPreExecute call ");
        dismissProgressDialog();
        showProgressDialog();
    }

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

        // try {

        temp_video_file_name_last = System.currentTimeMillis()
                + FileUtils.VIDEO_TYPE_MP4;
        // Vidopah = FileUtils.getScreenShotDirectory(getActivity(),
        // FileUtils.ATTACHMENT_TYPE_VIDEO)
        // + File.separator
        // + video_file_name_last;
        Videopath = getTempVideoDirectory() + File.separator
                + temp_video_file_name_last;

        File pF = new File(tempVideoShotDirecotry);
        if (!pF.exists()) {
            System.out.println("pF not exist");
            pF.mkdirs();
        }
        if (pF.exists()) {
            System.out.println("pF  exist");
        }
        int imgCounter = 0;

        File folder = new File(tempVideoShotDirecotry);

        File[] listOfFiles = folder.listFiles();
        System.out.println("listOfFiles size::" + listOfFiles.length);
        if (listOfFiles.length > 0) {

            for (int j = 0; j < listOfFiles.length; j++) {

                if (listOfFiles[j].getAbsolutePath().endsWith(".JPEG")
                        || listOfFiles[j].getAbsolutePath().endsWith(
                                ".jpeg")
                        || listOfFiles[j].getAbsolutePath()
                                .endsWith(".JPG")
                        || listOfFiles[j].getAbsolutePath()
                                .endsWith(".jpg")
                        || listOfFiles[j].getAbsolutePath()
                                .endsWith(".png")) {
                    System.out.println(j + " position listOfFiles::"
                            + listOfFiles[j].getAbsolutePath());
                    Bitmap bmp = BitmapFactory.decodeFile(listOfFiles[j]
                            .getAbsolutePath());
                    if (bmp != null) {
                        System.out.println("bmp is not null");
                        ++imgCounter;
                    }

                }

            }

        }

        Log.e("", "Intial Execute");

        FFmpegFrameRecorder recorder = null;
        recorder = new FFmpegFrameRecorder(Videopath, ScreenShotWidth,
                ScreenShotHeight);
        Log.d("Display",
                (new StringBuilder(String.valueOf(ScreenShotWidth)))
                        .append(":").append(ScreenShotHeight).toString());

        // working
        recorder.setVideoCodec(13);
        recorder.setFormat("mp4");
        recorder.setFrameRate(1.0D);
        recorder.setVideoQuality(1.0D);
        recorder.setVideoBitrate(40000);


        long l = System.currentTimeMillis();
        try {
            recorder.start();
        } catch (org.bytedeco.javacv.FrameRecorder.Exception e1) {
            e1.printStackTrace();
        }
        System.out.println("imgCounter size::" + imgCounter);



        for (int i = 0; i < imgCounter; i++) {
            org.bytedeco.javacpp.opencv_core.IplImage iplimageTemp = opencv_highgui
                    .cvLoadImage(listOfFiles[i].getAbsolutePath());
            long l1 = 1000L * (System.currentTimeMillis() - l);
            if (l1 < recorder.getTimestamp()) {
                l1 = 1000L + recorder.getTimestamp();
            }
            System.out.println(i + " timestamp:::" + l1);
            recorder.setTimestamp(l1);
            try {
                recorder.record(iplimageTemp);
            } catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
                e.printStackTrace();
            }
        }

        Log.e("", "End Execute");
        try {
            recorder.stop();
        } catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
            e.printStackTrace();
        }

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

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dismissProgressDialog();
        alert.setCustomToast("Video is Generated");

        if (isTempVideoAvailableInStorage()) {

            new MergeAudioAndVideo().execute();
        }
    }
}

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