zhs vor 3 Jahren
Ursprung
Commit
450878d744

+ 3 - 0
build.gradle

@@ -35,6 +35,9 @@ dependencies {
     implementation 'com.microsoft.sqlserver:mssql-jdbc:7.4.1.jre8'
     implementation 'org.springframework.boot:spring-boot-starter-aop'
     implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.5.4'
+    implementation 'ws.schild:jave-core:3.1.1'
+    implementation 'ws.schild:jave-nativebin-win64:3.1.1'
+    implementation 'commons-fileupload:commons-fileupload:1.3.2'
     implementation fileTree(dir:'lib',includes:['*jar'])
     annotationProcessor 'org.projectlombok:lombok'
     developmentOnly 'org.springframework.boot:spring-boot-devtools'

+ 184 - 0
src/main/java/com/travel/controller/VideiTiGif.java

@@ -0,0 +1,184 @@
+package com.travel.controller;
+
+import cn.hutool.core.date.DateUtil;
+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.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+import ws.schild.jave.Encoder;
+import ws.schild.jave.EncoderException;
+import ws.schild.jave.MultimediaObject;
+import ws.schild.jave.encode.EncodingAttributes;
+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.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * 视频转gif
+ */
+@Controller
+@RequestMapping("videoGif")
+public class VideiTiGif {
+
+    //输出格式
+    private static final String outputFormat = "gif";
+
+
+    /**
+     * 获得转化后的文件名
+     *
+     * @param sourceFilePath : 源视频文件路径
+     * @return
+     */
+    public static String getNewFileName(String sourceFilePath) {
+        File source = new File(sourceFilePath);
+        String fileName = source.getName().substring(0, source.getName().lastIndexOf("."));
+        return fileName + "." + outputFormat;
+    }
+
+    /**
+     * 转化音频格式
+     *
+     * @param sourceFilePath : 源视频文件路径
+     * @param targetFilePath : 目标gif文件路径
+     * @return
+     */
+    public List transform(String sourceFilePath, String targetFilePath) throws IOException {
+        //存储视频跟git路径
+        List<String> list = new ArrayList<>();
+        try {
+            File source = new File(sourceFilePath);//视频
+            File target = new File(targetFilePath);//图片
+            //往oos存储视频
+            MultipartFile file = getMulFileByPath(sourceFilePath); //file 转换成 MultipartFile
+            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
+            String suffix = file.getOriginalFilename()
+                    .substring(file.getOriginalFilename().lastIndexOf("."));
+            // 生成文件名称
+            String nameSuffix = file.getOriginalFilename()
+                    .substring(0, file.getOriginalFilename().lastIndexOf("."))
+                    .replaceAll(" ", "_").replaceAll(",", "")
+                    + format.format(DateUtil.date())
+                    + new Random().nextInt(1000) + suffix;
+            Map<String, Object> map = ossFileUtil.uploadAliyun(file, nameSuffix);
+            //判断如果不出错误则存储路径
+            if(!map.get("msg").equals("500")){
+                list.add((String) map.get("data"));
+            }
+
+            //获得原视频的分辨率
+            MultimediaObject mediaObject = new MultimediaObject(source);
+            MultimediaInfo multimediaInfo = mediaObject.getInfo();
+            VideoInfo videoInfo = multimediaInfo.getVideo();
+            VideoSize sourceSize = videoInfo.getSize();
+            //设置视频属性
+            VideoAttributes video = new VideoAttributes();
+            video.setCodec(outputFormat);
+            //设置视频帧率 正常为10 ,值越大越流畅
+            video.setFrameRate(80);
+            //设置视频分辨率
+            VideoSize targetSize = new VideoSize(sourceSize.getWidth() / 5, sourceSize.getHeight() / 5);
+            video.setSize(targetSize);
+            //设置转码属性
+            EncodingAttributes attrs = new EncodingAttributes();
+            attrs.setVideoAttributes(video);
+            // 音频转换格式类
+            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"));
+            }
+        } catch (EncoderException e) {
+            e.printStackTrace();
+        }
+        return list;
+    }
+
+    /**
+     * 批量转化视频格式
+     *
+     * @param urlFile : 文件名
+     * @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()));
+        }
+    }
+
+    /**
+     * file转换成MultipartFile
+     * @param filePath 路径
+     * @return 返回结果
+     */
+    public MultipartFile getMulFileByPath(String filePath){
+        FileItemFactory factory = new DiskFileItemFactory(16, null);
+        String textFieldName = "textField";
+        int num = filePath.lastIndexOf(".");
+        String extFile = filePath.substring(num);
+        FileItem item = factory.createItem(textFieldName, "text/plain", true,
+                "MyFileName" + extFile);
+        File newfile = new File(filePath);
+        int bytesRead = 0;
+        byte[] buffer = new byte[8192];
+        try{
+            FileInputStream fis = new FileInputStream(newfile);
+            OutputStream os = item.getOutputStream();
+            while ((bytesRead = fis.read(buffer, 0, 8192))
+                    != -1)
+            {
+                os.write(buffer, 0, bytesRead);
+            }
+            os.close();
+            fis.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        MultipartFile mfile = new CommonsMultipartFile(item);
+        return mfile;
+    }
+
+    /**
+     * 删除文件
+     * @param fileUrl 文件路径
+     */
+    public void deleteFile(String fileUrl){
+        File file = new File(fileUrl);
+        if(file.exists()){
+            file.delete();
+            System.out.println("删除成功");
+        }
+    }
+}

+ 1 - 1
src/main/resources/config/mapping/ReleaseTravelMapper.xml

@@ -14,7 +14,7 @@
     <!-- 查询游记内容 -->
     <select id="getTravel" resultType="Travel">
         select
-               a.*,b.countNum,c.flag,d.commCount
+            a.*,b.countNum,c.flag,d.commCount
         from bus_travel a
         JOIN ( SELECT COUNT ( * ) AS countNum, travel_uuid FROM sys_travel_give GROUP BY travel_uuid ) b ON a.uuid = b.travel_uuid
         JOIN ( SELECT COUNT ( * ) AS flag, travel_uuid FROM sys_travel_give WHERE code = #{code} GROUP BY travel_uuid ) c ON a.uuid = c.travel_uuid