获取视频第一帧、时长、 分辨率

 /**
     * 获取视频时长:秒 and 分辨率 (duration为视频秒数)
     *
     * @param multipartFile
     * @return
     */
    public static ResolutionDurationVO getWidthOrHeightMultimedia(MultipartFile multipartFile) {
        ResolutionDurationVO res = new ResolutionDurationVO();
        try {
            String fileName = multipartFile.getOriginalFilename();
            String prefix=fileName.substring(fileName.lastIndexOf("."));
            File file = File.createTempFile(prefix, String.valueOf(System.currentTimeMillis()));
            multipartFile.transferTo(file);
            file.deleteOnExit();
            MultimediaObject instance = new MultimediaObject(file);
            MultimediaInfo result = instance.getInfo();
            int width = result.getVideo().getSize().getWidth();
            int height = result.getVideo().getSize().getHeight();
            long duration = result.getDuration() / 1000;
            log.info("duration {}", duration);
            String resolution = width+"*"+height;
            log.info("resolution {}", resolution);
            res.setResolutionWidth(width);
            res.setResolutionHeight(height);
            res.setDuration((int)duration);
            res.setResolution(resolution);
            return res;
        } catch (Exception e) {
            throw new UnprocessableException(AdvertEnum.RESOLUTION_RESOLUTION_ANOMALY);
        }
    }

解析视频第一帧、分辨率及时长

/**
     * 解析视频第一帧、分辨率及时长
     * @param resourceMultipartFile
     * @return
     */
    public static ResolutionDurationVO generateVideoManyScreenImage( MultipartFile resourceMultipartFile, String advertLockKey, RedisTemplate<String,Long> redisTemplate) throws IOException {
        String fileName = resourceMultipartFile.getOriginalFilename();
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        File file = File.createTempFile(String.valueOf(UUID.randomUUID()),prefix);
        resourceMultipartFile.transferTo(file);
        String urlYuan = file.getPath();
        File outputFile = File.createTempFile(String.valueOf(System.currentTimeMillis()),".jpg");
        MultimediaObject multimediaObject = new MultimediaObject(new File(urlYuan));
        ScreenExtractor screenExtractor = new ScreenExtractor();
        int height = 0;
        int width = 0;
        String resolution = null;
        String aspectRatio = null;
        int duration = 0;
        try {
            height = multimediaObject.getInfo().getVideo().getSize().getHeight();
            width = multimediaObject.getInfo().getVideo().getSize().getWidth();
            resolution = width+"*"+height;
            aspectRatio = getAspectRatio(width,height);
            duration = (int)multimediaObject.getInfo().getDuration() / 1000;
            int millis = 1;
            int quality = 1;
            log.info("原视频 = {},生成截图 = {}", urlYuan, outputFile);
            screenExtractor.renderOneImage(multimediaObject, width, height, millis, outputFile, quality);
        } catch (EncoderException e) {
            redisTemplate.delete(advertLockKey);
            log.error("EncoderException : {}", e);
        }

        InputStream inputStream = new FileInputStream(outputFile);
        MultipartFile thFile = new MockMultipartFile(outputFile.getName(), inputStream);
        log.info("thFile转multipartFile成功. {}",thFile);

        boolean delete = file.delete();
        boolean outputFileDelete = outputFile.delete();

        List<MultipartFile> multipartFiles = new ArrayList<>();
        multipartFiles.add(thFile);
        ResolutionDurationVO resolutionDuration = new ResolutionDurationVO();
        resolutionDuration.setResolution(resolution);
        resolutionDuration.setResolutionWidth(width);
        resolutionDuration.setResolutionHeight(height);
        resolutionDuration.setAspectRatio(aspectRatio);
        resolutionDuration.setDuration(duration);
        resolutionDuration.setMultipartFileList(multipartFiles);
        return resolutionDuration;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值