فهرست منبع

update media module

reghao 2 سال پیش
والد
کامیت
247c2698ad

+ 0 - 7
media/pom.xml

@@ -23,13 +23,6 @@
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
 
-        <dependency>
-            <groupId>org.projectlombok</groupId>
-            <artifactId>lombok</artifactId>
-            <version>1.18.6</version>
-            <optional>true</optional>
-        </dependency>
-
         <dependency>
             <groupId>org.sejda.imageio</groupId>
             <artifactId>webp-imageio</artifactId>

+ 64 - 33
media/src/main/java/cn/reghao/jutil/media/video/FFmpegWrapper.java → media/src/main/java/cn/reghao/jutil/media/FFmpegWrapper.java

@@ -1,4 +1,4 @@
-package cn.reghao.jutil.media.video;
+package cn.reghao.jutil.media;
 
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
@@ -19,7 +19,6 @@ import java.time.LocalDateTime;
  * @date 2022-03-04 11:04:32
  */
 public class FFmpegWrapper {
-    static ShellExecutor shellExecutor = new ShellExecutor();
     private final static String ffprobe = "/usr/bin/ffprobe";
     private final static String ffmpeg = "/usr/bin/ffmpeg";
 
@@ -37,41 +36,83 @@ public class FFmpegWrapper {
                 if (codecType.equals("audio")) {
                     String codecName = jsonObject1.get("codec_name").getAsString();
                     String codecTagString = jsonObject1.get("codec_tag_string").getAsString();
-                    double bitRate = jsonObject1.get("bit_rate").getAsDouble();
-                    double duration = jsonObject1.get("duration").getAsDouble();
+
+                    JsonElement bitRateElement = jsonObject1.get("bit_rate");
+                    double bitRate;
+                    if (bitRateElement == null) {
+                        bitRate = 0.0;
+                    } else {
+                        bitRate = bitRateElement.getAsDouble();
+                    }
+
+                    JsonElement durationElement = jsonObject1.get("duration");
+                    double duration;
+                    if (durationElement == null) {
+                        duration = 0;
+                    } else {
+                        duration = durationElement.getAsDouble();
+                    }
                     audioProps = new AudioProps(codecName, codecTagString, bitRate, duration);
                 } else if (codecType.equals("video")) {
                     String codecName = jsonObject1.get("codec_name").getAsString();
                     String codecTagString = jsonObject1.get("codec_tag_string").getAsString();
-                    double bitRate = jsonObject1.get("bit_rate").getAsDouble();
-                    double duration = jsonObject1.get("duration").getAsDouble();
+
+                    double bitRate;
+                    JsonElement biteRateElement = jsonObject1.get("bit_rate");
+                    if (biteRateElement == null) {
+                        bitRate = 0;
+                    } else {
+                        bitRate = biteRateElement.getAsDouble();
+                    }
+
+                    double duration;
+                    JsonElement durationElement = jsonObject1.get("duration");
+                    if (durationElement == null) {
+                        duration = 0;
+                    } else {
+                        duration = durationElement.getAsDouble();
+                    }
+
                     double codedWidth = jsonObject1.get("coded_width").getAsDouble();
                     double codedHeight = jsonObject1.get("coded_height").getAsDouble();
                     videoProps = new VideoProps(codecName, codecTagString, bitRate, duration, codedWidth, codedHeight);
                 }
             }
 
+            if (videoProps == null) {
+                return null;
+            }
+
             JsonObject format = jsonObject.get("format").getAsJsonObject();
-            double duration = format.get("duration").getAsDouble();
-            double size = format.get("size").getAsDouble();
-            double bitRate = format.get("bit_rate").getAsDouble();
+            if (format.get("duration") != null) {
+                Double duration = format.get("duration").getAsDouble();
+                videoProps.setDuration(duration);
+            }
 
-            MediaProps mediaProps = new MediaProps(audioProps, videoProps);
-            JsonObject tags = format.get("tags").getAsJsonObject();
-            JsonElement jsonElement = tags.get("creation_time");
-            if (jsonElement != null) {
-                String creationTime = jsonElement.getAsString();
-                LocalDateTime localDateTime = DateTimeConverter.localDateTime(creationTime);
-                mediaProps.setCreateTime(localDateTime);
+            Long size = format.get("size").getAsLong();
+            if (format.get("bit_rate") != null) {
+                Double bitRate = format.get("bit_rate").getAsDouble();
+                videoProps.setBitRate(bitRate);
             }
 
+            MediaProps mediaProps = new MediaProps(audioProps, videoProps);
+            JsonElement tagsElement = format.get("tags");
+            if (tagsElement != null) {
+                JsonObject tags = tagsElement.getAsJsonObject();
+                JsonElement jsonElement = tags.get("creation_time");
+                if (jsonElement != null) {
+                    String creationTime = jsonElement.getAsString();
+                    LocalDateTime localDateTime = DateTimeConverter.localDateTime(creationTime);
+                    mediaProps.setCreateTime(localDateTime);
+                }
+            }
             return mediaProps;
         }
         return null;
     }
 
     public static int formatCovert(String src, String dest) {
-        String cmd = String.format("%s -y -i %s -c:a aac -c:v libx264 %s", ffmpeg, src, dest);
+        String cmd = String.format("%s -y -i %s -c:a aac -c:v libx264 -f mp4 %s", ffmpeg, src, dest);
         return Shell.exec(cmd);
     }
 
