| | |
| | | } |
| | | |
| | | // 生成视频缩略图 |
| | | public ByteArrayInputStream generateThumbnail1(String videoFile, int skipFrames, int targetWidth) { |
| | | // long start = System.currentTimeMillis(); |
| | | // FFmpegFrameGrabber grabber = null; |
| | | // |
| | | // try { |
| | | // grabber = new FFmpegFrameGrabber(videoFile); |
| | | // grabber.start(); |
| | | // |
| | | // int totalFrames = grabber.getLengthInFrames(); |
| | | // System.out.println("视频总帧数: " + totalFrames); |
| | | // System.out.println("视频时长: " + grabber.getLengthInTime() / 1000000 + "秒"); |
| | | // |
| | | // Frame frame = null; |
| | | // int currentFrame = 0; |
| | | // |
| | | // // 读取指定帧数后的第一帧有效图像 |
| | | // while (currentFrame < totalFrames) { |
| | | // frame = grabber.grabImage(); |
| | | // if (frame == null) { |
| | | // break; |
| | | // } |
| | | // |
| | | // if (currentFrame > skipFrames && frame.image != null) { |
| | | // break; |
| | | // } |
| | | // currentFrame++; |
| | | // } |
| | | // |
| | | // if (frame == null || frame.image == null) { |
| | | // throw new Exception("无法获取有效视频帧"); |
| | | // } |
| | | // |
| | | // // 转换 Frame 为 BufferedImage |
| | | // Java2DFrameConverter converter = new Java2DFrameConverter(); |
| | | // BufferedImage image = converter.getBufferedImage(frame); |
| | | // |
| | | // // 按目标宽度等比缩放 |
| | | // int targetHeight = (int) ((double) targetWidth / image.getWidth() * image.getHeight()); |
| | | // BufferedImage scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_3BYTE_BGR); |
| | | // scaledImage.getGraphics().drawImage(image, 0, 0, targetWidth, targetHeight, null); |
| | | // |
| | | // // 转成 ByteArrayInputStream |
| | | // ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| | | // ImageIO.write(scaledImage, "jpg", baos); |
| | | // long cost = System.currentTimeMillis() - start; |
| | | // System.out.println("处理耗时: " + cost + "ms"); |
| | | // |
| | | // return new ByteArrayInputStream(baos.toByteArray()); |
| | | // |
| | | // } catch (Exception e) { |
| | | // throw new RuntimeException(e); |
| | | // } finally { |
| | | // if (grabber != null) { |
| | | // try { |
| | | // grabber.stop(); |
| | | // grabber.close(); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // } |
| | | return null; |
| | | } |
| | | |
| | | public ByteArrayInputStream generateThumbnail( |
| | | String videoFile, |
| | | int skipSeconds, |
| | |
| | | try { |
| | | List<String> command = new ArrayList<>(); |
| | | command.add(ffmpegPath); |
| | | |
| | | command.add("-timeout"); |
| | | command.add("3000000"); |
| | | command.add("-rw_timeout"); |
| | | command.add("3000000"); |
| | | |
| | | command.add("-ss"); |
| | | command.add(String.format("00:00:%02d", skipSeconds)); |
| | | command.add("-i"); |