FileTest.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import cn.reghao.dfs.store.util.FileType;
  2. import cn.reghao.jutil.media.FFmpegWrapper;
  3. import cn.reghao.jutil.media.MediaQuality;
  4. import cn.reghao.jutil.media.MediaResolution;
  5. import cn.reghao.jutil.media.model.AudioProps;
  6. import cn.reghao.jutil.media.model.MediaProps;
  7. import cn.reghao.jutil.media.model.VideoProps;
  8. import cn.reghao.jutil.jdk.security.DigestUtil;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.junit.Test;
  11. import java.io.IOException;
  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.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * @author reghao
  23. * @date 2023-03-22 16:19:12
  24. */
  25. @Slf4j
  26. public class FileTest {
  27. public void walkDir(Path path) throws IOException {
  28. Files.walkFileTree(path, new FileVisitor<>() {
  29. @Override
  30. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  31. return FileVisitResult.CONTINUE;
  32. }
  33. @Override
  34. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  35. String absolutePath = file.toString();
  36. //process(absolutePath);
  37. unique(absolutePath);
  38. return FileVisitResult.CONTINUE;
  39. }
  40. @Override
  41. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  42. return FileVisitResult.CONTINUE;
  43. }
  44. @Override
  45. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  46. return FileVisitResult.CONTINUE;
  47. }
  48. });
  49. }
  50. private void process(String absolutePath) {
  51. MediaProps mediaProps = FFmpegWrapper.getMediaProps(absolutePath);
  52. if (mediaProps == null) {
  53. log.error("{} 没有媒体信息", absolutePath);
  54. return;
  55. }
  56. AudioProps audioProps = mediaProps.getAudioProps();
  57. if (audioProps == null) {
  58. log.error("{} 没有音频信息", absolutePath);
  59. return;
  60. }
  61. VideoProps videoProps = mediaProps.getVideoProps();
  62. if (videoProps == null) {
  63. log.error("{} 没有视频信息", absolutePath);
  64. return;
  65. }
  66. String audioCodec = audioProps.getCodecName();
  67. String videoCodec = videoProps.getCodecName();
  68. int width = videoProps.getCodedWidth().intValue();
  69. int height = videoProps.getCodedHeight().intValue();
  70. if ("aac".equals(audioCodec) && "h264".equals(videoCodec)) {
  71. MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
  72. } else {
  73. log.error("{} 不是 aac&h264", absolutePath);
  74. }
  75. }
  76. Map<String, String> map = new HashMap<>();
  77. List<String> list = new ArrayList<>();
  78. private void unique(String absolutePath) {
  79. try {
  80. String sha256sum = DigestUtil.sha256sum(absolutePath);
  81. String filePath = map.get(sha256sum);
  82. if (filePath == null) {
  83. map.put(sha256sum, filePath);
  84. } else {
  85. log.info("{} 已存在", filePath);
  86. list.add(filePath);
  87. }
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. @Test
  93. public void test22() throws IOException {
  94. //String baseDir = "/home/reghao/mnt/porn/1.待发布/13996.反差婊系列/p/";
  95. //Path path = Path.of(baseDir);
  96. //walkDir(path);
  97. String filePath = "/home/reghao/Downloads/仁和区 2.m4a";
  98. filePath = "/home/reghao/Downloads/三生三世.wav";
  99. String mediaType = FileType.getMediaType(filePath);
  100. MediaProps mediaProps = FFmpegWrapper.getMediaProps(filePath);
  101. //FFmpegWrapper.convertAudio(filePath, "/home/reghao/Downloads/三生三世.m4a");
  102. System.out.println();
  103. }
  104. }