@@ -83,27 +124,17 @@ public class FFmpegWrapper {
         return Shell.exec(cmd);
     }
 
-    public static void mergeToMp4(String dir, String videoId, String videoFilePath, String audioFilePath) throws Exception {
+    public static void mp4ToM3u8() {
+
+    }
+
+    public static void m3u8ToMp4(String dir, String videoId, String videoFilePath, String audioFilePath) throws Exception {
         String mp4FilePath = String.format("%s/%s.mp4", dir, videoId);
 
         StringBuilder sb = new StringBuilder();
         sb.append("ffmpeg -i ").append(audioFilePath).append(" ")
                 .append("-i ").append(videoFilePath).append(" ")
                 .append("-codec copy ").append(mp4FilePath);
-        ShellResult shellResult = shellExecutor.exec(dir, sb.toString().split("\\s+"));
-        if (!shellResult.isSuccess()) {
-            throw new Exception("合并成 mp4 文件异常: " + shellResult.getResult());
-        }
-    }
-
-    public static void generateDash(String dir, String video, String audio) throws Exception {
-        StringBuilder sb = new StringBuilder();
-        sb.append("MP4Box -dash 5000 -rap -frag-rap -profile dashavc264:onDemand -frag 5000 ")
-                .append(video).append(" ").append(audio).append(" ")
-                .append("-out index.mpd");
-        ShellResult shellResult = shellExecutor.exec(dir, sb.toString().split("\\s+"));
-        if (!shellResult.isSuccess()) {
-            throw new Exception("生成 dash 异常: " + shellResult.getResult());
-        }
+        Shell.exec(sb.toString());
     }
 }

+ 1 - 1
media/src/main/java/cn/reghao/jutil/media/image/ImageOps.java → media/src/main/java/cn/reghao/jutil/media/ImageOps.java

@@ -1,4 +1,4 @@
-package cn.reghao.jutil.media.image;
+package cn.reghao.jutil.media;
 
 import javax.imageio.ImageIO;
 import javax.imageio.ImageReader;

+ 41 - 0
media/src/main/java/cn/reghao/jutil/media/MediaQuality.java

@@ -0,0 +1,41 @@
+package cn.reghao.jutil.media;
+
+/**
+ * 视频质量
+ *
+ * @author reghao
+ * @date 2022-08-05 10:06:08
+ */
+public class MediaQuality {
+    /**
+     * @param
+     * @return
+     * @date 2022-08-18 下午2:25
+     */
+    public static MediaResolution getQuality(int width, int height) {
+        boolean horizontal = width > height;
+        MediaResolution[] arr = MediaResolution.values();
+        MediaResolution resolution = arr[0];
+        int currentAbs;
+        if (horizontal) {
+            currentAbs = Math.abs(height-arr[0].getHeight());
+            for (int i = 1; i < arr.length; i++) {
+                int currentAbs1 = Math.abs(height-arr[i].getHeight());
+                if (currentAbs1 < currentAbs) {
+                    currentAbs = currentAbs1;
+                    resolution = arr[i];
+                }
+            }
+        } else {
+            currentAbs = Math.abs(width-arr[0].getHeight());
+            for (int i = 1; i < arr.length; i++) {
+                int currentAbs1 = Math.abs(width-arr[i].getHeight());
+                if (currentAbs1 < currentAbs) {
+                    currentAbs = currentAbs1;
+                    resolution = arr[i];
+                }
+            }
+        }
+        return resolution;
+    }
+}

