瀏覽代碼

更新 media 相关工具类

reghao 2 年之前
父節點
當前提交
fc21921fda

+ 1 - 1
media/src/main/java/cn/reghao/jutil/media/po/AudioInfo.java → media/src/main/java/cn/reghao/jutil/media/po/AudioProps.java

@@ -9,7 +9,7 @@ import lombok.Getter;
  */
 @AllArgsConstructor
 @Getter
-public class AudioInfo {
+public class AudioProps {
     private String codecName;
     private String codecTagString;
     private double bitRate;

+ 0 - 23
media/src/main/java/cn/reghao/jutil/media/po/MediaInfo.java

@@ -1,23 +0,0 @@
-package cn.reghao.jutil.media.po;
-
-import lombok.Getter;
-import lombok.Setter;
-
-import java.time.LocalDateTime;
-
-/**
- * @author reghao
- * @date 2023-03-28 10:07:59
- */
-@Getter
-@Setter
-public class MediaInfo {
-    private AudioInfo audioInfo;
-    private VideoInfo videoInfo;
-    private LocalDateTime createTime;
-
-    public MediaInfo(AudioInfo audioInfo, VideoInfo videoInfo) {
-        this.audioInfo = audioInfo;
-        this.videoInfo = videoInfo;
-    }
-}

+ 23 - 0
media/src/main/java/cn/reghao/jutil/media/po/MediaProps.java

@@ -0,0 +1,23 @@
+package cn.reghao.jutil.media.po;
+
+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;
+    private LocalDateTime createTime;
+
+    public MediaProps(AudioProps audioProps, VideoProps videoProps) {
+        this.audioProps = audioProps;
+        this.videoProps = videoProps;
+    }
+}

+ 1 - 1
media/src/main/java/cn/reghao/jutil/media/po/VideoInfo.java → media/src/main/java/cn/reghao/jutil/media/po/VideoProps.java

@@ -9,7 +9,7 @@ import lombok.Getter;
  */
 @AllArgsConstructor
 @Getter
-public class VideoInfo {
+public class VideoProps {
     private String codecName;
     private String codecTagString;
     private double bitRate;

+ 58 - 0
media/src/main/java/cn/reghao/jutil/media/video/FFmpegWrapper.java

@@ -1,8 +1,18 @@
 package cn.reghao.jutil.media.video;
 
+import cn.reghao.jutil.jdk.converter.DateTimeConverter;
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import cn.reghao.jutil.jdk.shell.ShellExecutor;
 import cn.reghao.jutil.jdk.shell.ShellResult;
 import cn.reghao.jutil.media.Shell;
+import cn.reghao.jutil.media.po.AudioProps;
+import cn.reghao.jutil.media.po.MediaProps;
+import cn.reghao.jutil.media.po.VideoProps;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+import java.time.LocalDateTime;
 
 /**
  * @author reghao
@@ -10,8 +20,56 @@ import cn.reghao.jutil.media.Shell;
  */
 public class FFmpegWrapper {
     static ShellExecutor shellExecutor = new ShellExecutor();
+    private final static String ffprobe = "/usr/bin/ffprobe";
     private final static String ffmpeg = "/usr/bin/ffmpeg";
 
+    public static MediaProps getMediaProps(String src) {
+        String cmd = String.format("%s -v quiet -print_format json -show_format -show_streams -i \"%s\"", ffprobe, src);
+        String result = Shell.execWithResult(cmd);
+        if (result != null) {
+            JsonObject jsonObject = JsonConverter.jsonToJsonElement(result).getAsJsonObject();
+            JsonArray streams = jsonObject.get("streams").getAsJsonArray();
+            AudioProps audioProps = null;
+            VideoProps videoProps = null;
+            for (JsonElement jsonElement : streams) {
+                JsonObject jsonObject1 = jsonElement.getAsJsonObject();
+                String codecType = jsonObject1.get("codec_type").getAsString();
+                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();
+                    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 codedWidth = jsonObject1.get("coded_width").getAsDouble();
+                    double codedHeight = jsonObject1.get("coded_height").getAsDouble();
+                    videoProps = new VideoProps(codecName, codecTagString, bitRate, duration, codedWidth, codedHeight);
+                }
+            }
+
+            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();
+
+            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);
+            }
+
+            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);
         return Shell.exec(cmd);

+ 0 - 3
media/src/main/java/cn/reghao/jutil/media/video/VideoOps.java

@@ -22,9 +22,6 @@ import java.util.*;
  * @date 2021-08-04 09:51:30
  */
 public class VideoOps {
-    static String bash = "/bin/bash";
-    static String ffprobe = "/usr/bin/ffprobe";
-    static String ffmpeg = "/usr/bin/ffmpeg";
     static {
         avutil.av_log_set_level(avutil.AV_LOG_QUIET);
     }

+ 0 - 203
media/src/test/java/ImageTest.java

@@ -1,203 +0,0 @@
-import cn.reghao.jutil.jdk.converter.DateTimeConverter;
-import cn.reghao.jutil.jdk.serializer.JsonConverter;
-import cn.reghao.jutil.media.Shell;
-import cn.reghao.jutil.media.image.ImageOps;
-import cn.reghao.jutil.media.po.AudioInfo;
-import cn.reghao.jutil.media.po.MediaInfo;
-import cn.reghao.jutil.media.po.VideoInfo;
-import cn.reghao.jutil.media.video.FFmpegWrapper;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import lombok.extern.slf4j.Slf4j;
-
-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;
-import java.sql.SQLOutput;
-import java.time.LocalDateTime;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author reghao
- * @date 2023-02-21 21:09:37
- */
-public class ImageTest {
-    static String bash = "/bin/bash";
-    static String file = "/bin/file";
-    static String ffprobe = "/usr/bin/ffprobe";
-    static String ffmpeg = "/usr/bin/ffmpeg";
-
-    static String fileInfo(String src) {
-        String cmd = String.format("%s -b \"%s\"", file, src);
-        return Shell.execWithResult(cmd);
-    }
-
-    static String getMediaType(String src) {
-        String cmd = String.format("%s -b --mime-type \"%s\"", file, src);
-        return Shell.execWithResult(cmd);
-    }
-
-    static String probe(String src) {
-        return String.format("%s -v quiet -print_format json -show_format -show_streams -i \"%s\"", ffprobe, src);
-    }
-
-    static String covert(String src, String dest) {
-        return String.format("%s -y -i %s -c:a aac -c:v libx264 %s", ffmpeg, src, dest);
-    }
-
-    static String covert1(String src, int width, int height, String dest) {
-        String audioBitRate = "128k";
-        String videoBitRate = "1500k";
-        return String.format("%s -i %s -s %sx%s -c:a aac -b:a %s -c:v libx264 -b:v %s -g 90 %s",
-                ffmpeg, src, width, height, audioBitRate, videoBitRate, dest);
-    }
-
-    static String split(String src, String start, String duration, String dest) {
-        return String.format("%s -i %s -ss %s -t %s -vcodec copy -acodec copy %s", ffmpeg, src, start, duration, dest);
-    }
-
-    static Map<File, MediaInfo> map = new HashMap<>();
-    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();
-                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();
-                    }
-                }
-
-                /*MediaInfo mediaInfo = getMediaInfo(file1);
-                map.put(file1, mediaInfo);*/
-                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 MediaInfo getMediaInfo(File file) {
-        String cmd = probe(file.getAbsolutePath());
-        String result = Shell.execWithResult(cmd);
-        if (result != null) {
-            JsonObject jsonObject = JsonConverter.jsonToJsonElement(result).getAsJsonObject();
-            JsonArray streams = jsonObject.get("streams").getAsJsonArray();
-            AudioInfo audioInfo = null;
-            VideoInfo videoInfo = null;
-            for (JsonElement jsonElement : streams) {
-                JsonObject jsonObject1 = jsonElement.getAsJsonObject();
-                String codecType = jsonObject1.get("codec_type").getAsString();
-                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();
-                    audioInfo = new AudioInfo(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 codedWidth = jsonObject1.get("coded_width").getAsDouble();
-                    double codedHeight = jsonObject1.get("coded_height").getAsDouble();
-                    videoInfo = new VideoInfo(codecName, codecTagString, bitRate, duration, codedWidth, codedHeight);
-                }
-            }
-
-            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();
-
-            MediaInfo mediaInfo = new MediaInfo(audioInfo, videoInfo);
-            JsonObject tags = format.get("tags").getAsJsonObject();
-            JsonElement jsonElement = tags.get("creation_time");
-            if (jsonElement != null) {
-                String creationTime = jsonElement.getAsString();
-                LocalDateTime localDateTime = DateTimeConverter.localDateTime(creationTime);
-                mediaInfo.setCreateTime(localDateTime);
-            }
-
-            return mediaInfo;
-        }
-        return null;
-    }
-
-    static void test() throws IOException {
-        String readFormats[] = ImageIO.getReaderFormatNames();
-        String writeFormats[] = ImageIO.getWriterFormatNames();
-        //walkDir("/home/reghao/data/video/");
-        walkDir("/home/reghao/data/picture/");
-
-        /*String filePath1 = "/home/reghao/data/picture/2.png";
-        File file1 = new File(filePath1);
-        String format = ImageOps.getFormat(file1);
-        System.out.println();*/
-
-        map.forEach((file, mediaInfo) -> {
-            if (mediaInfo == null) {
-                return;
-            }
-
-            String audioCodec = mediaInfo.getAudioInfo().getCodecName();
-            String videoCodec = mediaInfo.getVideoInfo().getCodecName();
-            if (audioCodec.equals("aac") && videoCodec.equals("h264")) {
-                String result = Shell.execWithResult(fileInfo(file.getAbsolutePath()));
-                System.out.println();
-            } else {
-                String filePath = file.getAbsolutePath();
-                String filename = file.getName();
-                String cmd = covert(filePath, "/home/reghao/Downloads/0/" + filename);
-                Shell.exec(cmd);
-            }
-        });
-    }
-
-    public static void main(String[] args) throws IOException {
-        String src = "/home/reghao/Downloads/video.mp4";
-        String dest = "/home/reghao/Downloads/video_x264.mp4";
-        /*int status = FFmpegWrapper.formatCovert(src, dest);
-        if (status != 0) {
-            System.out.println("视频格式转换错误");
-            return;
-        }*/
-
-        /*int width = 144;
-        int height = 256;
-        String dest1 = String.format("/home/reghao/Downloads/video_x264_%sx%s.mp4", width, height);
-        int status1 = FFmpegWrapper.qualityCovert(dest, width, height, dest1);
-        if (status1 != 0) {
-            System.out.println("视频质量转换错误");
-        }*/
-        String mediaType = getMediaType("/opt/oss/disk/00b989fc-991b-4d4e-959e-9b6e19299b72/2/101//14abaa52e32243a196561c19a0ef70f1.dat");
-        System.out.println(mediaType);
-    }
-}

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

@@ -0,0 +1,84 @@
+import cn.reghao.jutil.media.image.ImageOps;
+import cn.reghao.jutil.media.po.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();
+    }
+}