Playing a video with JavaCV and FFmpeg

為{幸葍}努か 提交于 2019-11-30 13:31:57

first you create the CanvasFrame then use "canvas.showImage(captured_frame);" instead of "recorder.record(captured_frame);"

Here is the code:

public class GrabberShow implements Runnable 
{
    final static int INTERVAL=40;///you may use interval
    IplImage image;
    static CanvasFrame canvas = new CanvasFrame("JavaCV player");


    public GrabberShow() 
   {
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
   }


    public static void convert(File file) 
    {

    FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file.getAbsolutePath());

    IplImage captured_frame = null;

    FrameRecorder recorder = null;
    //recorder = new FFmpegFrameRecorder("/mnt/sdcard/external_sd/videosteste/primeiroteste.mp4", 300, 300);
    recorder = new FFmpegFrameRecorder("D://temp.mp4", 300, 300);
    recorder.setVideoCodec(13);
    recorder.setFrameRate(30);
    recorder.setFormat("mp4");
    try {
        recorder.start();
        frameGrabber.start();
        while (true) {
            try {
                captured_frame = frameGrabber.grab();

                if (captured_frame == null) {
                    System.out.println("!!! Failed cvQueryFrame");
                    break;
                }
                //recorder.record(captured_frame);
                canvas.showImage(captured_frame);
                Thread.sleep(INTERVAL);
            } catch (Exception e) {
            }
        }
        recorder.stop();
        recorder.release();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public void run() 
{
    convert(new File("D://aes.mp4")); 
}

public static void main(String[] args) {
    GrabberShow gs = new GrabberShow();
    Thread th = new Thread(gs);
    th.start();
}
}
Aqeel Haider

Is there any way I can play any video on Android without recoding them?

Why are you recording the Video?? There is no need to record the video. JavaCv.

This is sample code for giving you the idea, how you can achieve it.

FrameGrabber grabber = new FrameGrabber(videoFile);
grabber.start();
BufferedImage image= null;
while((image=grabber.grab())!=null){
   // TODO set the image on the canvas or panel where ever you want. 
}
grabber.stop();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!