+ 55 - 0
media/src/main/java/cn/reghao/jutil/media/MediaResolution.java

@@ -0,0 +1,55 @@
+package cn.reghao.jutil.media;
+
+/**
+ * 视频图像分辨率
+ *
+ * 横屏视频宽高比 = 16:9
+ * 竖屏视频宽高比 = 9:16
+ *
+ * 横屏视频分辨率
+ * 144p = 256x144
+ * 288p = 512x288
+ * 360p = 640x360
+ * 480p = 854x480
+ * 720p = 1280x720
+ * 1080p = 1920x1080
+ * 1440p(2k) = 2560x1440
+ * 2160p(4k) = 3840x2160
+ * 4320p(8k) = 7680×4320
+ *
+ * @author reghao
+ * @date 2022-08-04 09:21:38
+ */
+public enum MediaResolution {
+    p144("144p", 256, 144),
+    p288("288p", 512, 288),
+    p360("360p", 640, 360),
+    p480("480p", 854, 480),
+    p720("720p", 1280, 720),
+    p1080("1080p", 1920, 1080),
+    p1440("2k", 2560, 1440),
+    p2160("4k", 3840, 2160),
+    p4320("8k", 7680, 4320);
+
+    private final String quality;
+    private final int width;
+    private final int height;
+
+    MediaResolution(String quality, int width, int height) {
+        this.quality = quality;
+        this.width = width;
+        this.height = height;
+    }
+
+    public String getQuality() {
+        return quality;
+    }
+
+    public int getWidth() {
+        return width;
+    }
+
+    public int getHeight() {
+        return height;
+    }
+}

+ 2 - 2
media/src/main/java/cn/reghao/jutil/media/video/VideoOps.java → media/src/main/java/cn/reghao/jutil/media/VideoOps.java

@@ -1,6 +1,5 @@
-package cn.reghao.jutil.media.video;
+package cn.reghao.jutil.media;
 
-import cn.reghao.jutil.media.image.ImageOps;
 import org.bytedeco.ffmpeg.avformat.AVFormatContext;
 import org.bytedeco.ffmpeg.avutil.AVDictionary;
 import org.bytedeco.ffmpeg.avutil.AVDictionaryEntry;
@@ -21,6 +20,7 @@ import java.util.*;
  * @author reghao
  * @date 2021-08-04 09:51:30
  */
