Просмотр исходного кода

content 中添加一个 RPC 接口, 供 file-service 中的视频转码完成后调用进行后续处理

reghao 1 год назад
Родитель
Сommit
cc50ba8874

+ 1 - 0
content/content-api/src/main/java/cn/reghao/tnb/content/api/constant/VideoStatus.java

@@ -16,6 +16,7 @@ public enum VideoStatus {
     censorFailed(3, "审核未通过"),
     revoke(4, "下架"),
     cache(5, "待缓存"),
+    converted(6, "待转码"),
     coverNotFound(8, "无封面"),
     resourceNotFound(9, "无资源");
 

+ 21 - 0
content/content-api/src/main/java/cn/reghao/tnb/content/api/dto/VideoConvertedDto.java

@@ -0,0 +1,21 @@
+package cn.reghao.tnb.content.api.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+import java.io.Serializable;
+
+/**
+ * @author reghao
+ * @date 2024-11-25 09:38:03
+ */
+@AllArgsConstructor
+@Getter
+public class VideoConvertedDto implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private String videoFileId;
+    private int channelCode;
+    private String uploadId;
+    private String format;
+}

+ 2 - 4
content/content-api/src/main/java/cn/reghao/tnb/content/api/iface/AdminVideoService.java

@@ -1,10 +1,7 @@
 package cn.reghao.tnb.content.api.iface;
 
 import cn.reghao.jutil.jdk.db.PageList;
-import cn.reghao.tnb.content.api.dto.UserVideoPost;
-import cn.reghao.tnb.content.api.dto.VideoRegion;
-import cn.reghao.tnb.content.api.dto.VideoSearch;
-import cn.reghao.tnb.content.api.dto.VideoUrls;
+import cn.reghao.tnb.content.api.dto.*;
 
 import java.util.List;
 
@@ -17,4 +14,5 @@ public interface AdminVideoService {
     PageList<UserVideoPost> getVideoPosts(VideoSearch videoSearch);
     VideoUrls getVideoUrl(String videoId);
     Long getImageAlbum(String objectId);
+    void addConvertedVideo(VideoConvertedDto videoConvertedDto);
 }

+ 2 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/db/mapper/VideoPostMapper.java

