Browse Source

上传音频

dengsixing 2 years ago
parent
commit
d3b02181f4

+ 19 - 0
src/main/java/com/fdkankan/scene/controller/SceneEditController.java

@@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -428,6 +429,24 @@ public class SceneEditController extends BaseController {
         return sceneProService.downloadVisionData(sceneNum);
     }
 
+    /**
+     * 上传音频
+     * @param file
+     * @return
+     */
+    @CheckCurrentUser(description = "上传音频")
+    @PostMapping(value = "/uploadScreencapVoiceNew")
+    public ResultData uploadScreencapVoice(HttpServletRequest request, HttpServletResponse response, @RequestParam("file")  MultipartFile file) throws Exception {
+        String sceneNum = request.getParameter("sceneNum");
+        String type = request.getParameter("type");
+        String fileName = request.getParameter("fileName");
+        String length = request.getParameter("length");
+        //替换视频,1为替换,0或没值为不替换
+        String replace = request.getParameter("replace");
+
+        return sceneProService.uploadScreencapVoiceNew(sceneNum, type, fileName, file, length, replace);
+    }
+
 
 
 }

+ 2 - 0
src/main/java/com/fdkankan/scene/service/ISceneProService.java

@@ -97,4 +97,6 @@ public interface ISceneProService extends IService<ScenePro> {
 
     ResultData downloadVisionData(String sceneNum) throws Exception;
 
+    ResultData uploadScreencapVoiceNew(String sceneNum, String type, String fileName, MultipartFile file, String length, String replace) throws Exception;
+
 }

+ 187 - 0
src/main/java/com/fdkankan/scene/service/impl/SceneProServiceImpl.java

@@ -3452,4 +3452,191 @@ public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro>
         fYunFileService.uploadFile(dataBuffer.toString() + "vision.json", dataBuf.toString() + "vision.json");
         return ResultData.ok(fyunHost + dataBuf.toString() + "vision.json?t=" + System.currentTimeMillis());
     }
