|
@@ -5,6 +5,7 @@ import com.travel.util.ossFileUtil;
|
|
|
import org.apache.commons.fileupload.FileItem;
|
|
|
import org.apache.commons.fileupload.FileItemFactory;
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
@@ -18,15 +19,10 @@ import ws.schild.jave.encode.VideoAttributes;
|
|
|
import ws.schild.jave.info.MultimediaInfo;
|
|
|
import ws.schild.jave.info.VideoInfo;
|
|
|
import ws.schild.jave.info.VideoSize;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Random;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 视频转gif
|
|
@@ -38,6 +34,8 @@ public class VideiTiGif {
|
|
|
//输出格式
|
|
|
private static final String outputFormat = "gif";
|
|
|
|
|
|
+ private static final String urlGif = "C:\\video";
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 获得转化后的文件名
|
|
@@ -58,15 +56,17 @@ public class VideiTiGif {
|
|
|
* @param targetFilePath : 目标gif文件路径
|
|
|
* @return
|
|
|
*/
|
|
|
- public List transform(String sourceFilePath, String targetFilePath) throws IOException {
|
|
|
+ public Map<String, Object> transform(String sourceFilePath, String targetFilePath){
|
|
|
//存储视频跟git路径
|
|
|
List<String> list = new ArrayList<>();
|
|
|
+ Map<String, Object> mapData = new HashMap<>();
|
|
|
try {
|
|
|
File source = new File(sourceFilePath);//视频
|
|
|
File target = new File(targetFilePath);//图片
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ //---------------------------------------视频上传oos-------------------------------------------------------------
|
|
|
//往oos存储视频
|
|
|
MultipartFile file = getMulFileByPath(sourceFilePath); //file 转换成 MultipartFile
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
String suffix = file.getOriginalFilename()
|
|
|
.substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
// 生成文件名称
|
|
@@ -80,7 +80,7 @@ public class VideiTiGif {
|
|
|
if(!map.get("msg").equals("500")){
|
|
|
list.add((String) map.get("data"));
|
|
|
}
|
|
|
-
|
|
|
+ //----------------------------------------------视频生成gif-----------------------------------------------------
|
|
|
//获得原视频的分辨率
|
|
|
MultimediaObject mediaObject = new MultimediaObject(source);
|
|
|
MultimediaInfo multimediaInfo = mediaObject.getInfo();
|
|
@@ -100,42 +100,82 @@ public class VideiTiGif {
|
|
|
// 音频转换格式类
|
|
|
Encoder encoder = new Encoder();
|
|
|
encoder.encode(mediaObject, target, attrs);
|
|
|
- System.out.println("生成完成");
|
|
|
- MultipartFile file1 = getMulFileByPath(targetFilePath); //file 转换成 MultipartFile
|
|
|
- String suffix1 = file1.getOriginalFilename()
|
|
|
- .substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
- // 生成文件名称
|
|
|
- String nameSuffix1 = file1.getOriginalFilename()
|
|
|
- .substring(0, file.getOriginalFilename().lastIndexOf("."))
|
|
|
- .replaceAll(" ", "_").replaceAll(",", "")
|
|
|
- + format.format(DateUtil.date())
|
|
|
- + new Random().nextInt(1000) + suffix1;
|
|
|
- //获取返回结果
|
|
|
- Map<String, Object> map1 = ossFileUtil.uploadAliyun(file1, nameSuffix1);
|
|
|
- deleteFile(target.getPath());//上传结束之后删除git
|
|
|
- //判断如果不出错误则存储路径
|
|
|
- if(!map1.get("msg").equals("500")){
|
|
|
- list.add((String) map1.get("data"));
|
|
|
+ System.out.println("转换已完成...");
|
|
|
+ //---------------------------------------------gif上传oos------------------------------------------
|
|
|
+ //gif上传oos
|
|
|
+ MultipartFile file1 = getMulFileByPath(targetFilePath);
|
|
|
+ if(!file1.isEmpty()){
|
|
|
+ String suffix1 = file1.getOriginalFilename()
|
|
|
+ .substring(file1.getOriginalFilename().lastIndexOf("."));
|
|
|
+ // 生成文件名称
|
|
|
+ String nameSuffix1 = file1.getOriginalFilename()
|
|
|
+ .substring(0, file1.getOriginalFilename().lastIndexOf("."))
|
|
|
+ .replaceAll(" ", "_").replaceAll(",", "")
|
|
|
+ + format.format(DateUtil.date())
|
|
|
+ + new Random().nextInt(1000) + suffix1;
|
|
|
+ Map<String, Object> mapOos = ossFileUtil.uploadAliyun(file1, nameSuffix1);
|
|
|
+ deleteFile(urlGif);//上传结束之后删除制定文件夹下所有
|
|
|
+ //判断如果不出错误则存储路径
|
|
|
+ if(!mapOos.get("msg").equals("500")){
|
|
|
+ list.add((String) mapOos.get("data"));
|
|
|
+ }
|
|
|
}
|
|
|
- } catch (EncoderException e) {
|
|
|
+ mapData.put("msg", "200");
|
|
|
+ mapData.put("data", list);
|
|
|
+ } catch (EncoderException | IOException e) {
|
|
|
e.printStackTrace();
|
|
|
+ mapData.put("msg", "500");
|
|
|
}
|
|
|
- return list;
|
|
|
+ return mapData;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 批量转化视频格式
|
|
|
*
|
|
|
- * @param urlFile : 文件名
|
|
|
+ * @param file : 视频
|
|
|
* @return
|
|
|
*/
|
|
|
@RequestMapping("batchTransform")
|
|
|
@ResponseBody
|
|
|
- public void batchTransform(String urlFile) throws IOException {
|
|
|
- File file = new File(urlFile);
|
|
|
- if(file.length() != 0) {
|
|
|
- List<String> list = transform(file.getPath(), file.getPath().replace(file.getName(), "") + getNewFileName(file.getName()));
|
|
|
+ public Map<String, Object> batchTransform(MultipartFile file) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ if(!file.isEmpty()) {
|
|
|
+ deleteFile(urlGif);
|
|
|
+ Map<String, Object> uploadMap = upload(file);
|
|
|
+ if(uploadMap.get("msg").equals("200")){
|
|
|
+ String videoUrl = urlGif + "\\" +uploadMap.get("fileName");
|
|
|
+ map = transform(videoUrl, urlGif + "\\" + getNewFileName(videoUrl));
|
|
|
+ } else {
|
|
|
+ map = upload(file);
|
|
|
+ }
|
|
|
}
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ * @param files
|
|
|
+ */
|
|
|
+ public Map<String, Object> upload(MultipartFile files) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ File tmpFile = new File(urlGif + "\\" + files.getOriginalFilename());
|
|
|
+ try (BufferedOutputStream outBuff = new BufferedOutputStream(new FileOutputStream(tmpFile)); BufferedInputStream inBuff = new BufferedInputStream(files.getInputStream())) {
|
|
|
+ // 缓冲数组
|
|
|
+ byte[] b = new byte[1024 * 5];
|
|
|
+ int len;
|
|
|
+ while ((len = inBuff.read(b)) != -1) {
|
|
|
+ outBuff.write(b, 0, len);
|
|
|
+ }
|
|
|
+ // 刷新此缓冲的输出流
|
|
|
+ outBuff.flush();
|
|
|
+ resultMap.put("msg", "200");
|
|
|
+ resultMap.put("fileName", files.getOriginalFilename());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ resultMap.put("msg", "500");
|
|
|
+ }
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -171,14 +211,55 @@ public class VideiTiGif {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 删除文件
|
|
|
- * @param fileUrl 文件路径
|
|
|
+ * 删除文件夹下所有文件
|
|
|
+ */
|
|
|
+ public void deleteFile(String path){
|
|
|
+ File file = new File(path);
|
|
|
+ //如果文件夹不存在则先创建
|
|
|
+ if(!file.exists()){
|
|
|
+ mkirksFile();
|
|
|
+ }
|
|
|
+ String[] content = file.list();//取得当前目录下所有文件和文件夹
|
|
|
+ if(content.length != 0){
|
|
|
+ for(String name : content){
|
|
|
+ File temp = new File(path, name);
|
|
|
+ if(temp.isDirectory()){//判断是否是目录
|
|
|
+ deleteFile(temp.getAbsolutePath());//递归调用,删除目录里的内容
|
|
|
+ temp.delete();//删除空目录
|
|
|
+ }else{
|
|
|
+ if(!temp.delete()){//直接删除文件
|
|
|
+ System.err.println("删除文件为 " + name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MultipartFile转换成file
|
|
|
+ * @param multiFile
|
|
|
+ * @return
|
|
|
*/
|
|
|
- public void deleteFile(String fileUrl){
|
|
|
- File file = new File(fileUrl);
|
|
|
- if(file.exists()){
|
|
|
- file.delete();
|
|
|
- System.out.println("删除成功");
|
|
|
+ public File MultipartFileToFile(MultipartFile multiFile) {
|
|
|
+ // 获取文件名
|
|
|
+ String fileName = multiFile.getOriginalFilename();
|
|
|
+ // 获取文件后缀
|
|
|
+ String prefix = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ // 若须要防止生成的临时文件重复,能够在文件名后添加随机码
|
|
|
+ try {
|
|
|
+ File file = File.createTempFile(fileName, prefix);
|
|
|
+ multiFile.transferTo(file);
|
|
|
+ return file;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void mkirksFile(){
|
|
|
+ File file = new File(urlGif);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
}
|
|
|
}
|
|
|
}
|