/** * 获取远程图片属性 * <p> * * @param path * @return * @throws Exception * @author * @date 2021/12/23 15:06 * @version 1.0.0 */ public static FilePictureAttributeDTO pictureAttribute(String path) {
FilePictureAttributeDTO res = null; FileInputStream inputStream = null; try {
long l = System.currentTimeMillis(); if (StringUtils.isBlank(path)) {
return res; } File file = new File(path); long l1 = System.currentTimeMillis(); inputStream = new FileInputStream(file); BufferedImage bi = ImageIO.read(inputStream); log.info("pictureAttribute_url-ImageIO.read耗时:{}", System.currentTimeMillis() - l1); res = new FilePictureAttributeDTO().setWidth(bi.getWidth()).setHeight(bi.getHeight()).setSize(file.length());
log.info("pictureAttribute_url-返回:{},总耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
} catch (Exception ex) {
log.error("{} - pictureAttribute_url-获取图片像素异常:{}", path, ex);
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
/** * 获取远程图片属性 * <p> * * @param url * @return * @throws Exception * @author * @date 2021/12/23 15:06 * @version 1.0.0 */
public static FilePictureAttributeDTO pictureAttribute_url(String url) {
FilePictureAttributeDTO res = null;
File tempFile = null;
FileOutputStream fileOutputStream = null;
try {
if (StringUtils.isBlank(url)) {
return res;
}
long l = System.currentTimeMillis();
URL remoteUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) remoteUrl.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
tempFile = File.createTempFile(UUID.randomUUID().toString().replaceAll("-", ""), url.substring(url.lastIndexOf(".")));
fileOutputStream = new FileOutputStream(tempFile);
IoUtil.copy(inputStream, fileOutputStream);
log.info("pictureAttribute_url-下载到本地远程图片耗时:{}", System.currentTimeMillis() - l);
long l1 = System.currentTimeMillis();
BufferedImage bi = ImageIO.read(tempFile);
log.info("pictureAttribute_url-ImageIO.read耗时:{}", System.currentTimeMillis() - l1);
res = new FilePictureAttributeDTO().setWidth(bi.getWidth()).setHeight(bi.getHeight()).setSize(tempFile.length());
log.info("pictureAttribute_url-返回:{},总耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
} catch (Exception ex) {
log.error("{} - pictureAttribute_url-获取图片像素异常:{}", url, ex);
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != tempFile) {
tempFile.delete();
}
}
return res;
}
@Data
@Accessors(chain = true)
public class FilePictureAttributeDTO {
/** * 宽 */
private Integer width;
/** * 高 */
private Integer height;
/** * 大小 */
private Long size;
/** * 格式 */
private String format;
}
/** * 视频属性获取 * <p> * * @param path 本地视频地址 * @return * @author * @date 2021/12/23 14:31 * @version 1.0.0 */
public static FileVideoAttributeDTO videoAttribute(String path) {
FileVideoAttributeDTO res = null;
if (StringUtils.isBlank(path)) {
return res;
}
FileChannel fc = null;
try {
long l = System.currentTimeMillis();
File source = new File(path);
Encoder encoder = new Encoder();
it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);
FileInputStream fis = new FileInputStream(source);
fc = fis.getChannel();
if (null == m.getVideo()) {
throw new BusinessException(500, "解析视频video为空");
}
res = new FileVideoAttributeDTO().setVideoTime(m.getDuration() / 1000)
.setHeight(m.getVideo().getSize().getHeight())
.setWidth(m.getVideo().getSize().getWidth())
.setFrameRate(m.getVideo().getFrameRate())
.setBitRate(m.getVideo().getBitRate())
.setFormat(m.getFormat())
.setSize(fc.size())
;
log.info("videoAttribute获取视频属性返回:{},耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
return res;
// BigDecimal fileSize = new BigDecimal(fc.size());
// size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
} catch (Exception e) {
log.error("{} - videoAttribute获取视频属性异常:{}", path, e);
} finally {
if (null != fc) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}
/** * 视频属性获取 * <p> * * @param url 远程视频地址 * @return * @author * @date 2021/12/23 14:31 * @version 1.0.0 */
public static FileVideoAttributeDTO videoAttribute_url(String url) {
FileVideoAttributeDTO res = null;
if (StringUtils.isBlank(url)) {
return res;
}
FileChannel fc = null;
FileOutputStream fileOutputStream = null;
File tempFile = null;
InputStream inputStream = null;
try {
long l = System.currentTimeMillis();
URL remoteUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) remoteUrl.openConnection();
connection.setRequestMethod("GET");
inputStream = connection.getInputStream();
tempFile = File.createTempFile(UUID.randomUUID().toString().replaceAll("-", ""), url.substring(url.lastIndexOf(".")));
fileOutputStream = new FileOutputStream(tempFile);
IoUtil.copy(inputStream, fileOutputStream);
log.info("videoAttribute_url-下载远程视频至本地耗时:{}", (System.currentTimeMillis() - l));
long l3 = System.currentTimeMillis();
Encoder encoder = new Encoder();
it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(tempFile);
log.info("encoder.getInfo解析视频耗时:{}", (System.currentTimeMillis() - l3));
FileInputStream fis = new FileInputStream(tempFile);
fc = fis.getChannel();
if (null == m.getVideo()) {
throw new BusinessException(500, "解析视频video为空");
}
res = new FileVideoAttributeDTO().setVideoTime(m.getDuration() / 1000)
.setHeight(m.getVideo().getSize().getHeight())
.setWidth(m.getVideo().getSize().getWidth())
.setFormat(m.getFormat())
.setFrameRate(m.getVideo().getFrameRate())
.setBitRate(m.getVideo().getBitRate())
.setSize(fc.size())
;
log.info("videoAttribute获取视频属性返回:{},耗时:{}", JSON.toJSONString(res), System.currentTimeMillis() - l);
return res;
// BigDecimal fileSize = new BigDecimal(fc.size());
// size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
} catch (Exception e) {
log.error("{} - videoAttribute获取视频属性异常:{}", url, e);
} finally {
if (null != fc) {
try {
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != tempFile) {
tempFile.delete();
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return res;
}