MediaTest.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import cn.reghao.jutil.media.image.ImageOps;
  2. import cn.reghao.jutil.media.po.MediaProps;
  3. import cn.reghao.jutil.media.video.FFmpegWrapper;
  4. import javax.imageio.ImageIO;
  5. import java.io.*;
  6. import java.nio.file.FileVisitResult;
  7. import java.nio.file.FileVisitor;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.attribute.BasicFileAttributes;
  11. /**
  12. * @author reghao
  13. * @date 2023-02-21 21:09:37
  14. */
  15. public class MediaTest {
  16. static void walkDir(String dirPath) throws IOException {
  17. Path path = Path.of(dirPath);
  18. Files.walkFileTree(path, new FileVisitor<>() {
  19. @Override
  20. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  21. return FileVisitResult.CONTINUE;
  22. }
  23. @Override
  24. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  25. File file1 = file.toFile();
  26. return FileVisitResult.CONTINUE;
  27. }
  28. @Override
  29. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  30. return FileVisitResult.CONTINUE;
  31. }
  32. @Override
  33. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  34. return FileVisitResult.CONTINUE;
  35. }
  36. });
  37. }
  38. static void imageTest() throws IOException {
  39. String readFormats[] = ImageIO.getReaderFormatNames();
  40. String writeFormats[] = ImageIO.getWriterFormatNames();
  41. walkDir("/home/reghao/data/picture/");
  42. String filePath1 = "/home/reghao/data/picture/2.png";
  43. File file1 = new File(filePath1);
  44. String format = ImageOps.getFormat(file1);
  45. if (format == null) {
  46. System.out.println("文件格式未知");
  47. } else if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
  48. } else {
  49. if (format.equalsIgnoreCase("png")) {
  50. System.out.printf("%s covert to jpg\n", format);
  51. byte[] bytes = ImageOps.png2jpg(file1);
  52. System.out.println();
  53. }
  54. }
  55. }
  56. static void videoTest() throws IOException {
  57. String src = "/home/reghao/Downloads/video.mp4";
  58. MediaProps mediaProps = FFmpegWrapper.getMediaProps(src);
  59. if (mediaProps == null) {
  60. return;
  61. }
  62. String audioCodec = mediaProps.getAudioProps().getCodecName();
  63. String videoCodec = mediaProps.getVideoProps().getCodecName();
  64. if (audioCodec.equals("aac") && videoCodec.equals("h264")) {
  65. } else {
  66. }
  67. }
  68. public static void main(String[] args) throws IOException {
  69. videoTest();
  70. }
  71. }