Ver código fonte

视频合成

wangyunpeng 2 semanas atrás
pai
commit
25994d7312

+ 2 - 2
core/src/main/java/com/tzld/supply/job/ContentMediaSearchJob.java

@@ -60,7 +60,7 @@ public class ContentMediaSearchJob {
             Long now = System.currentTimeMillis();
             for (ContentSearchResponse.DataItem dataItem : dataItems) {
                 // 2. 调用saveInPublic转存到OSS
-                String fileName = String.format("image/%s_%d.jpg", content.getId(), System.currentTimeMillis());
+                String fileName = String.format("supply/image/%s_%d.jpg", content.getId(), System.currentTimeMillis());
                 String fileUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, dataItem.getUrl(), "image/jpeg");
                 if (StringUtils.isBlank(fileUrl)) {
                     log.warn("图片转存OSS失败,URL: {}", dataItem.getUrl());
@@ -137,7 +137,7 @@ public class ContentMediaSearchJob {
             Long now = System.currentTimeMillis();
             for (ContentSearchResponse.DataItem dataItem : dataItems) {
                 // todo 时长过滤
-                String fileName = String.format("video/%s_%d.mp4", content.getId(), System.currentTimeMillis());
+                String fileName = String.format("supply/video/%s_%d.mp4", content.getId(), System.currentTimeMillis());
                 String fileUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, dataItem.getUrl(), "video/mp4");
                 if (StringUtils.isBlank(fileUrl)) {
                     log.warn("视频转存OSS失败,URL: {}", dataItem.getUrl());

+ 27 - 11
core/src/main/java/com/tzld/supply/job/VideoGenerateJob.java

@@ -169,17 +169,21 @@ public class VideoGenerateJob {
         if (CollectionUtil.isNotEmpty(imageList)) {
             for (SpiderContentMedia image : imageList) {
                 // 现仅一种效果,后更改为多种效果随机
-                String command = "ffmpeg -loop 1 -i input.jpg -t 3 -r 25 -filter_complex \"[0:v]scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,gblur=sigma=5[bg];[0:v]scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=black@0[fg];[fg]zoompan=z='min(1.00+0.000266*on,1.02)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1080x1920[fg_anim];[bg][fg_anim]overlay=0:0:format=auto\" -c:v libx264 -pix_fmt yuv420p -movflags +faststart \"output.mp4\"";
-                command = command.replace("input.jpg", CdnUtil.getOssHttpUrl(image.getOssKey()));
                 String outputVideoName = String.format("%s_%d.mp4", produceVideo.getContentId(), System.currentTimeMillis());
-                command = command.replace("output.mp4", outputVideoName);
+                String command = String.format("ffmpeg -loop 1 -i %s -t 3 -r 25 -filter_complex " +
+                        "\"[0:v]scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,gblur=sigma=5[bg];" +
+                        "[0:v]scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2:color=black@0[fg];" +
+                        "[fg]zoompan=z='min(1.00+0.000266*on,1.02)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1080x1920[fg_anim];" +
+                        "[bg][fg_anim]overlay=0:0:format=auto\" " +
+                        "-c:v libx264 -pix_fmt yuv420p -movflags +faststart \"%s\"",
+                        CdnUtil.getOssHttpUrl(image.getOssKey()), outputVideoName);
                 CommandParam commandParam = CommandParam.builder()
                         .ffmpegCommand(command)
                         .outputFile(outputVideoName)
                         .outputType("video")
                         .build();
                 String commandResponseData = ffmpegApiService.command(commandParam);
-                String fileName = String.format("material/video/%s", outputVideoName);
+                String fileName = String.format("supply/material/video/%s", outputVideoName);
                 String imageToVideoOssUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, commandResponseData, "video/mp4");
 
                 // 获取视频信息
@@ -231,7 +235,7 @@ public class VideoGenerateJob {
                             .build();
                     lastEndTime = endTime;
                     String videoCutUrl = ffmpegApiService.timeCutVideo(timeCutParam);
-                    String fileName = String.format("material/video/%s_%d.mp4", produceVideo.getContentId(), System.currentTimeMillis());
+                    String fileName = String.format("supply/material/video/%s_%d.mp4", produceVideo.getContentId(), System.currentTimeMillis());
                     String videoCutOssUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, videoCutUrl, "video/mp4");
                     // 获取视频信息
                     MediaInfo mediaInfo = getMediaInfo(videoCutOssUrl);
@@ -300,6 +304,10 @@ public class VideoGenerateJob {
             if (materialList.size() < 10) {
                 continue;
             }
+            ProduceVideoAudio audio = getProduceAudio(produceVideo);
+            if (audio == null) {
+                continue;
+            }
             // short video to long video
              // 视频素材拼接长视频
             VideoUrlsParam concatParam = VideoUrlsParam.builder()
@@ -309,17 +317,15 @@ public class VideoGenerateJob {
             String concatVideoUrl = ffmpegApiService.videoConcat(concatParam);
             // add audio
             String videoName = String.format("%s_%d.mp4", produceVideo.getId(), System.currentTimeMillis());
-            String command = "ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -map 0:v:0 -map 1:a:0 -shortest -y output.mp4";
-            command = command.replace("video.mp4", concatVideoUrl);
-            command = command.replace("audio.mp3", materialList.get(0).getUrl());
-            command = command.replace("output.mp4", videoName);
+            String command = String.format("ffmpeg -i %s -i %s -c:v copy -map 0:v:0 -map 1:a:0 -shortest -y %s",
+                    concatVideoUrl, audio.getUrl(), videoName);
             CommandParam commandParam = CommandParam.builder()
                     .ffmpegCommand(command)
                     .outputFile(videoName)
                     .outputType("video")
                     .build();
             String commandResponseData = ffmpegApiService.command(commandParam);
-            String fileName = String.format("video/%s", videoName);
+            String fileName = String.format("supply/video/%s", videoName);
             String finalVideoUrl = AliOssFileTool.downloadAndSaveInOSS(fileName, commandResponseData, "video/mp4");
              // 更新视频地址
              produceVideo.setUrl(finalVideoUrl);
@@ -329,9 +335,19 @@ public class VideoGenerateJob {
         return ReturnT.SUCCESS;
     }
 
+    private ProduceVideoAudio getProduceAudio(ProduceVideo produceVideo) {
+        ProduceVideoAudioExample example = new ProduceVideoAudioExample();
+        example.createCriteria().andProduceVideoIdEqualTo(produceVideo.getId()).andStatusEqualTo(1);
+        List<ProduceVideoAudio> audioList = produceVideoAudioMapper.selectByExample(example);
+        if (CollectionUtil.isEmpty(audioList)) {
+            return null;
+        }
+        return audioList.get(0);
+    }
+
     private List<ProduceVideoMaterial> getVideoMaterialList(ProduceVideo produceVideo) {
         ProduceVideoMaterialExample example = new ProduceVideoMaterialExample();
-        example.createCriteria().andProduceVideoIdEqualTo(produceVideo.getId());
+        example.createCriteria().andProduceVideoIdEqualTo(produceVideo.getId()).andStatusEqualTo(1);
         return produceVideoMaterialMapper.selectByExample(example);
     }
 

+ 2 - 2
core/src/main/java/com/tzld/supply/util/AliOssFileTool.java

@@ -1239,7 +1239,7 @@ public class AliOssFileTool extends AliOssConfig {
             try {
                 ossInternalClient.restoreObject(bucket, srckey);
             } catch (Exception e) {
-                // TODO OSS Auto-generated catch blockvideo-common/src/main/java/com/weiqu/video/common/enums/ExceptionCodeEnum.java
+                // OSS Auto-generated catch blockvideo-common/src/main/java/com/weiqu/video/common/enums/ExceptionCodeEnum.java
 //				e.printStackTrace();
                 logger.error("视频可能在解冻中");
             }
@@ -1261,7 +1261,7 @@ public class AliOssFileTool extends AliOssConfig {
         try {
             isRestoreCompleted = objectMetadata.isRestoreCompleted();
         } catch (Exception e) {
-            // TODO OSS Auto-generated catch block
+            // OSS Auto-generated catch block
 //			e.printStackTrace();
             logger.error("视频可能在解冻中");
         }