liuxiaolong
2019-05-06 71af9c46c24b562acc00a71ec6c97c04eb048d9c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.cloud.device.utils;
 
import com.cloud.common.model.FileInfos;
import com.cloud.common.utils.FastDFSUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.*;
import java.util.List;
 
@Component
@Slf4j
public class CreatePhUtil {
 
    //user 用户模块 接口 端口
    @Value("${fdfsImgUrl.ffmpeg}")
    private String ffmpegUrl = "ffmpeg.exe";
 
    //user 用户模块 接口 端口
    @Value("${fdfsImgUrl.ffmpegSaveJpgUrl}")
    private String saveJpgUrl = "test.jpg";
 
    @Autowired
    private FastDFSUtil fastDFSUtil;
    //public static final String FFMPEG_PATH = "E:/ffmpeg/ffmpeg.exe";
    public String processImg(String veido_path) throws IOException {
 
        String window = System.getProperties().getProperty("os.name");
        log.info("===========os.name:" + window);
        log.info("===========file.separator:" + System.getProperties().getProperty("file.separator"));
        String ffmpeg_path = ffmpegUrl;
        if (StringUtils.isBlank(ffmpeg_path)) {
            if (window.contains("Windows")) ffmpeg_path = "ffmpeg.exe";
            else ffmpeg_path = "ffmpeg";
        }
//     ffmpeg -i rtsp://admin:a1234567@192.168.1.215:554/h264/ch1/main/av_stream
//     -vf select='eq(pict_type\,I)',setpts='N/(25*TB)'
//             -f image2 -s 1920*1080 -y C:\Users\25135\Desktop\test233.jpg
        log.info("veido_path=" + veido_path);
        List<String> commands = new java.util.ArrayList<String>();
        commands.add(ffmpeg_path);
        commands.add("-i");
        commands.add(veido_path);
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
//  commands.add("-ss");    commands.add("0.001");//这个参数是设置截取视频多少秒时的画面
        commands.add("-t");
        commands.add("0.001");  // -vframes 1
  /*commands.add("-vframes");
  commands.add("1");*/
        commands.add("-vf");
        commands.add("select='eq(pict_type\\,I)',setpts='N/(25*TB)'");
        commands.add("-s");
        commands.add("1920*1080");
        commands.add(saveJpgUrl);
        Process process = null;
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
//   builder.redirectErrorStream(true);
            process = builder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);
        String line = "";
        while ((line = br.readLine()) != null) {
        }
        log.info("流返回:" + line);
        if (line == null) {
            try {
                FileInputStream fis = new FileInputStream(saveJpgUrl);
                byte[] bytes = readStream(fis);
                FileInfos fileInfos = fastDFSUtil.uploadByByte(bytes, "jpg");
                return fileInfos.getPath();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    br.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (errorStream != null) {
                    errorStream.close();
                }
            }
        }
        return null;
    }
 
    public static void main(String[] args) {
        try {
            String s = new CreatePhUtil().processImg("rtsp://admin:a1234567@192.168.1.215:554/h264/ch1/main/av_stream");
            log.info("图片地址:" + s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static byte[] readStream(InputStream inStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
 
}