ImageTest.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import cn.reghao.jutil.jdk.converter.DateTimeConverter;
  2. import cn.reghao.jutil.jdk.serializer.JsonConverter;
  3. import cn.reghao.jutil.media.image.ImageOps;
  4. import cn.reghao.jutil.media.po.AudioInfo;
  5. import cn.reghao.jutil.media.po.MediaInfo;
  6. import cn.reghao.jutil.media.po.VideoInfo;
  7. import com.google.gson.JsonArray;
  8. import com.google.gson.JsonElement;
  9. import com.google.gson.JsonObject;
  10. import javax.imageio.ImageIO;
  11. import java.io.*;
  12. import java.nio.file.FileVisitResult;
  13. import java.nio.file.FileVisitor;
  14. import java.nio.file.Files;
  15. import java.nio.file.Path;
  16. import java.nio.file.attribute.BasicFileAttributes;
  17. import java.time.LocalDateTime;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * @author reghao
  22. * @date 2023-02-21 21:09:37
  23. */
  24. public class ImageTest {
  25. static String bash = "/bin/bash";
  26. static String file = "/bin/file";
  27. static String ffprobe = "/usr/bin/ffprobe";
  28. static String ffmpeg = "/usr/bin/ffmpeg";
  29. static String fileInfo(String src) {
  30. return String.format("%s -b \"%s\"", file, src);
  31. //return String.format("%s -b -i \"%s\"", file, src);
  32. }
  33. static String probe(String src) {
  34. return String.format("%s -v quiet -print_format json -show_format -show_streams -i \"%s\"", ffprobe, src);
  35. }
  36. static String covert(String src, String dest) {
  37. return String.format("%s -y -i %s -c:a aac -c:v libx264 %s", ffmpeg, src, dest);
  38. }
  39. static String covert1(String src, int width, int height, String dest) {
  40. String audioBitRate = "128k";
  41. String videoBitRate = "1500k";
  42. return String.format("%s -i %s -s %sx%s -c:a aac -b:a %s -c:v libx264 -b:v %s -g 90 %s",
  43. ffmpeg, src, width, height, audioBitRate, videoBitRate, dest);
  44. }
  45. static String split(String src, String start, String duration, String dest) {
  46. return String.format("%s -i %s -ss %s -t %s -vcodec copy -acodec copy %s", ffmpeg, src, start, duration, dest);
  47. }
  48. static void exec(String cmd) {
  49. Runtime runtime = Runtime.getRuntime();
  50. try {
  51. Process proc = runtime.exec(new String[]{bash, "-c", cmd});
  52. new Thread(new Output(proc)).start();
  53. int status = proc.waitFor();
  54. if (status != 0) {
  55. System.out.println("失败");
  56. }
  57. proc.getOutputStream().close();
  58. proc.getInputStream().close();
  59. proc.getErrorStream().close();
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. } finally {
  63. runtime.freeMemory();
  64. }
  65. }
  66. static String execWithResult(String cmd) {
  67. Runtime runtime = Runtime.getRuntime();
  68. try {
  69. Process proc = runtime.exec(new String[]{bash, "-c", cmd});
  70. int status = proc.waitFor();
  71. if (status != 0) {
  72. return null;
  73. }
  74. BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  75. StringBuilder sb = new StringBuilder();
  76. try {
  77. String line;
  78. while((line = br.readLine()) != null){
  79. sb.append(line);
  80. }
  81. br.close();
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. proc.getOutputStream().close();
  86. proc.getInputStream().close();
  87. proc.getErrorStream().close();
  88. return sb.toString();
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. } finally {
  92. runtime.freeMemory();
  93. }
  94. return null;
  95. }
  96. static Map<File, MediaInfo> map = new HashMap<>();
  97. static void walkDir(String dirPath) throws IOException {
  98. Path path = Path.of(dirPath);
  99. Files.walkFileTree(path, new FileVisitor<>() {
  100. @Override
  101. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  102. return FileVisitResult.CONTINUE;
  103. }
  104. @Override
  105. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  106. File file1 = file.toFile();
  107. String format = ImageOps.getFormat(file1);
  108. if (format == null) {
  109. System.out.println("文件格式未知");
  110. } else if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
  111. } else {
  112. if (format.equalsIgnoreCase("png")) {
  113. System.out.printf("%s covert to jpg\n", format);
  114. byte[] bytes = ImageOps.png2jpg(file1);
  115. System.out.println();
  116. }
  117. }
  118. /*MediaInfo mediaInfo = getMediaInfo(file1);
  119. map.put(file1, mediaInfo);*/
  120. return FileVisitResult.CONTINUE;
  121. }
  122. @Override
  123. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  124. return FileVisitResult.CONTINUE;
  125. }
  126. @Override
  127. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  128. return FileVisitResult.CONTINUE;
  129. }
  130. });
  131. }
  132. static MediaInfo getMediaInfo(File file) {
  133. String cmd = probe(file.getAbsolutePath());
  134. String result = execWithResult(cmd);
  135. if (result != null) {
  136. JsonObject jsonObject = JsonConverter.jsonToJsonElement(result).getAsJsonObject();
  137. JsonArray streams = jsonObject.get("streams").getAsJsonArray();
  138. AudioInfo audioInfo = null;
  139. VideoInfo videoInfo = null;
  140. for (JsonElement jsonElement : streams) {
  141. JsonObject jsonObject1 = jsonElement.getAsJsonObject();
  142. String codecType = jsonObject1.get("codec_type").getAsString();
  143. if (codecType.equals("audio")) {
  144. String codecName = jsonObject1.get("codec_name").getAsString();
  145. String codecTagString = jsonObject1.get("codec_tag_string").getAsString();
  146. double bitRate = jsonObject1.get("bit_rate").getAsDouble();
  147. double duration = jsonObject1.get("duration").getAsDouble();
  148. audioInfo = new AudioInfo(codecName, codecTagString, bitRate, duration);
  149. } else if (codecType.equals("video")) {
  150. String codecName = jsonObject1.get("codec_name").getAsString();
  151. String codecTagString = jsonObject1.get("codec_tag_string").getAsString();
  152. double bitRate = jsonObject1.get("bit_rate").getAsDouble();
  153. double duration = jsonObject1.get("duration").getAsDouble();
  154. double codedWidth = jsonObject1.get("coded_width").getAsDouble();
  155. double codedHeight = jsonObject1.get("coded_height").getAsDouble();
  156. videoInfo = new VideoInfo(codecName, codecTagString, bitRate, duration, codedWidth, codedHeight);
  157. }
  158. }
  159. JsonObject format = jsonObject.get("format").getAsJsonObject();
  160. double duration = format.get("duration").getAsDouble();
  161. double size = format.get("size").getAsDouble();
  162. double bitRate = format.get("bit_rate").getAsDouble();
  163. MediaInfo mediaInfo = new MediaInfo(audioInfo, videoInfo);
  164. JsonObject tags = format.get("tags").getAsJsonObject();
  165. JsonElement jsonElement = tags.get("creation_time");
  166. if (jsonElement != null) {
  167. String creationTime = jsonElement.getAsString();
  168. LocalDateTime localDateTime = DateTimeConverter.localDateTime(creationTime);
  169. mediaInfo.setCreateTime(localDateTime);
  170. }
  171. return mediaInfo;
  172. }
  173. return null;
  174. }
  175. static void test() throws IOException {
  176. String readFormats[] = ImageIO.getReaderFormatNames();
  177. String writeFormats[] = ImageIO.getWriterFormatNames();
  178. //walkDir("/home/reghao/data/video/");
  179. walkDir("/home/reghao/data/picture/");
  180. /*String filePath1 = "/home/reghao/data/picture/2.png";
  181. File file1 = new File(filePath1);
  182. String format = ImageOps.getFormat(file1);
  183. System.out.println();*/
  184. map.forEach((file, mediaInfo) -> {
  185. if (mediaInfo == null) {
  186. return;
  187. }
  188. String audioCodec = mediaInfo.getAudioInfo().getCodecName();
  189. String videoCodec = mediaInfo.getVideoInfo().getCodecName();
  190. if (audioCodec.equals("aac") && videoCodec.equals("h264")) {
  191. String result = execWithResult(fileInfo(file.getAbsolutePath()));
  192. System.out.println();
  193. } else {
  194. String filePath = file.getAbsolutePath();
  195. String filename = file.getName();
  196. String cmd = covert(filePath, "/home/reghao/Downloads/0/" + filename);
  197. exec(cmd);
  198. }
  199. });
  200. }
  201. public static void main(String[] args) throws IOException {
  202. String src = "/home/reghao/Downloads/sxd.mp4";
  203. String start = "00:00:00";
  204. String duration = "00:02:00";
  205. String dest = "/home/reghao/Downloads/sxd-1.mp4";
  206. String cmd = split(src, start, duration, dest);
  207. exec(cmd);
  208. }
  209. static class Output implements Runnable {
  210. private final Process proc;
  211. public Output(Process proc) {
  212. this.proc = proc;
  213. }
  214. @Override
  215. public void run() {
  216. BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
  217. try {
  218. String line;
  219. while((line = br.readLine()) != null){
  220. System.out.println(line);
  221. }
  222. br.close();
  223. } catch (Exception e) {
  224. e.printStackTrace();
  225. }
  226. }
  227. }
  228. }