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 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("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; } }