liuxiaolong
2019-05-06 0bc4f4c791437b39b8c30624f5c21f8c855dc61d
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
package com.cloud.retrieve.utils;
 
import com.cloud.common.model.FileInfos;
import com.cloud.common.utils.FastDFSUtil;
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;
import java.util.UUID;
 
@Component
public class CreatePhUtil {
 
    //user 用户模块 接口 端口
    @Value("${fdfsImgUrl.ffmpeg}")
    private String ffmpegUrl = "D:\\ffmpeg-20190219-ff03418-win64-static\\bin\\ffmpeg.exe";
 
    //user 用户模块 接口 端口
    @Value("${fdfsImgUrl.ffmpegSaveJpgUrl}")
    private String saveJpgUrl = "C:\\Users\\25135\\Desktop\\test.jpg";
 
    @Autowired
    private FastDFSUtil fastDFSUtil;
    //public static final String FFMPEG_PATH = "E:/ffmpeg/ffmpeg.exe";
    public boolean processImg(String veido_path) throws IOException {
     /*File file = new File(veido_path);    if (!file.exists()) {
         System.err.println("路径[" + veido_path + "]对应的视频文件不存在!");    return false;
     }*/
        String window = System.getProperties().getProperty("os.name");
        System.out.println("===========os.name:" + window);
        System.out.println("===========file.separator:" + System.getProperties().getProperty("file.separator"));
        String ffmpeg_path = ffmpegUrl;
        if (ffmpeg_path == null) {
            if (window.contains("Windows")) ffmpeg_path = "ffmpeg.exe";
            else ffmpeg_path = "ffmpeg";
        }
        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("3.001");//这个参数是设置截取视频多少秒时的画面
        commands.add("-t");
        commands.add("0.001");  // -vframes 1
        commands.add("-vframes");
        commands.add("1");
        commands.add("-s");
        commands.add("1200x1000");
        commands.add(saveJpgUrl);
        Process process = null;
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.redirectErrorStream(true);
            process = builder.start();
 
            System.out.println("截取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理
        InputStream inputStream = process.getInputStream();
        InputStream in = process.getInputStream();
        byte[] bytes = new byte[10240];
        System.out.print("正在进行截图,请稍候");
        while (in.read(bytes) != -1) {
        }
        System.out.println("截圖長度:" + bytes.length);
        FileInfos jpg = fastDFSUtil.uploadByByte(bytes, "jpg");
        System.out.println(jpg);
        /* InputStream errorStream = process.getErrorStream();
         InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
         BufferedReader br = new BufferedReader(inputStreamReader);
         String line = "";
         while ((line = br.readLine()) != null){
         }
         System.out.println("流返回:"+line);
         if (br != null) {
             br.close();
         }
         if (inputStreamReader != null) {
             inputStreamReader.close();
         }
         if (errorStream != null) {
             errorStream.close();
         }*/
        try {
            Thread.sleep(400);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        return true;
 
    }
 
    public static void main(String[] args) {
        try {
            new CreatePhUtil().processImg("rtsp://admin:a1234567@192.168.1.215:554/h264/ch1/main/av_stream");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}