南昌市物联网技防平台-公安版
zengh
2021-06-04 c926acaadc3d98fd8ba8926466b842f1edb3aee3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.springblade.jfpt.rvideo.controller;
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.*;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class RecordVideoThread implements Runnable {
    public String streamURL;// 流地址(测试可以用obs推流)
    public String filePath;// 文件路径
    public Long timesSec = 100L;// 停止录制时长 0为不限制时长
    public String fileFormat = "mp4";//录制的文件格式
    public boolean isAudio = false;//是否录制声音
 
    public void setStreamURL(String streamURL) {
        this.streamURL = streamURL;
    }
 
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
 
    @Override
    public void run() {
        // 获取视频源
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(streamURL);
        FFmpegFrameRecorder recorder = null;
        try {
            grabber.start();
            Frame frame = grabber.grabFrame();
            createFile();
            //"rtmp://192.168.1.225:1935/live/home"
            // 流媒体输出地址,分辨率(长,高),是否录制音频(0:不录制/1:录制)
            recorder = new FFmpegFrameRecorder(filePath, 1280, 720, isAudio ? 1 : 0);
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);// 直播流格式
            recorder.setFormat(fileFormat);// 录制的视频格式
            recorder.setFrameRate(25);// 帧数
            //百度翻译的比特率,默认400000,但是我400000贼模糊,调成800000比较合适
            recorder.setVideoBitrate(800000);
            recorder.start();
            System.out.println("推流开始");
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    RecordVideoThread thread = new RecordVideoThread();
//                    thread.pushVideoAsRTSP(1, null);
//                }
//            }).start();
            // 计算结束时间
            //long endTime = System.currentTimeMillis() + timesSec * 1000;
            // 如果没有到录制结束时间并且获取到了下一帧则继续录制
            while (frame != null) {
                System.out.println("开始");
                recorder.record(frame);//录制
                frame = grabber.grabFrame();//获取下一帧
                System.out.println("结束");
            }
            recorder.record(frame);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //停止录制
            if (null != grabber) {
                try {
                    grabber.stop();
                } catch (FrameGrabber.Exception e) {
                    e.printStackTrace();
                }
            }
            if (recorder != null) {
                try {
                    recorder.stop();
                } catch (FrameRecorder.Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("录制完成,录制时长:" + timesSec + "秒");
        }
    }
 
    private void createFile() {
        File outFile = new File(filePath);
        if (filePath.isEmpty() || !outFile.exists() || outFile.isFile()) {
            try {
                outFile.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
 
    }
}