1. 程式人生 > >javacv解碼、錄製視訊,儲存圖片

javacv解碼、錄製視訊,儲存圖片

貌似解碼得到的每幀影象是RGB24的,要儲存Bitmap.Config.ARGB_8888,要轉一下cvCvtColor(img,tempImage,CV_BGR2RGBA)。
    mGrabber = new FFmpegFrameGrabber(mMvPath);
    mGrabber.start();

    opencv_core.IplImage tempImage = opencv_core.IplImage.create(mGrabber.getImageWidth(), mGrabber.getImageHeight(), opencv_core.IPL_DEPTH_8U, 4);
    Bitmap bmp = Bitmap.createBitmap(mGrabber.getImageWidth(),mGrabber.getImageHeight(), Bitmap.Config.ARGB_8888);

    String fileName = mMvPath.replace(".mp4", "_edited.mp4");
    FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(fileName, mGrabber.getImageWidth(), mGrabber.getImageHeight(),mGrabber.getAudioChannels());
    recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); //avcodec.AV_CODEC_ID_H264  //AV_CODEC_ID_MPEG4
    recorder.setFormat("mp4");
    recorder.setFrameRate(mGrabber.getFrameRate());
    recorder.setSampleFormat(mGrabber.getSampleFormat());  //
    recorder.setSampleRate(mGrabber.getSampleRate());
    recorder.setFrameRate(mGrabber.getFrameRate());
    recorder.start();

    while (true) {
        Frame frame = mGrabber.grabFrame();

        if (frame == null)
            break;

        opencv_core.IplImage img = frame.image;//儲存BMP
        cvCvtColor(img,tempImage,CV_BGR2RGBA);
        IntBuffer intBuf = tempImage.getIntBuffer();
        bmp.copyPixelsFromBuffer(intBuf);
        ImageUtil.saveBitmap(bmp);

        recorder.record(img);//錄製
    }

    recorder.stop();
    recorder.release();
    mGrabber.stop();

    if (mEvent != null){
        mEvent.onDecordFinish();
    }

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