From ba8455ff0dc0fcdb0a24e67b20f025853cfa5c64 Mon Sep 17 00:00:00 2001 From: itning Date: Mon, 12 Feb 2024 16:30:38 +0800 Subject: [PATCH] feat: optimization --- .../yunshunas/video/video/FFmpegUtils.java | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/nas-video/src/main/java/top/itning/yunshunas/video/video/FFmpegUtils.java b/nas-video/src/main/java/top/itning/yunshunas/video/video/FFmpegUtils.java index 0f08c010..a389dabc 100644 --- a/nas-video/src/main/java/top/itning/yunshunas/video/video/FFmpegUtils.java +++ b/nas-video/src/main/java/top/itning/yunshunas/video/video/FFmpegUtils.java @@ -96,7 +96,7 @@ public static long getVideoBitRate(String ffprobePath, String file) throws IOExc } /** - * 将视频文件转换成M3U8(HLS) + * 转换视频文件 * * @param ffmpegPath ffmpeg文件路径 * @param file 视频文件 @@ -105,11 +105,11 @@ public static long getVideoBitRate(String ffprobePath, String file) throws IOExc * @param commandInfo 输出信息 * @throws IOException 转换失败 */ - public static void convertedToM3u8(String ffmpegPath, - String file, - String outFileName, - ConvertedM3u8Params params, - Consumer commandInfo) throws IOException { + public static void converterVideo(String ffmpegPath, + String file, + String outFileName, + ConverterParams params, + Consumer commandInfo) throws IOException { List command = new ArrayList<>(); command.add(ffmpegPath); command.add("-y"); @@ -145,12 +145,16 @@ public static void convertedToM3u8(String ffmpegPath, command.add(audioBitRate); }); } - command.add("-hls_time"); - command.add(Optional.ofNullable(params.getHlsTime()).orElse(10).toString()); - command.add("-hls_list_size"); - command.add(Optional.ofNullable(params.getHlsListSize()).orElse(0).toString()); - command.add("-start_number"); - command.add(Optional.ofNullable(params.getStartNumber()).orElse(0).toString()); + if (Optional.ofNullable(params.getCover2hls()).orElse(true)) { + command.add("-hls_time"); + command.add(Optional.ofNullable(params.getHlsTime()).orElse(10).toString()); + command.add("-hls_list_size"); + command.add(Optional.ofNullable(params.getHlsListSize()).orElse(0).toString()); + command.add("-start_number"); + command.add(Optional.ofNullable(params.getStartNumber()).orElse(0).toString()); + command.add("-f"); + command.add("hls"); + } command.add(outFileName); command.add("-progress"); command.add("-"); @@ -173,7 +177,8 @@ public static void convertedToM3u8(String ffmpegPath, @Data @Builder - public static class ConvertedM3u8Params { + public static class ConverterParams { + private Boolean cover2hls; private String videoCodec; private String audioCodec; private Integer startNumber; @@ -185,8 +190,21 @@ public static class ConvertedM3u8Params { private String maxVideoBitRate; private String videoBufSize; - public static ConvertedM3u8Params defaultParams() { - return ConvertedM3u8Params.builder() + public static ConverterParams toHls() { + return ConverterParams.builder() + .cover2hls(true) + .videoCodec("libx264") + .audioCodec("aac") + .videoBitRate("5000k") + .minVideoBitRate("2000k") + .maxVideoBitRate("10000k") + .videoBufSize("5000k") + .build(); + } + + public static ConverterParams defaultParams() { + return ConverterParams.builder() + .cover2hls(false) .videoCodec("libx264") .audioCodec("aac") .videoBitRate("5000k")