+
+    @Override
+    public ResultData uploadScreencapVoiceNew(String sceneNum, String type, String fileName, MultipartFile file, String length, String replace) throws Exception{
+        if(StrUtil.isEmpty(sceneNum)){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_3001);
+        }
+
+        ScenePro sceneProEntity = this.findBySceneNum(sceneNum);
+        if(sceneProEntity == null){
+            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
+        }
+
+        if (!file.isEmpty()&& file.getSize() <= 0) {
+            throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
+        }
+        //文件上传的位置可以自定义
+        String path = ConstantFilePath.SCENE_PATH+"voice"+File.separator+"voice"+sceneNum;
+        File targetFile = new File(path);
+        if (!targetFile.exists()) {
+            targetFile.mkdirs();
+        }
+
+        //获取文件名去掉后缀
+        String originalFileName = file.getOriginalFilename().substring(0,
+            file.getOriginalFilename().indexOf(".") > 0 ? file.getOriginalFilename().indexOf(".") : file.getOriginalFilename().length());
+        log.info("上传的音频文件名:" + file.getOriginalFilename());
+        String soundFileName = "sound201810.wav";
+
+        String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json");
+        JSONObject scenejson = new JSONObject();
+        if(strsceneInfos!=null) {
+            scenejson = JSONObject.parseObject(strsceneInfos);
+        }
+        if("soundsync".equals(type) && !"1".equals(replace)){
+            synchronized(this){
+                if(scenejson.containsKey("screencapVoiceSoundsync") && StrUtil.isNotEmpty(scenejson.getString("screencapVoiceSoundsync"))){
+                    String target = ConstantFilePath.SCENE_PATH + scenejson.getString("screencapVoiceSoundsync").substring(
+                        scenejson.getString("screencapVoiceSoundsync").indexOf("voice"));
+
+                    if(new File(target).exists()){
+                        targetFile = new File(path + File.separator +soundFileName);
+                        if(targetFile.exists())
+                        {
+                            FileUtils.deleteFile(path + File.separator + soundFileName);
+                        }
+                        file.transferTo(targetFile);
+                        FileUtils.changeVoiceToMp3(path + File.separator + soundFileName, path + File.separator + "newSound201810.mp3");
+
+                        //目标文件改名
+                        new File(target).renameTo(new File(path + File.separator + "oldSound201810.mp3"));
+
+                        //判断音频时长,不够拼接空白音效
+                        double total = Double.parseDouble(length);
+                        double now = (FileUtils.getAudioPlayTime(new File(path + File.separator + "oldSound201810.mp3")) +
+                            FileUtils.getAudioPlayTime(new File(path + File.separator + "newSound201810.mp3"))) / 1000.0;
+                        if(total - now > 0.5){
+                            CreateObjUtil.createMuteViode(total - now, path + File.separator + "muteSound.mp3");
+                            if(new File(path + File.separator + "muteSound.mp3").exists()){
+                                //需要将旧的音频改名
+                                new File(path + File.separator + "oldSound201810.mp3").renameTo(new File(path + File.separator + "oldMuteSound201810.mp3"));
+                                log.info("生成新的静音音频和旧的音频合并");
+                                CreateObjUtil.mergeVideo(path + File.separator + "oldMuteSound201810.mp3", path + File.separator + "muteSound.mp3",
+                                    path + File.separator + "oldSound201810.mp3");
+                            }
+                        }
+
+                        //app端和pc端名字不一致,统一文件名字
+                        target = path + File.separator + originalFileName + ".mp3";
+                        CreateObjUtil.mergeVideo(path + File.separator + "oldSound201810.mp3", path + File.separator + "newSound201810.mp3", target);
+                        //删除改名后的文件
+//                    new File(path + File.separator + "oldSound201810.mp3").delete();
+
+                        Map map = new HashMap();
+                        map.put("screencapVoiceSoundsyncFileName", originalFileName + ".mp3");
+                        String proVideo = "voice/voice"+sceneNum+"/"+originalFileName  + ".mp3";
+                        map.put("screencapVoiceSoundsync", proVideo);
+                        map.put("uploadVoiceSoundsync", 1);
+                        map.put("screencapVoiceType", type);
+                        map.put("version", scenejson.getIntValue("version")+1);
+                        FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
+                        return ResultData.ok("voice/voice"+sceneNum+"/"+originalFileName + ".mp3");
+                    }
+
+                }else {
+                    targetFile = new File(path + File.separator +soundFileName);
+                    if(targetFile.exists())
+                    {
+                        FileUtils.deleteFile(path + File.separator + soundFileName);
+                    }
+                    file.transferTo(targetFile);
+                    //判断音频时长,不够拼接空白音效
+                    FileUtils.changeVoiceToMp3(path + File.separator + soundFileName, path + File.separator + "newSound201810.mp3");
+                    double total = Double.parseDouble(length);
+                    double now = (FileUtils.getAudioPlayTime(new File(path + File.separator + "newSound201810.mp3"))) / 1000.0;
+                    log.info("前端传的时长:" + total);
+                    log.info("获取文件的时长:" + now);
+
+                    //同步录音前一段没空白,直接生成文件
+                    if(total - now < 0.5){
+                        FileUtils.changeVoiceToMp3(path + File.separator + soundFileName, path + File.separator + originalFileName + ".mp3");
+                        Map map = new HashMap();
+                        map.put("screencapVoiceSoundsyncFileName", originalFileName + ".mp3");
+                        String proVideo = "voice/voice"+sceneNum+"/"+originalFileName  + ".mp3";
+                        map.put("screencapVoiceSoundsync", proVideo);
+                        map.put("uploadVoiceSoundsync", 1);
+                        map.put("screencapVoiceType", type);
+                        map.put("version", scenejson.getIntValue("version")+1);
+
+                        FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
+                        //uploadToQiniuForScene3DService.upload(path + File.separator +fileName, "voice/voice"+sceneNum+"/"+CONSTANTFILENAME.VOICENAME +fileName.substring(fileName.lastIndexOf(".")));
+                        //sceneService.updateScreencapMusic(sceneNum, voiceSrc);
+                        return ResultData.ok(proVideo);
+                    }
+
+                    ////同步录音前一段有空白,生成一段静音后拼接文件
+                    CreateObjUtil.createMuteViode(total - now, path + File.separator + "muteSound.mp3");
+
+                    //app端和pc端名字不一致,统一文件名字
+                    String target = path + File.separator + originalFileName + ".mp3";
+                    CreateObjUtil.mergeVideo(path + File.separator + "muteSound.mp3", path + File.separator + "newSound201810.mp3", target);
+
+                    Map map = new HashMap();
+                    map.put("screencapVoiceSoundsyncFileName", originalFileName + ".mp3");
+                    String proVideo = "voice/voice"+sceneNum+"/"+originalFileName  + ".mp3";
+                    map.put("screencapVoiceSoundsync", proVideo);
+                    map.put("uploadVoiceSoundsync", 1);
+                    map.put("screencapVoiceType", type);
+                    map.put("version", scenejson.getIntValue("version")+1);
+                    FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
+                    return ResultData.ok("voice/voice"+sceneNum+"/"+originalFileName + ".mp3");
+                }
+
+            }
+        }
+
+        String name = "201810.wav";
+        targetFile = new File(path + File.separator +name);
+        // 保存
+        synchronized(this){
+            if(targetFile.exists())
+            {
+                FileUtils.deleteFile(path + File.separator + name);
+            }
+            file.transferTo(targetFile);
+        }
+        Map map = new HashMap();
+
+        if("soundsync".equals(type)){
+            FileUtils.changeVoiceToMp3(path + File.separator + name, path + File.separator + originalFileName + ".mp3");
+            map.put("screencapVoiceSoundsyncFileName", originalFileName + ".mp3");
+            String proVideo = "voice/voice"+sceneNum+"/"+originalFileName  + ".mp3";
+            map.put("screencapVoiceSoundsync", proVideo);
+            map.put("uploadVoiceSoundsync", 1);
+            map.put("version", scenejson.getIntValue("version")+1);
+            map.put("screencapVoiceType", type);
+            FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
+            return ResultData.ok(proVideo);
+        }
+
+        if("sound".equals(type)){
+            FileUtils.changeVoiceToMp3(path + File.separator + name, path + File.separator + originalFileName + ".mp3");
+            map.put("screencapVoiceSoundFileName", originalFileName + ".mp3");
+            String proVideo = "voice/voice"+sceneNum+"/"+originalFileName  + ".mp3";
+            map.put("screencapVoiceSound", proVideo);
+            map.put("uploadVoiceSound", 1);
+            map.put("version", scenejson.getIntValue("version")+1);
+            map.put("screencapVoiceType", type);
+            FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
+            return ResultData.ok(proVideo);
+        }
+
+        FileUtils.changeVoiceToMp3(path + File.separator + name, path + File.separator +"201810.mp3");
+        if(StrUtil.isEmpty(fileName)){
+            map.put("screencapVoiceFileName", originalFileName + ".mp3");
+        }else {
+            map.put("screencapVoiceFileName", fileName);
+        }
+        String voiceSrc = "voice/voice"+sceneNum+"/"+"201810.mp3";
+        map.put("screencapVoiceSrc", voiceSrc);
+        map.put("uploadVoiceSrc", 1);
+        map.put("version", scenejson.getIntValue("version")+1);
+        map.put("screencapVoiceType", type);
+        FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + File.separator + "scene.json", map);
+        //uploadToQiniuForScene3DService.upload(path + File.separator +fileName, "voice/voice"+sceneNum+"/"+CONSTANTFILENAME.VOICENAME +fileName.substring(fileName.lastIndexOf(".")));
+        //sceneService.updateScreencapMusic(sceneNum, voiceSrc);
+        return ResultData.ok(voiceSrc);
+    }
 }