reghao 2 лет назад
Родитель
Сommit
ce802b470e
1 измененных файлов с 36 добавлено и 17 удалено
  1. 36 17
      media/src/main/java/cn/reghao/jutil/media/FFmpegWrapper.java

+ 36 - 17
media/src/main/java/cn/reghao/jutil/media/FFmpegWrapper.java

@@ -3,8 +3,6 @@ package cn.reghao.jutil.media;
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import cn.reghao.jutil.jdk.shell.Shell;
-import cn.reghao.jutil.jdk.shell.ShellExecutor;
-import cn.reghao.jutil.jdk.shell.ShellResult;
 import cn.reghao.jutil.media.model.AudioProps;
 import cn.reghao.jutil.media.model.MediaProps;
 import cn.reghao.jutil.media.model.VideoProps;
@@ -22,8 +20,8 @@ public class FFmpegWrapper {
     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);
+    public static MediaProps getMediaProps(String srcPath) {
+        String cmd = String.format("%s -v quiet -print_format json -show_format -show_streams -i \"%s\"", ffprobe, srcPath);
         String result = Shell.execWithResult(cmd);
         if (result != null) {
             JsonObject jsonObject = JsonConverter.jsonToJsonElement(result).getAsJsonObject();
@@ -111,30 +109,51 @@ public class FFmpegWrapper {
         return null;
     }
 
-    public static int formatCovert(String src, String dest) {
-        String cmd = String.format("%s -y -i %s -c:a aac -c:v libx264 -f mp4 %s", ffmpeg, src, dest);
+    public static int formatCovert(String srcPath, String destPath) {
+        String cmd = String.format("%s -loglevel error -y -i %s -c:a aac -c:v libx264 -f mp4 %s",
+                ffmpeg, srcPath, destPath);
         return Shell.exec(cmd);
     }
 
-    public static int qualityCovert(String src, int width, int height, String dest) {
+    public static int qualityCovert(String srcPath, int width, int height, String destPath) {
         String audioBitRate = "128k";
         String videoBitRate = "1500k";
-        String cmd = 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);
+        String cmd = String.format("%s -loglevel error -i %s -s %sx%s -c:a aac -b:a %s -c:v libx264 -b:v %s -g 90 %s",
+                ffmpeg, srcPath, width, height, audioBitRate, videoBitRate, destPath);
         return Shell.exec(cmd);
     }
 
-    public static void mp4ToM3u8() {
+    public static int split(String srcPath, String audioPath, String videoPath) {
+        String cmd = String.format("%s -loglevel error -y -i %s -acodec copy -vn %s", ffmpeg, srcPath, audioPath);
+        int ret = Shell.exec(cmd);
+        if (ret != 0) {
+            return ret;
+        }
+
+        String cmd1 = String.format("%s -loglevel error -y -i %s -vcodec copy –an %s", ffmpeg, srcPath, videoPath);
+        int ret1 = Shell.exec(cmd1);
+        if (ret1 != 0) {
+            return ret1;
+        }
 
+        return -1;
     }
 
-    public static void m3u8ToMp4(String dir, String videoId, String videoFilePath, String audioFilePath) throws Exception {
-        String mp4FilePath = String.format("%s/%s.mp4", dir, videoId);
+    public static int merge(String audioPath, String videoPath, String destPath) {
+        String cmd = String.format("%s -loglevel error -y -i %s -i %s -codec copy %s",
+                ffmpeg, audioPath, videoPath, destPath);
+        return Shell.exec(cmd);
+    }
 
-        StringBuilder sb = new StringBuilder();
-        sb.append("ffmpeg -i ").append(audioFilePath).append(" ")
-                .append("-i ").append(videoFilePath).append(" ")
-                .append("-codec copy ").append(mp4FilePath);
-        Shell.exec(sb.toString());
+    public static int covertToM3u8(String srcPath, String m3u8Path) {
+        String cmd = String.format("%s -loglevel error -i %s -c:v libx264 -c:a aac -strict -2 " +
+                "-f hls -hls_list_size 0 -hls_time 60 %s", ffmpeg, srcPath, m3u8Path);
+        return Shell.exec(cmd);
+    }
+
+    public static int m3u8ToMp4(String m3u8Dir, String destPath) {
+        String cmd = String.format("%s -allowed_extensions ALL -protocol_whitelist \"file,http,crypto,tcp\" " +
+                "-i %s -c:a aac -c:v libx264 -f mp4 %s", ffmpeg, m3u8Dir, destPath);
+        return Shell.exec(cmd);
     }
 }