javacv拉流保存为视频

maven依赖

查看 javacv实现rtsp转rtmp(支持拉一路推多路)

代码实现

//inputFile 输入地址,rtsp/rtmp
//outputFile 保存文件地址  d:\\1.flv
//m 秒数
public static void  frameRecord(String inputFile, String outputFile,Long m) throws Exception{
    boolean isStart = true;
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);
    grabber.setOption("rtsp_transport","tcp");
    //帧数
    grabber.setFrameRate(25);
    grabber.setVideoBitrate(2000000);
    FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, 1280, 720,0);
    recorder.setFrameRate(25);
    recorder.setVideoBitrate(2000000);
    recordByFrame(grabber, recorder, isStart,m);
}

private static void recordByFrame(FFmpegFrameGrabber grabber, FFmpegFrameRecorder recorder, Boolean status,Long m)
        throws Exception{
    try {
        grabber.start();
        recorder.setVideoCodec(grabber.getVideoCodec());
        recorder.start();
        Frame frame = null;
        //计算帧数
        long index=m*25;
        //已经录制帧数
        long finishIndex=0L;
        //Long start=CommonUtil.getSecondTimestamp();
        while (status&& (frame = grabber.grabFrame()) != null) {
            recorder.record(frame);
            finishIndex++;
            //Long end=CommonUtil.getSecondTimestamp();
            if(index<=finishIndex){
                System.out.println("完成录制");
                status=false;
                break;
            }
        }
        recorder.stop();
        grabber.stop();
    } finally {
        if (grabber != null) {
            grabber.stop();
        }
    }
}

你可能感兴趣的:(流媒体)