Kaynağa Gözat

完成基础功能开发

liyuntian 10 ay önce
ebeveyn
işleme
42fef645de

+ 2 - 2
mcp/src/main/java/com/chuanxia/mcp/sched/M3U8Utils.java

@@ -28,7 +28,6 @@ import java.util.regex.Pattern;
 public class M3U8Utils {
 
     private final RestTemplate restTemplate;
-    private final VideoM3u8Service m3u8Service;
 
     @Value("${m3u8.downLoadPath}")
     private String downLoadPath;
@@ -64,7 +63,6 @@ public class M3U8Utils {
         if (m3u8Arr.length > 0) {
             //判断当前m3U8是单码文件还是多码文件
             String endData = m3u8Arr[m3u8Arr.length - 1];
-            log.info("当前m3u8最后结尾内容:{}", endData);
             if (endData.endsWith("#EXT-X-ENDLIST")) {
                 //当前m3u8文件是单码文件,直接解析,入库
                 VideoM3u8 videoM3u8 = analysisM3u8(url + uri + fileName, m3u8Str, resolutionRatio, bandwidth, videoId, url, uri);
@@ -221,6 +219,7 @@ public class M3U8Utils {
                     m3u8.setEXTINF(orderVo.getExtinf());
                     m3u8.setTS_LAST_TIME(orderVo.getTsLastTime());
                     m3u8.setTS_NAME(orderVo.getTsName());
+                    m3u8.setTsListStr(JSON.toJSONString(orderVo.getVideoTs()));
                 } else {
                     //不是自然排序,设置arr
                     m3u8.setTsListStr(JSON.toJSONString(orderVo.getVideoTs()));
@@ -344,6 +343,7 @@ public class M3U8Utils {
                 orderVo.setTsStart(firstNum);
                 orderVo.setTsEnd(lastNum);
                 orderVo.setTsName(tsName);
+                orderVo.setVideoTs(tsList);
             } else {
                 orderVo.setOrder(false);
                 orderVo.setVideoTs(tsList);

+ 54 - 3
mcp/src/main/java/com/chuanxia/mcp/service/impl/CdnDoaminServiceImpl.java

@@ -29,8 +29,10 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.*;
 
@@ -206,13 +208,18 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
                     m3u8 = videoM3u8s.get(0);
                 }
                 if (!StringUtils.isBlank(type)) {
-                    String downLoadFileUrl = "http://192.168.1.134:8088/api?cdn=" + cdnDomain.getDomain() + "&id=" + m3u8.getId();
+                    String downLoadFileUrl = "http://127.0.0.1:8088/api/cdnDomain/downLoadM3u8File?cdn=" + cdnDomain.getDomain() + "&id=" + m3u8.getId();
                     throw new ResultException(888, downLoadFileUrl);
                 }
                 String tsListStr = m3u8.getTsListStr();
+                String tsName = m3u8.getTS_NAME();
+
                 if (!StringUtils.isBlank(tsListStr)) {
-                    List<VideoTs> tsList = JSON.parseArray(tsListStr, VideoTs.class);
-                    m3u8.setTS_LIST(tsList);
+                    if (StringUtils.isBlank(tsName)) {
+                        List<VideoTs> tsList = JSON.parseArray(tsListStr, VideoTs.class);
+                        //说明是有序的视频,则不需要返回tsList
+                        m3u8.setTS_LIST(tsList);
+                    }
                     String prefixPath = m3u8.getPREFIX_PATH();
                     if (StringUtils.isNotBlank(prefixPath)) {
                         m3u8.setPREFIX_PATH(cdnDomain.getDomain() + m3u8.getPREFIX_PATH());
@@ -233,6 +240,50 @@ public class CdnDoaminServiceImpl extends ServiceImpl<CdnDomainMapper, CdnDomain
 
     @Override
     public Result downLoadM3u8File(String cdn, Integer id, HttpServletResponse response) {
+        VideoM3u8 m3u8 = m3u8Mapper.selectById(id);
+        if (m3u8 == null) {
+            throw new ResultException(500, "当前视频不存在");
+        }
+        response.setContentType("text/plain");
+        response.setHeader("Content-Disposition", "attachment;filename=" + id + ".m3u8");
+        ServletOutputStream out = null;
+        try {
+            out = response.getOutputStream();
+            StringBuilder builder = new StringBuilder();
+            String last = "\n";
+            builder.append("#EXTM3U").append(last);
+            builder.append("#EXT-X-VERSION:").append(m3u8.getVERSION()).append(last);
+            builder.append("#EXT-X-TARGETDURATION:").append(m3u8.getTARGETDURATION()).append(last);
+            builder.append("#EXT-X-MEDIA-SEQUENCE:").append(m3u8.getSEQUENCE()).append(last);
+            builder.append("#EXT-X-PLAYLIST-TYPE:").append(m3u8.getTYPE()).append(last);
+            if (StringUtils.isNotBlank(m3u8.getKEY())) {
+                builder.append("#EXT-X-KEY:METHOD=AES-128,URI=\"data:application/octet-stream;base64,").append(m3u8.getKEY()).append("\"").
+                        append(",IV=").append(m3u8.getIV()).append(last);
+            }
+            String tsListStr = m3u8.getTsListStr();
+            if (StringUtils.isNotBlank(tsListStr)) {
+                List<VideoTs> tsList = JSON.parseArray(tsListStr, VideoTs.class);
+                String prefixPath = m3u8.getPREFIX_PATH();
+                boolean notBlank = StringUtils.isNotBlank(prefixPath);
+
+                for (VideoTs videoTs : tsList) {
+                    String name = videoTs.getName();
+                    builder.append("#EXTINF:").append(videoTs.getTime()).append(",").append(last);
+                    if (notBlank) {
+                        //为true表示不为空,说明ts地址是相对路径,这里拼接cdn地址
+                        builder.append(cdn).append(prefixPath).append(name).append(last);
+                    } else {
+                        builder.append(name).append(last);
+                    }
+                }
+            }
+            builder.append("#EXT-X-ENDLIST").append(last);
+            out.write(builder.toString().getBytes());
+            out.flush();
+            out.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
         return null;
     }