Просмотр исходного кода

shell 命令行中的变量包含在 '' 单引号中, 防止转义

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

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

@@ -21,7 +21,7 @@ public class FFmpegWrapper {
     private final static String ffmpeg = "/usr/bin/ffmpeg";
     private final static String ffmpeg = "/usr/bin/ffmpeg";
 
 
     public static MediaProps getMediaProps(String srcPath) {
     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 cmd = String.format("%s -v quiet -print_format json -show_format -show_streams -i '%s'", ffprobe, srcPath);
         String result = Shell.execWithResult(cmd);
         String result = Shell.execWithResult(cmd);
         if (result != null) {
         if (result != null) {
             JsonObject jsonObject = JsonConverter.jsonToJsonElement(result).getAsJsonObject();
             JsonObject jsonObject = JsonConverter.jsonToJsonElement(result).getAsJsonObject();
@@ -110,7 +110,7 @@ public class FFmpegWrapper {
     }
     }
 
 
     public static int formatCovert(String srcPath, String destPath) {
     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",
+        String cmd = String.format("%s -loglevel error -y -i '%s' -c:a aac -c:v libx264 -f mp4 '%s'",
                 ffmpeg, srcPath, destPath);
                 ffmpeg, srcPath, destPath);
         return Shell.exec(cmd);
         return Shell.exec(cmd);
     }
     }
@@ -118,19 +118,19 @@ public class FFmpegWrapper {
     public static int qualityCovert(String srcPath, int width, int height, String destPath) {
     public static int qualityCovert(String srcPath, int width, int height, String destPath) {
         String audioBitRate = "128k";
         String audioBitRate = "128k";
         String videoBitRate = "1500k";
         String videoBitRate = "1500k";
-        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",
+        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);
                 ffmpeg, srcPath, width, height, audioBitRate, videoBitRate, destPath);
         return Shell.exec(cmd);
         return Shell.exec(cmd);
     }
     }
 
 
     public static int split(String srcPath, String audioPath, String videoPath) {
     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);
+        String cmd = String.format("%s -loglevel error -y -i '%s' -acodec copy -vn '%s'", ffmpeg, srcPath, audioPath);
         int ret = Shell.exec(cmd);
         int ret = Shell.exec(cmd);
         if (ret != 0) {
         if (ret != 0) {
             return ret;
             return ret;
         }
         }
 
 
-        String cmd1 = String.format("%s -loglevel error -y -i %s -vcodec copy –an %s", ffmpeg, srcPath, videoPath);
+        String cmd1 = String.format("%s -loglevel error -y -i '%s' -vcodec copy –an '%s'", ffmpeg, srcPath, videoPath);
         int ret1 = Shell.exec(cmd1);
         int ret1 = Shell.exec(cmd1);
         if (ret1 != 0) {
         if (ret1 != 0) {
             return ret1;
             return ret1;
@@ -140,20 +140,20 @@ public class FFmpegWrapper {
     }
     }
 
 
     public static int merge(String audioPath, String videoPath, String destPath) {
     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",
+        String cmd = String.format("%s -loglevel error -y -i '%s' -i %s -codec copy '%s'",
                 ffmpeg, audioPath, videoPath, destPath);
                 ffmpeg, audioPath, videoPath, destPath);
         return Shell.exec(cmd);
         return Shell.exec(cmd);
     }
     }
 
 
     public static int covertToM3u8(String srcPath, String m3u8Path) {
     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);
+        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);
         return Shell.exec(cmd);
     }
     }
 
 
     public static int m3u8ToMp4(String m3u8Dir, String destPath) {
     public static int m3u8ToMp4(String m3u8Dir, String destPath) {
         String cmd = String.format("%s -allowed_extensions ALL -protocol_whitelist \"file,http,crypto,tcp\" " +
         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);
+                "-i '%s' -c:a aac -c:v libx264 -f mp4 '%s'", ffmpeg, m3u8Dir, destPath);
         return Shell.exec(cmd);
         return Shell.exec(cmd);
     }
     }
 }
 }