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 commands = new java.util.ArrayList(); 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(); } } }