+@Deprecated
 public class VideoOps {
     static {
         avutil.av_log_set_level(avutil.AV_LOG_QUIET);

+ 39 - 5
media/src/main/java/cn/reghao/jutil/media/model/AudioProps.java

@@ -1,17 +1,51 @@
 package cn.reghao.jutil.media.model;
 
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-
 /**
  * @author reghao
  * @date 2023-03-28 10:07:48
  */
-@AllArgsConstructor
-@Getter
 public class AudioProps {
     private String codecName;
     private String codecTagString;
     private double bitRate;
     private double duration;
+
+    public AudioProps(String codecName, String codecTagString, double bitRate, double duration) {
+        this.codecName = codecName;
+        this.codecTagString = codecTagString;
+        this.bitRate = bitRate;
+        this.duration = duration;
+    }
+
+    public void setCodecName(String codecName) {
+        this.codecName = codecName;
+    }
+
+    public String getCodecName() {
+        return codecName;
+    }
+
+    public void setCodecTagString(String codecTagString) {
+        this.codecTagString = codecTagString;
+    }
+
+    public String getCodecTagString() {
+        return codecTagString;
+    }
+
+    public void setBitRate(double bitRate) {
+        this.bitRate = bitRate;
+    }
+
+    public double getBitRate() {
+        return bitRate;
+    }
+
+    public void setDuration(double duration) {
+        this.duration = duration;
+    }
+
+    public double getDuration() {
+        return duration;
+    }
 }

+ 16 - 5
media/src/main/java/cn/reghao/jutil/media/model/MediaProps.java

@@ -1,16 +1,11 @@
 package cn.reghao.jutil.media.model;
 
-import lombok.Getter;
-import lombok.Setter;
-
 import java.time.LocalDateTime;
 
 /**
  * @author reghao
  * @date 2023-03-28 10:07:59
  */
-@Getter
-@Setter
 public class MediaProps {
     private AudioProps audioProps;
     private VideoProps videoProps;
@@ -20,4 +15,20 @@ public class MediaProps {
         this.audioProps = audioProps;
         this.videoProps = videoProps;
     }
+
+    public AudioProps getAudioProps() {
+        return audioProps;
+    }
+
+    public VideoProps getVideoProps() {
+        return videoProps;
+    }
+
+    public void setCreateTime(LocalDateTime createTime) {
+        this.createTime = createTime;
+    }
+
+    public LocalDateTime getCreateTime() {
+        return createTime;
+    }
 }

+ 45 - 9
media/src/main/java/cn/reghao/jutil/media/model/VideoProps.java

@@ -1,19 +1,55 @@
 package cn.reghao.jutil.media.model;
 
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-
 /**
  * @author reghao
  * @date 2023-03-28 10:07:53
  */
-@AllArgsConstructor
-@Getter
 public class VideoProps {
     private String codecName;
     private String codecTagString;
-    private double bitRate;
-    private double duration;
-    private double codedWidth;
-    private double codedHeight;
+    private Double bitRate;
+    private Double duration;
+    private Double codedWidth;
+    private Double codedHeight;
+
+    public VideoProps(String codecName, String codecTagString, double bitRate, double duration, double codedWidth, double codedHeight) {
+        this.codecName = codecName;
+        this.codecTagString = codecTagString;
+        this.bitRate = bitRate;
+        this.duration = duration;
+        this.codedWidth = codedWidth;
+        this.codedHeight = codedHeight;
+    }
+
+    public String getCodecName() {
+        return codecName;
+    }
+
+    public String getCodecTagString() {
+        return codecTagString;
+    }
+
+    public void setBitRate(Double bitRate) {
+        this.bitRate = bitRate;
+    }
+
+    public Double getBitRate() {
+        return bitRate;
+    }
+
+    public void setDuration(Double duration) {
+        this.duration = duration;
+    }
+
+    public Double getDuration() {
+        return duration;
+    }
+
+    public Double getCodedWidth() {
+        return codedWidth;
+    }
+
+    public Double getCodedHeight() {
+        return codedHeight;
+    }
 }

+ 0 - 84
media/src/test/java/MediaTest.java

@@ -1,84 +0,0 @@
-import cn.reghao.jutil.media.image.ImageOps;
-import cn.reghao.jutil.media.model.MediaProps;
-import cn.reghao.jutil.media.video.FFmpegWrapper;
-
-import javax.imageio.ImageIO;
-import java.io.*;
-import java.nio.file.FileVisitResult;
-import java.nio.file.FileVisitor;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.attribute.BasicFileAttributes;
-
-/**
- * @author reghao
- * @date 2023-02-21 21:09:37
- */
-public class MediaTest {
-    static void walkDir(String dirPath) throws IOException {
-        Path path = Path.of(dirPath);
-        Files.walkFileTree(path, new FileVisitor<>() {
-            @Override
-            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
-                return FileVisitResult.CONTINUE;
-            }
-
-            @Override
-            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
-                File file1 = file.toFile();
-                return FileVisitResult.CONTINUE;
-            }
-
-            @Override
-            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
-                return FileVisitResult.CONTINUE;
-            }
-
-            @Override
-            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
-                return FileVisitResult.CONTINUE;
-            }
-        });
-    }
-
-    static void imageTest() throws IOException {
-        String readFormats[] = ImageIO.getReaderFormatNames();
-        String writeFormats[] = ImageIO.getWriterFormatNames();
-        walkDir("/home/reghao/data/picture/");
-
-        String filePath1 = "/home/reghao/data/picture/2.png";
-        File file1 = new File(filePath1);
-        String format = ImageOps.getFormat(file1);
-        if (format == null) {
-            System.out.println("文件格式未知");
-        } else if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
-
-        } else {
-            if (format.equalsIgnoreCase("png")) {
-                System.out.printf("%s covert to jpg\n", format);
-                byte[] bytes = ImageOps.png2jpg(file1);
-                System.out.println();
-            }
-        }
-    }
-
-    static void videoTest() throws IOException {
-        String src = "/home/reghao/Downloads/video.mp4";
-        MediaProps mediaProps = FFmpegWrapper.getMediaProps(src);
-        if (mediaProps == null) {
-            return;
-        }
-
-        String audioCodec = mediaProps.getAudioProps().getCodecName();
-        String videoCodec = mediaProps.getVideoProps().getCodecName();
-        if (audioCodec.equals("aac") && videoCodec.equals("h264")) {
-
-        } else {
-
-        }
-    }
-
-    public static void main(String[] args) throws IOException {
-        videoTest();
-    }
-}