FileTest.java 3.8 KB

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