如何使用JavaCV/OpenCV从安卓系统SD卡中的图像创建视频



抱歉英语不好…:(

我想从SD卡上的图像创建视频,并且每个帧/图像在屏幕上停留至少1秒或任何值(如2,3,4秒)。

为此,我使用javacv/opencv库

我使用了下面的代码,但没有为我工作,我得到了下面给出的错误。

我也把我的方法和错误日志,请尽快帮助我。提前感谢

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();
                 }
    }

但我收到了一个错误,请

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)

请帮帮我!!!:)提前感谢

//mp4视频

这是工作代码。我正在使用这个代码来制作视频,这些图像位于SD卡中。

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();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新