@@ -29,6 +29,7 @@ public interface VideoPostMapper extends BaseMapper<VideoPost> {
     void updateVideoCover(String videoId, String coverUrl);
     void updateVideoFile(@Param("videoId") String videoId, @Param("videoInfo") VideoInfo videoInfo);
     void updateVideoStatus(String videoId, int status);
+    void updateVideoPublish(String videoFileId, int status);
     void updateVideoCached(@Param("videoId") String videoId,
                            @Param("videoInfo") VideoInfo videoInfo,
                            @Param("channelId") int channelId);
@@ -44,6 +45,7 @@ public interface VideoPostMapper extends BaseMapper<VideoPost> {
     List<VideoPostCard> findByNextVideos(VideoQuery videoQuery, LocalDateTime publishAt);
 
     VideoPost findByVideoId(String videoId);
+    VideoPost findByVideoFileId(String videoFileId);
     VideoDetail findVideoPostDetail(VideoQuery videoQuery);
 
     List<VideoPostCard> findVideoCardByVideoIds(List<String> videoIds);

+ 11 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/db/repository/VideoRepository.java

@@ -1,5 +1,6 @@
 package cn.reghao.tnb.content.app.vod.db.repository;
 
+import cn.reghao.tnb.content.api.constant.VideoStatus;
 import cn.reghao.tnb.content.app.vod.db.mapper.*;
 import cn.reghao.tnb.common.db.GroupCount;
 import cn.reghao.tnb.content.app.vod.model.po.VideoFile;
@@ -49,6 +50,12 @@ public class VideoRepository {
         videoPostMapper.updateVideoCached(videoId, videoInfo, channelId);
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    public void updateVideoPublish(String videoFileId, VideoFile videoFile) {
+        videoFileMapper.save(videoFile);
+        videoPostMapper.updateVideoPublish(videoFileId, VideoStatus.publish.getValue());
+    }
+
     public List<String> deleteVideoFile(String videoFileId) {
         List<String> objectIds = videoFileMapper.findByVideoFileId(videoFileId).stream()
                 .map(VideoFile::getObjectId)
@@ -88,4 +95,8 @@ public class VideoRepository {
     public VideoPost getVideoPost(String videoId) {
         return videoPostMapper.findByVideoId(videoId);
     }
+
+    public VideoPost getVideoPostByFileId(String videoFileId) {
+        return videoPostMapper.findByVideoFileId(videoFileId);
+    }
 }

+ 17 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/model/po/VideoFile.java

@@ -49,4 +49,21 @@ public class VideoFile extends BaseObject<Integer> {
         this.horizontal = videoInfo.getWidth()>videoInfo.getHeight();
         this.duration = videoInfo.getDuration();
     }
+
+    public VideoFile(String videoFileId, VideoInfo videoInfo) {
+        this.videoFileId = videoFileId;
+        this.objectId = videoInfo.getObjectId();
+        this.videoCodec = videoInfo.getVideoCodec();
+        this.vbitRate = videoInfo.getVbitRate();
+        this.audioCodec = videoInfo.getAudioCodec();
+        this.abitRate = videoInfo.getAbitRate();
+        this.formatName = videoInfo.getFormatName();
+        this.urlType = videoInfo.getUrlType();
+        this.url = videoInfo.getUrl();
+        this.quality = videoInfo.getQuality();
+        this.width = videoInfo.getWidth();
+        this.height = videoInfo.getHeight();
+        this.horizontal = videoInfo.getWidth()>videoInfo.getHeight();
+        this.duration = videoInfo.getDuration();
+    }
 }

+ 29 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/rpc/AdminVideoServiceImpl.java

@@ -4,6 +4,8 @@ import cn.reghao.file.api.iface.OssService;
 import cn.reghao.jutil.jdk.db.Page;
 import cn.reghao.jutil.jdk.db.PageList;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
+import cn.reghao.oss.sdk.model.constant.ObjectScope;
+import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
 import cn.reghao.oss.sdk.model.dto.media.VideoUrlDto;
 import cn.reghao.tnb.content.api.constant.PostScope;
 import cn.reghao.tnb.content.api.dto.*;
@@ -11,6 +13,7 @@ import cn.reghao.tnb.content.api.iface.AdminVideoService;
 import cn.reghao.tnb.content.app.vod.db.mapper.VideoPostMapper;
 import cn.reghao.tnb.content.app.vod.db.repository.VideoRepository;
 import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
+import cn.reghao.tnb.content.app.vod.model.po.VideoFile;
 import cn.reghao.tnb.content.app.vod.model.po.VideoPost;
 import cn.reghao.tnb.content.app.vod.model.query.VideoQuery;
 import cn.reghao.tnb.content.app.vod.service.CategoryService;
@@ -163,4 +166,30 @@ public class AdminVideoServiceImpl implements AdminVideoService {
     public Long getImageAlbum(String objectId) {
         return null;
     }
+
+    @Override
+    public void addConvertedVideo(VideoConvertedDto videoConvertedDto) {
+        String uploadId = videoConvertedDto.getUploadId();
+        int channelCode = videoConvertedDto.getChannelCode();
+        try {
+            VideoInfo videoInfo = ossService.getVideoInfo(channelCode, uploadId);
+            if (videoInfo == null) {
+                String errMsg = String.format("视频文件 %s 在 oss 中不存在", uploadId);
+                log.info(errMsg);
+                return;
+            }
+
+            String videoFileId = videoConvertedDto.getVideoFileId();
+            VideoPost videoPost = videoRepository.getVideoPostByFileId(videoFileId);
+            int scope = videoPost.getScope();
+            if (scope != ObjectScope.PRIVATE.getCode()) {
+                ossService.setObjectScope(channelCode, uploadId, scope);
+            }
+
+            VideoFile videoFile = new VideoFile(videoFileId, videoInfo);
+            videoRepository.updateVideoPublish(videoFileId, videoFile);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 }

+ 20 - 5
content/content-service/src/main/java/cn/reghao/tnb/content/app/vod/service/impl/VideoPostServiceImpl.java

@@ -1,12 +1,15 @@
 package cn.reghao.tnb.content.app.vod.service.impl;
 
+import cn.reghao.file.api.iface.FileService;
 import cn.reghao.file.api.iface.OssService;
 import cn.reghao.jutil.jdk.result.Result;
+import cn.reghao.jutil.jdk.result.ResultStatus;
 import cn.reghao.jutil.tool.id.IdGenerator;
 import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
 import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
 import cn.reghao.tnb.common.auth.UserContext;
 import cn.reghao.tnb.content.api.constant.PostScope;
+import cn.reghao.tnb.content.api.constant.VideoStatus;
 import cn.reghao.tnb.content.api.dto.VideoFileUpdate;
 import cn.reghao.tnb.content.api.dto.VideoPublishSbt;
 import cn.reghao.tnb.content.app.vod.db.mapper.*;
@@ -33,6 +36,8 @@ import java.util.*;
 public class VideoPostServiceImpl implements VideoPostService {
     @DubboReference(check = false)
     private OssService ossService;
+    @DubboReference(check = false)
+    private FileService fileService;
 
     private int pageSize = 12;
     private final VideoPostMapper videoPostMapper;
@@ -66,8 +71,8 @@ public class VideoPostServiceImpl implements VideoPostService {
 
             String coverUrl = imageInfo.getUrl();
             String videoFileId = videoPublishSbt.getVideoFileId();
-            int channelId = videoPublishSbt.getChannelCode();
-            VideoInfo videoInfo = ossService.getVideoInfo(channelId, videoFileId);
+            int channelCode = videoPublishSbt.getChannelCode();
+            VideoInfo videoInfo = ossService.getVideoInfo(channelCode, videoFileId);
             if (videoInfo == null) {
                 String errMsg = String.format("视频文件 %s 在 oss 中不存在", videoFileId);
                 return Result.fail(errMsg);
@@ -81,21 +86,31 @@ public class VideoPostServiceImpl implements VideoPostService {
             }
             String codec = String.format("%s&%s", videoCodec, audioCodec);
 
-            Integer channelScope = ossService.getChannelScope(channelId);
+            Integer channelScope = ossService.getChannelScope(channelCode);
             if (channelScope == null ) {
-                String errMsg = String.format("channelId %s 的 scope 在 oss 中不存在", channelId);
+                String errMsg = String.format("channelCode %s 的 scope 在 oss 中不存在", channelCode);
                 return Result.fail(errMsg);
             }
 
             int scope = videoPublishSbt.getScope();
             if (channelScope != scope) {
-                ossService.setObjectScope(channelId, videoFileId, scope);
+                ossService.setObjectScope(channelCode, videoFileId, scope);
             }
 
             String videoId = idGenerator.getUuid();
             Set<String> tags = videoPublishSbt.getTags();
             String tags1 = tags.toString().replace("[", "").replace("]", "");
             VideoPost videoPost = new VideoPost(videoId, videoPublishSbt, coverUrl, videoFile, codec);
+
+            if (!videoInfo.getFormatName().contains("mov,mp4")) {
+                Result result1 = fileService.convertVideo(videoFileId, channelCode);
+                if (result1.getCode() == ResultStatus.SUCCESS.getCode()) {
+                    videoPost.setStatus(VideoStatus.converted.getValue());
+                } else {
+                    log.error(result1.getMsg());
+                }
+            }
+
             saveVideo(videoFile, videoPost, tags1);
             result = Result.success();
         } catch (Exception e) {

+ 10 - 0
content/content-service/src/main/resources/mapper/vod/VideoPostMapper.xml

@@ -34,6 +34,11 @@
         set update_time=now(),`status`=#{status}
         where video_id=#{videoId}
     </update>
+    <update id="updateVideoPublish">
+        update video_post
+        set update_time=now(),`status`=#{status}
+        where video_file_id=#{videoFileId}
+    </update>
     <update id="updateVideoCached">
         update video_post
         set update_time=now(),video_file_id=#{videoInfo.videoFileId},duration=#{videoInfo.duration},quality=#{videoInfo.quality},channel_id=#{channelId},`status`=2
@@ -384,6 +389,11 @@
         from video_post
         where deleted=0 and video_id=#{videoId}
     </select>
+    <select id="findByVideoFileId" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
+        select *
+        from video_post
+        where deleted=0 and video_file_id=#{videoFileId}
+    </select>
     <select id="findAll" resultType="cn.reghao.tnb.content.app.vod.model.po.VideoPost">
         select *
         from video_post