From 3e8e2bfdfbcf06659d7734d3e1cb63fd0f46c35d Mon Sep 17 00:00:00 2001 From: czt <czt18638530771@163.com> Date: 星期五, 03 一月 2025 15:30:22 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- src/main/java/com/fzzy/api/utils/FileUtils.java | 236 +++++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 167 insertions(+), 69 deletions(-) diff --git a/src/main/java/com/fzzy/api/utils/FileUtils.java b/src/main/java/com/fzzy/api/utils/FileUtils.java index df2a8e2..eedfb43 100644 --- a/src/main/java/com/fzzy/api/utils/FileUtils.java +++ b/src/main/java/com/fzzy/api/utils/FileUtils.java @@ -1,9 +1,16 @@ package com.fzzy.api.utils; import com.fzzy.data.ConfigData; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import ws.schild.jave.Encoder; +import ws.schild.jave.EncoderException; +import ws.schild.jave.MultimediaObject; +import ws.schild.jave.encode.AudioAttributes; +import ws.schild.jave.encode.EncodingAttributes; +import ws.schild.jave.encode.VideoAttributes; import java.io.ByteArrayOutputStream; import java.io.File; @@ -14,86 +21,177 @@ /** * 灏嗘枃浠惰浆涓轰簩杩涘埗娴佸伐鍏风被 - * @author czt * + * @author czt */ +@Slf4j @Service(FileUtils.BEAN_ID) public class FileUtils { - public static final String BEAN_ID = "base.fileUtil"; + public static final String BEAN_ID = "base.fileUtil"; - @Autowired - private ConfigData configData; + @Autowired + private ConfigData configData; - /** - * 鏍规嵁鏂囦欢璺緞灏嗘枃浠惰浆涓轰簩杩涘埗鏂囦欢娴佸瓧绗︿覆 - * @param filePath:鏂囦欢璺緞 - * @return - */ - public static String fileToByteString(String filePath) { - byte[] bytes = file2byte(filePath); - return toHexString(bytes); - } + /** + * 鏍规嵁鏂囦欢璺緞灏嗘枃浠惰浆涓轰簩杩涘埗鏂囦欢娴佸瓧绗︿覆 + * + * @param filePath:鏂囦欢璺緞 + * @return + */ + public static String fileToByteString(String filePath) { + byte[] bytes = file2byte(filePath); + return toHexString(bytes); + } - /** - * 鏍规嵁鏂囦欢璺緞灏嗘枃浠惰浆涓轰簩杩涘埗鏁扮粍 - * @param filePath:鏂囦欢璺緞 - * @return - */ - public static byte[] file2byte(String filePath) { - byte[] buffer = null; - try { - File file = new File(filePath); - FileInputStream fis = new FileInputStream(file); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - byte[] b = new byte[1024]; - int n; - while ((n = fis.read(b)) != -1) { - bos.write(b, 0, n); - } - fis.close(); - bos.close(); - buffer = bos.toByteArray(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return buffer; - } - - /** - * 灏嗕簩杩涘埗鏁扮粍杞负瀛楃涓� - * @param byteArray - * @return - */ - private static String toHexString(byte[] byteArray) { - if (byteArray == null || byteArray.length < 1) - throw new IllegalArgumentException( - "this byteArray must not be null or empty"); + /** + * 鏍规嵁鏂囦欢璺緞灏嗘枃浠惰浆涓轰簩杩涘埗鏁扮粍 + * + * @param filePath:鏂囦欢璺緞 + * @return + */ + public static byte[] file2byte(String filePath) { + byte[] buffer = null; + try { + File file = new File(filePath); + FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] b = new byte[1024]; + int n; + while ((n = fis.read(b)) != -1) { + bos.write(b, 0, n); + } + fis.close(); + bos.close(); + buffer = bos.toByteArray(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return buffer; + } - final StringBuilder hexString = new StringBuilder(); - for (int i = 0; i < byteArray.length; i++) { - if ((byteArray[i] & 0xff) < 0x10)// 0~F鍓嶉潰涓嶉浂 - hexString.append("0"); - hexString.append(Integer.toHexString(0xFF & byteArray[i])); - } - return hexString.toString().toLowerCase(); - } + /** + * 灏嗕簩杩涘埗鏁扮粍杞负瀛楃涓� + * + * @param byteArray + * @return + */ + private static String toHexString(byte[] byteArray) { + if (byteArray == null || byteArray.length < 1) + throw new IllegalArgumentException( + "this byteArray must not be null or empty"); - public String getInoutFilePath(Date date) { - if (null == date) { - date = new Date(); - } + final StringBuilder hexString = new StringBuilder(); + for (int i = 0; i < byteArray.length; i++) { + if ((byteArray[i] & 0xff) < 0x10)// 0~F鍓嶉潰涓嶉浂 + hexString.append("0"); + hexString.append(Integer.toHexString(0xFF & byteArray[i])); + } + return hexString.toString().toLowerCase(); + } - String basePath = configData.getImgPath() + "INOUT/" - + DateFormatUtils.format(date, "yyyyMM") + "/"; + public String getInoutFilePath(Date date) { + if (null == date) { + date = new Date(); + } - File file = new File(basePath); - if (!file.exists()) { - file.mkdirs(); - } + String basePath = configData.getImgPath() + "INOUT/" + + DateFormatUtils.format(date, "yyyyMM") + "/"; - return basePath; - } + File file = new File(basePath); + if (!file.exists()) { + file.mkdirs(); + } + + return basePath; + } + + public String getSnapFilePath(Date date) { + if (null == date) { + date = new Date(); + } + + String basePath = configData.getImgPath() + "SNAP/" + + DateFormatUtils.format(date, "yyyyMM") + "/"; + File file = new File(basePath); + if (!file.exists()) { + file.mkdirs(); + } + return basePath; + } + + public String getCommonFilePath(Date date) { + if (null == date) + date = new Date(); + + String basePath = configData.getImgPath() + "COMMON/" + + DateFormatUtils.format(date, "yyyyMM") + "/"; + + File file = new File(basePath); + if (!file.exists()) { + file.mkdirs(); + } + return basePath; + } + + /** + * 瑙嗛鍘嬬缉 + * + * @param source 婧愭枃浠� + * @param target 鐩爣鏂囦欢 + * @param rate 鍘嬬缉姣� + */ + public static void compre(File source, File target, Integer rate) throws EncoderException { + try { + log.info("---------------寮�濮嬪帇缂�---------------"); + long start = System.currentTimeMillis(); + + // 闊抽缂栫爜灞炴�ч厤缃� + AudioAttributes audio = new AudioAttributes(); + audio.setCodec("libmp3lame"); + // 璁剧疆闊抽姣旂壒鐜�,鍗曚綅:b (姣旂壒鐜囪秺楂橈紝娓呮櫚搴�/闊宠川瓒婂ソ锛屽綋鐒舵枃浠朵篃灏辫秺澶� 56000 = 56kb) + // audio.setBitRate(new Integer(56_000)); + audio.setBitRate(new Integer(10)); + // 璁剧疆閲嶆柊缂栫爜鐨勯煶棰戞祦涓娇鐢ㄧ殑澹伴亾鏁帮紙1 =鍗曞0閬擄紝2 = 鍙屽0閬擄紙绔嬩綋澹帮級锛� + audio.setChannels(1); + // 閲囨牱鐜囪秺楂樺0闊崇殑杩樺師搴﹁秺濂斤紝鏂囦欢瓒婂ぇ + // audio.setSamplingRate(new Integer(44100)); + audio.setSamplingRate(new Integer(22050)); + // 瑙嗛缂栫爜灞炴�ч厤缃� + VideoAttributes video = new VideoAttributes(); + // 璁剧疆缂栫爜 + video.setCodec("h254"); + //璁剧疆闊抽姣旂壒鐜�,鍗曚綅:b (姣旂壒鐜囪秺楂橈紝娓呮櫚搴�/闊宠川瓒婂ソ锛屽綋鐒舵枃浠朵篃灏辫秺澶� 5600000 = 5600kb) + // video.setBitRate(new Integer(5_600_000 / rate)); + video.setBitRate(10 / rate); + + // 璁剧疆瑙嗛甯х巼锛堝抚鐜囪秺浣庯紝瑙嗛浼氬嚭鐜版柇灞傦紝瓒婇珮璁╀汉鎰熻瓒婅繛缁級,杩欓噷 闄�1000鏄负浜嗗崟浣嶈浆鎹� + video.setFrameRate(15); + + + // 缂栫爜璁剧疆 + EncodingAttributes attr = new EncodingAttributes(); + attr.setOutputFormat("mp4"); + attr.setAudioAttributes(audio); + attr.setVideoAttributes(video); + + // 璁剧疆鍊肩紪鐮� + Encoder ec = new Encoder(); + ec.encode(new MultimediaObject(source), target, attr); + + + log.info("---------------缁撴潫鍘嬬缉---------------"); + long end = System.currentTimeMillis(); + log.info("鍘嬬缉鍓嶅ぇ灏忥細" + source.length() + " 鍘嬬缉鍚庡ぇ灏忥細" + target.length()); + log.info("鍘嬬缉鑰楁椂锛�" + (end - start)); + + } catch (EncoderException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + + } } -- Gitblit v1.9.3