/**
* 获取视频时长:秒 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;
}