Commit 17f61a23 authored by yanzg's avatar yanzg

参数处理

parent 0dbf312b
......@@ -51,7 +51,7 @@ public interface YzgFileService {
*
* @param req
*/
void convertVideoFirst(YzgFileMoveItemReqVo req);
void convertVideoFirst(YzgFileConvertVideoFirstReqVo req);
/**
* 转换视频质量
......
package com.yanzuoguang.cloud.file.vo;
import io.swagger.annotations.ApiModelProperty;
/**
* 转换基础类
*
* @author 颜佐光
*/
public class YzgFileConvertBase {
/**
* 高质量品质
*/
public static final YzgFileConvertBase VIDEO_TYPE_HIGH = new YzgFileConvertBase(
0.01f, 0.01f, 1920, 1080, 8311136
);
/**
* 常规品质
*/
public static final YzgFileConvertBase VIDEO_TYPE_COMMON = new YzgFileConvertBase(
0.01f, 0.01f, 800, 600, 600000
);
/**
* 低质量品质
*/
public static final YzgFileConvertBase VIDEO_TYPE_LOW = new YzgFileConvertBase(
0.01f, 0.01f, 960, 0, 2955811
);
@ApiModelProperty(notes = "压缩尺寸比例,默认为1,范围在0~1之间.")
protected float size = 1f;
@ApiModelProperty(notes = "质量压缩比例,默认为1,范围在0~1之间.")
protected float quote = 1f;
@ApiModelProperty(notes = "最低宽度,如: 1920*1080中的1920.")
protected int width = 0;
@ApiModelProperty(notes = "最低高度,如: 1920*1080中的1080.")
protected int height = 0;
@ApiModelProperty(notes = "最低质量值")
protected int bitrate = 0;
public YzgFileConvertBase() {
}
public YzgFileConvertBase(float size, float quote, int width, int height, int bitrate) {
this.size = size;
this.quote = quote;
this.width = width;
this.height = height;
this.bitrate = bitrate;
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
public float getQuote() {
return quote;
}
public void setQuote(float quote) {
this.quote = quote;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getBitrate() {
return bitrate;
}
public void setBitrate(int bitrate) {
this.bitrate = bitrate;
}
}
package com.yanzuoguang.cloud.file.vo;
import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 转换文件首帧信息
*
* @author 颜佐光
*/
@ApiModel(description = "转换文件首帧信息")
public class YzgFileConvertVideoFirstReqVo extends BaseVo {
/**
* 转换文件的来源路径,当目标路径存在时,则认为已经移动成功
*/
@ApiModelProperty(notes = "移动文件的来源路径,当目标路径存在时,则认为已经移动成功", position = 1000)
private String from;
/**
* 转换文件的目标路径,建议扩展名为jpg
*/
@ApiModelProperty(notes = "转换文件的目标路径,建议扩展名为jpg", position = 1010)
private String to;
public YzgFileConvertVideoFirstReqVo() {
}
public YzgFileConvertVideoFirstReqVo(String from) {
this.from = from;
}
public YzgFileConvertVideoFirstReqVo(String from, String to) {
this.from = from;
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
......@@ -13,51 +13,51 @@ public class YzgFileVideoImageInfoVo extends YzgFileBaseVo {
* 分辨率x,如: 1920*1080中的1920.
*/
@ApiModelProperty(notes = "分辨率x,如: 1920*1080中的1920.")
private Long width;
private Integer width;
/**
* 分辨率y,如: 1920*1080中的1080.
*/
@ApiModelProperty(notes = "分辨率y,如: 1920*1080中的1080.")
private Long height;
private Integer height;
/**
* 质量
*/
@ApiModelProperty(notes = "质量", position = 1040)
private Long bitrate;
private Integer bitrate;
public YzgFileVideoImageInfoVo() {
}
public YzgFileVideoImageInfoVo(String server, String display, long size, String mime, int type, Long width, Long height, Long bitrate) {
public YzgFileVideoImageInfoVo(String server, String display, long size, String mime, int type, Integer width, Integer height, Integer bitrate) {
super(server, display, size, mime, type);
this.width = width;
this.height = height;
this.bitrate = bitrate;
}
public Long getWidth() {
public Integer getWidth() {
return width;
}
public void setWidth(Long width) {
public void setWidth(Integer width) {
this.width = width;
}
public Long getHeight() {
public Integer getHeight() {
return height;
}
public void setHeight(Long height) {
public void setHeight(Integer height) {
this.height = height;
}
public Long getBitrate() {
public Integer getBitrate() {
return bitrate;
}
public void setBitrate(Long bitrate) {
public void setBitrate(Integer bitrate) {
this.bitrate = bitrate;
}
}
package com.yanzuoguang.cloud.file.vo;
import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
......@@ -10,93 +9,29 @@ import javax.validation.constraints.NotNull;
*
* @author 颜佐光
*/
public class YzgFileVideoImageItemReqVo extends BaseVo {
/**
* 高质量品质
*/
public static final YzgFileVideoImageItemReqVo VIDEO_TYPE_HIGH = new YzgFileVideoImageItemReqVo(1D, 1D, 1920, 1080);
/**
* 常规品质
*/
public static final YzgFileVideoImageItemReqVo VIDEO_TYPE_COMMON = new YzgFileVideoImageItemReqVo(1D, 1D, 800, 600);
/**
* 低质量品质
*/
public static final YzgFileVideoImageItemReqVo VIDEO_TYPE_LOW = new YzgFileVideoImageItemReqVo(1D, 1D, 960, 0);
public class YzgFileVideoImageItemReqVo extends YzgFileConvertBase {
@ApiModelProperty(notes = "目标文件")
private String toFile;
@ApiModelProperty(notes = "压缩尺寸比例,默认为1,范围在0~1之间.")
private double size = 1D;
@ApiModelProperty(notes = "质量压缩比例,默认为1,范围在0~1之间.")
private double quote = 1D;
@ApiModelProperty(notes = "分辨率x,如: 1920*1080中的1920.")
private long width = 0;
@ApiModelProperty(notes = "分辨率x,如: 1920*1080中的1080.")
private long height = 0;
private String to;
public YzgFileVideoImageItemReqVo() {
}
private YzgFileVideoImageItemReqVo(double size, double quote, long width, long height) {
this.size = size;
this.quote = quote;
this.width = width;
this.height = height;
}
public YzgFileVideoImageItemReqVo(String toFile, @NotNull YzgFileVideoImageItemReqVo from) {
this(from.size, from.quote, from.width, from.height);
this.toFile = toFile;
}
public YzgFileVideoImageItemReqVo(String toFile, double size, double quote, long width, long height) {
this(size, quote, width, height);
this.toFile = toFile;
}
public String getToFile() {
return toFile;
}
public void setToFile(String toFile) {
this.toFile = toFile;
}
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getQuote() {
return quote;
}
public void setQuote(double quote) {
this.quote = quote;
}
public long getWidth() {
return width;
public YzgFileVideoImageItemReqVo(String to, @NotNull YzgFileConvertBase from) {
super(from.size, from.quote, from.width, from.height, from.bitrate);
this.to = to;
}
public void setWidth(long width) {
this.width = width;
public YzgFileVideoImageItemReqVo(String to, float size, float quote, int width, int height, int bitrate) {
super(size, quote, width, height, bitrate);
this.to = to;
}
public long getHeight() {
return height;
public String getTo() {
return to;
}
public void setHeight(long height) {
this.height = height;
public void setTo(String to) {
this.to = to;
}
}
......@@ -17,36 +17,36 @@ public class YzgFileVideoImageReqVo extends YzgFileMoveItemReqVo {
* 压缩后的文件参数集合
*/
@ApiModelProperty(notes = "压缩后的文件参数集合", position = 10000)
private List<YzgFileVideoImageReqVo> list = new ArrayList<>();
private List<YzgFileVideoImageItemReqVo> list = new ArrayList<>();
public YzgFileVideoImageReqVo() {
}
public YzgFileVideoImageReqVo(String from, YzgFileVideoImageReqVo... list) {
public YzgFileVideoImageReqVo(String from, YzgFileVideoImageItemReqVo... list) {
super(from);
this.list.addAll(Arrays.asList(list));
}
public YzgFileVideoImageReqVo(String from, String to, YzgFileVideoImageReqVo... list) {
public YzgFileVideoImageReqVo(String from, String to, YzgFileVideoImageItemReqVo... list) {
super(from, to);
this.list.addAll(Arrays.asList(list));
}
public YzgFileVideoImageReqVo(String from, List<YzgFileVideoImageReqVo> list) {
public YzgFileVideoImageReqVo(String from, List<YzgFileVideoImageItemReqVo> list) {
super(from);
this.list = list;
}
public YzgFileVideoImageReqVo(String from, String to, List<YzgFileVideoImageReqVo> list) {
public YzgFileVideoImageReqVo(String from, String to, List<YzgFileVideoImageItemReqVo> list) {
super(from, to);
this.list = list;
}
public List<YzgFileVideoImageReqVo> getList() {
public List<YzgFileVideoImageItemReqVo> getList() {
return list;
}
public void setList(List<YzgFileVideoImageReqVo> list) {
public void setList(List<YzgFileVideoImageItemReqVo> list) {
this.list = list;
}
}
......@@ -8,7 +8,6 @@ import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.StringHelper;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
......@@ -22,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.List;
/**
* 文件上传服务实现
......@@ -126,6 +126,7 @@ public class YzgFileServiceImpl implements YzgFileService {
if (req.getList() == null || req.getList().isEmpty()) {
throw new CodeException("请传入需要移动的文件");
}
// 先检查一遍参数
for (YzgFileMoveItemReqVo item : req.getList()) {
if (StringHelper.isEmpty(item.getFrom())) {
......@@ -176,7 +177,6 @@ public class YzgFileServiceImpl implements YzgFileService {
throw new CodeException(String.format("移动文件 %s 到 %s 失败,原因:%s", item.getFrom(), item.getTo(), ex.getMessage()));
}
}
}
/**
......@@ -195,20 +195,20 @@ public class YzgFileServiceImpl implements YzgFileService {
switch (info.getType()) {
case FileHelper.FILE_TYPE_AUDIO: {
MediaParameter parameter = MediaHelper.getGrabberParameter(serverFullPath);
info.setBitrate(Long.valueOf(parameter.getAudioBitRate()));
info.setBitrate(parameter.getAudioBitRate());
break;
}
case FileHelper.FILE_TYPE_VIDEO: {
MediaParameter parameter = MediaHelper.getGrabberParameter(serverFullPath);
info.setBitrate(Long.valueOf(parameter.getVideoBitRate()));
info.setWidth(Long.valueOf(parameter.getVideoWidth()));
info.setHeight(Long.valueOf(parameter.getVideoHeight()));
info.setBitrate(parameter.getVideoBitRate());
info.setWidth(parameter.getVideoWidth());
info.setHeight(parameter.getVideoHeight());
break;
}
case FileHelper.FILE_TYPE_IMAGE: {
BufferedImage sourceImg = ImageIO.read(new FileInputStream(serverFullPath));
info.setWidth(Long.valueOf(sourceImg.getWidth()));
info.setHeight(Long.valueOf(sourceImg.getHeight()));
info.setWidth(sourceImg.getWidth());
info.setHeight(sourceImg.getHeight());
break;
}
case FileHelper.FILE_TYPE_NONE:
......@@ -238,7 +238,7 @@ public class YzgFileServiceImpl implements YzgFileService {
* @param req
*/
@Override
public void convertVideoFirst(YzgFileMoveItemReqVo req) {
public void convertVideoFirst(YzgFileConvertVideoFirstReqVo req) {
}
......@@ -249,7 +249,54 @@ public class YzgFileServiceImpl implements YzgFileService {
*/
@Override
public void convertVideo(YzgFileVideoImageReqVo req) {
List<YzgFileVideoImageItemReqVo> toFiles = req.getList();
if (toFiles == null || toFiles.isEmpty()) {
return;
}
// 先循环检测参数
checkFolder(req.getFrom());
for (YzgFileVideoImageItemReqVo item : req.getList()) {
checkFolder(item.getTo());
}
// 来源文件路径
String from = fileConfig.getServerFullPath(req.getFrom());
for (YzgFileVideoImageItemReqVo item : req.getList()) {
String to = fileConfig.getServerFullPath(item.getTo());
// 先压缩到临时文件
String toFileTemp = to + StringHelper.getNewID() + ".tmp";
MediaParameter parameter = new MediaParameter(item.getSize(), item.getQuote());
// 设置最小宽度,指的是宽度和高度中的大值,宽度>高度
parameter.setMinVideoWidth(StringHelper.getFirst(item.getWidth(), parameter.getMinVideoWidth()));
// 设置最小高度,指的是宽度和高度中的小值,宽度>高度
parameter.setMinVideoHeight(StringHelper.getFirst(item.getHeight(), parameter.getMinVideoHeight()));
// 设置视频最小比特率
parameter.setMinVideoBitRate(StringHelper.getFirst(item.getBitrate(), parameter.getMinVideoBitRate()));
// 将临时文件改成具体的文件
File fileTemp = new File(toFileTemp);
File file = new File(to);
try {
if (fileTemp.exists()) {
fileTemp.delete();
}
// 压缩视频到临时文件
MediaHelper.zipVideoMp4(from, toFileTemp, parameter);
// 判断文件是否存在,不存在则更改临时文件名
if (file.exists()) {
file.delete();
}
// 判断文件是否存在,文件不存在则压缩,文件存在则不压缩
fileTemp.renameTo(file);
} catch (IOException ex) {
throw new CodeException(ex);
} finally {
// 删除临时文件
if (fileTemp.exists()) {
fileTemp.delete();
}
}
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment