diff --git a/src/main/java/com/github/tonydeng/fmj/handler/DefaultCallbackHandler.java b/src/main/java/com/github/tonydeng/fmj/handler/DefaultCallbackHandler.java index f1ea079..7cf458d 100644 --- a/src/main/java/com/github/tonydeng/fmj/handler/DefaultCallbackHandler.java +++ b/src/main/java/com/github/tonydeng/fmj/handler/DefaultCallbackHandler.java @@ -1,28 +1,30 @@ package com.github.tonydeng.fmj.handler; import com.github.tonydeng.fmj.runner.BaseCommandOption; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.google.common.collect.Lists; +import lombok.Cleanup; +import lombok.extern.slf4j.Slf4j; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.List; /** * Created by tonydeng on 15/4/20. */ +@Slf4j public class DefaultCallbackHandler implements ProcessCallbackHandler { - private static final Logger log = LoggerFactory.getLogger(DefaultCallbackHandler.class); - + @Override public String handler(InputStream inputStream) throws IOException { - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, BaseCommandOption.UTF8)); - StringBuffer sb = new StringBuffer(); + @Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, BaseCommandOption.UTF8)); + List buffer = Lists.newArrayList(); String line; while ((line = reader.readLine()) != null) { - sb.append(line).append("\n"); + buffer.add(line); } - return sb.toString(); + return String.join("\n", buffer); } } diff --git a/src/main/java/com/github/tonydeng/fmj/runner/BaseCommandOption.java b/src/main/java/com/github/tonydeng/fmj/runner/BaseCommandOption.java index 92cfb5d..2b99cb5 100644 --- a/src/main/java/com/github/tonydeng/fmj/runner/BaseCommandOption.java +++ b/src/main/java/com/github/tonydeng/fmj/runner/BaseCommandOption.java @@ -2,8 +2,8 @@ import com.github.tonydeng.fmj.model.VideoInfo; import com.google.common.collect.Lists; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import java.util.Collections; import java.util.List; @@ -11,14 +11,14 @@ /** * Created by tonydeng on 15/4/15. */ +@Slf4j public class BaseCommandOption { - private static final Logger log = LoggerFactory.getLogger(BaseCommandOption.class); private static boolean isWin = false; private static boolean isLinux = false; - private static Integer DEFAULT_IO_THREADS = 1; + private static Integer defaultIoThreads = 1; - private static List FFMPEG_BINARY; - private static List FFPROBE_BINARY; + private static List ffmpegBinary; + private static List ffprobeBinary; public static final String WINCMD = "cmd"; public static final String WINCMDOP = "/c"; @@ -56,57 +56,63 @@ public class BaseCommandOption { static { String env = System.getProperty("os.name"); - if (log.isDebugEnabled()) - log.debug("current operate system :{}", env); + log.debug("current operate system :{}", env); - if (null != env) { + if (StringUtils.isNotEmpty(env)) { String os = env.toLowerCase(); - if (os.indexOf("win") >= 0) { + if (os.contains("win")) { isWin = true; - } else if (os.indexOf("linux") >= 0 || os.indexOf("mac") >= 0) { + } else if (os.contains("linux") || os.contains("mac")) { isLinux = true; } } //获得当前机器的CPU核数 - DEFAULT_IO_THREADS = Runtime.getRuntime().availableProcessors(); + defaultIoThreads = Runtime.getRuntime().availableProcessors(); if (log.isDebugEnabled()) { - log.debug("isWindows : '{}' or isLinux:'{}' DEFAULT_IO_THREADS:'{}'", - isWin, isLinux, DEFAULT_IO_THREADS); + log.debug("isWindows : '{}' or isLinux:'{}' defaultIoThreads:'{}'", + isWin, isLinux, defaultIoThreads); } } + private BaseCommandOption() { + + } + /** * 得到ffmpeg命令参数 + * * @return */ public static List getFFmpegBinary() { - if (FFMPEG_BINARY == null) { + if (ffmpegBinary == null) { if (isWin) { - FFMPEG_BINARY = Lists.newArrayList(WINCMD, WINCMDOP, FFMPEG); + ffmpegBinary = Lists.newArrayList(WINCMD, WINCMDOP, FFMPEG); } else if (isLinux) { - FFMPEG_BINARY = Lists.newArrayList(LINUXCMD, FFMPEG); + ffmpegBinary = Lists.newArrayList(LINUXCMD, FFMPEG); } } - return FFMPEG_BINARY; + return ffmpegBinary; } /** * 得到ffprobe命令 + * * @return */ public static List getFFprobeBinary() { - if (null == FFPROBE_BINARY) { + if (null == ffprobeBinary) { if (isWin) { - FFPROBE_BINARY = Lists.newArrayList(WINCMD, WINCMDOP, FFPROBE); + ffprobeBinary = Lists.newArrayList(WINCMD, WINCMDOP, FFPROBE); } else if (isLinux) { - FFPROBE_BINARY = Lists.newArrayList(LINUXCMD, FFPROBE); + ffprobeBinary = Lists.newArrayList(LINUXCMD, FFPROBE); } } - return FFPROBE_BINARY; + return ffprobeBinary; } /** * 视频输入的命令参数 + * * @param input * @return */ @@ -118,6 +124,7 @@ public static List toInputCommonsCmdArrays(String input) { /** * 截图的命令参数 + * * @param input * @param output * @param shotSecond @@ -127,7 +134,7 @@ public static List toInputCommonsCmdArrays(String input) { public static List toScreenshotCmdArrays(String input, String output, int shotSecond, VideoInfo vi) { if (vi != null && vi.getSize() > 0) { List commands = Lists.newArrayList(); - if (vi.getDuration() < Long.valueOf(shotSecond).longValue()) { + if (vi.getDuration() < shotSecond) { shotSecond = 1; } commands.add(SS); @@ -147,11 +154,12 @@ public static List toScreenshotCmdArrays(String input, String output, in commands.add(output); return commands; } - return Collections.EMPTY_LIST; + return Collections.emptyList(); } /** * 转成HLS的命令参数 + * * @param input * @param m3u8Output * @param cutSecond @@ -168,7 +176,7 @@ public static List toHLSCmdArrays(String input, String m3u8Output, int c CA, FORMAT_ACC, STRICT, "-2", F, FORMAT_HLS, - THREADS,DEFAULT_IO_THREADS.toString(), + THREADS, defaultIoThreads.toString(), HLS_TIME, String.valueOf(cutSecond), HLS_LIST_SIZE, "0", HLS_WRAP, "0", @@ -178,11 +186,12 @@ public static List toHLSCmdArrays(String input, String m3u8Output, int c return commands; } - return Collections.EMPTY_LIST; + return Collections.emptyList(); } /** * 转码到mp4的命令参数 + * * @param input * @param output * @param vi @@ -196,33 +205,28 @@ public static List toMP4CmdArrays(String input, String output, VideoInfo CV, FORMAT_LIB264, CA, FORMAT_ACC, STRICT, "-2", - THREADS, DEFAULT_IO_THREADS.toString(), + THREADS, defaultIoThreads.toString(), output )); return commands; } - return Collections.EMPTY_LIST; + return Collections.emptyList(); } /** * 获取视频转向的参数 + * * @param vi * @return */ - public static List getRoateCmdArrays(VideoInfo vi){ - if (vi != null && vi.getSize() > 0) { - if (vi.getRotate() > 0) { - //-vf "transpose=1" -c:a copy - return Lists.newArrayList( - VF,"transpose=1" + public static List getRoateCmdArrays(VideoInfo vi) { + if (vi != null && vi.getSize() > 0 && vi.getRotate() > 0) { + //-vf "transpose=1" -c:a copy + return Lists.newArrayList( + VF, "transpose=1" // CA,COPY - ); -// commands.add(VF); -// commands.add("transpose=1"); -// commands.add(CA); -// commands.add(COPY); - } + ); } - return Collections.EMPTY_LIST; + return Collections.emptyList(); } } diff --git a/src/main/java/com/github/tonydeng/fmj/utils/EncriptUtils.java b/src/main/java/com/github/tonydeng/fmj/utils/EncriptUtils.java index dcaf9f1..4f3850f 100644 --- a/src/main/java/com/github/tonydeng/fmj/utils/EncriptUtils.java +++ b/src/main/java/com/github/tonydeng/fmj/utils/EncriptUtils.java @@ -1,13 +1,20 @@ package com.github.tonydeng.fmj.utils; +import lombok.extern.slf4j.Slf4j; + import java.security.MessageDigest; /** * Created by tonydeng on 15/4/17. */ +@Slf4j public class EncriptUtils { //十六进制下数字到字符的映射数组 - private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; + private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; + + private EncriptUtils() { + + } /** * 把inputString加密 @@ -27,10 +34,9 @@ private static String encodeByMD5(String originString) { //使用指定的字节数组对摘要进行最后更新,然后完成摘要计算 byte[] results = md5.digest(originString.getBytes()); //将得到的字节数组变成字符串返回 - String result = byteArrayToHexString(results); - return result; + return byteArrayToHexString(results); } catch (Exception e) { - e.printStackTrace(); + log.error("encode md5 error", e); } } return null; @@ -43,20 +49,25 @@ private static String encodeByMD5(String originString) { * @return 十六进制字符串 */ private static String byteArrayToHexString(byte[] b) { - StringBuffer resultSb = new StringBuffer(); + StringBuilder builder = new StringBuilder(); for (int i = 0; i < b.length; i++) { - resultSb.append(byteToHexString(b[i])); + builder.append(byteToHexString(b[i])); } - return resultSb.toString(); + return builder.toString(); } - //将一个字节转化成十六进制形式的字符串 + /** + * 将一个字节转化成十六进制形式的字符串 + * + * @param b + * @return + */ private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; - return hexDigits[d1] + hexDigits[d2]; + return HEX_DIGITS[d1] + HEX_DIGITS[d2]; } } diff --git a/src/test/java/com/github/tonydeng/fmj/utils/EncriptUtilsTest.java b/src/test/java/com/github/tonydeng/fmj/utils/EncriptUtilsTest.java new file mode 100644 index 0000000..d2b0f75 --- /dev/null +++ b/src/test/java/com/github/tonydeng/fmj/utils/EncriptUtilsTest.java @@ -0,0 +1,22 @@ +package com.github.tonydeng.fmj.utils; + +import com.github.tonydeng.fmj.BaseTest; +import lombok.extern.slf4j.Slf4j; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Tony Deng + * @version V1.0 + * @date 2019-09-10 00:28 + **/ +@Slf4j +public class EncriptUtilsTest extends BaseTest { + + private static final String INPUT = "12345"; + + @Test + public void testMd5() { + Assert.assertEquals("827CCB0EEA8A706C4C34A16891F84E7B",EncriptUtils.md5(INPUT)); + } +}