派生自 projectDept/qhighschool

胡仁荣
2022-11-03 68408ba6f0ff9c4e7978735818ab12990c957c1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.qxueyou.scc.base;
 
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.alibaba.fastjson.JSONObject;
import com.qxueyou.scc.base.dao.CommonDAO;
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.media.model.MediaVideo;
import com.qxueyou.scc.sys.service.IOssService;
 
/**
 * aliyun×ÊÔ´ÁÙʱÊÚȨ
 * @author cyq
 *
 */
@Controller
@RequestMapping(value = "/sts")
public class StsCtrl {
    @SuppressWarnings("unused")
    private static final String M3U8 = "m3u8";
 
    private static final String MP4 = "mp4";
    
    @SuppressWarnings("unused")
    private static final String AES_VIDEO_KEY = "u9WH6ldpMd29cJ0Z";
 
    private final Logger log =  LogManager.getLogger(StsCtrl.class);
    
    @Autowired
    IOssService ossService;
    
    /**
     * ¹«¹²Êý¾Ý·ÃÎʶÔÏó
     */
    @Autowired
    private CommonDAO commonDAO;
    
    /**
     * »ñÈ¡ÊÓÆµµÄm3u8·ÃÎÊȨÏÞ£¬ÓûñÈ¡ÁÙʱ·ÃÎÊȨÏÞµÄkey·¾¶×÷Ϊ½âÃÜÃÜÔ¿£¬ÁÙʱÉú³Ém3u8Îļþ£¬²¢ÉÏ´«µ½°¢ÀïÔÆ£¬²¢·µ»ØÂ·¾¶
     * @param videoId
     * @param definition
     * @return
     */
    @RequestMapping(value="video/m3u8", method=RequestMethod.GET, produces = "application/json;charset=utf-8")
    public @ResponseBody Result videoM3u8(@RequestParam("videoId") String videoId, String definition) {
        if (StringUtils.isEmpty(videoId)) {
            return new Result(false, "²ÎÊý´íÎó");
        }
        
//        json.put("result", "success");
//        // ¼ì²é»º´æÖÐÊÇ·ñÓпÉÓõÄurl£¬Èç¹ûÓУ¬ÔòÖ±½Ó·µ»Ø
//        String urlCache = cacheService.get("sts_url_" + resId, String.class);
//        if (StringUtils.isNotEmpty(urlCache)) {
//            json.put("url", urlCache);
        
        
        BufferedReader bf = null;
        ByteArrayOutputStream byteArrOS =null;
        try {
            String def = definition;
            MediaVideo video = commonDAO.read(MediaVideo.class, videoId);
            
            if (video == null) {
                return new Result(false, "×ÊÔ´²»´æÔÚ");
            }
            
            String resId = "";
            String resUrl = null;
            JSONObject json = null;
            
            //ÅжÏÊÇ·ñÊÇmp3µÈÒôÀÖ¸ñʽÎļþ£¬Èç¹ûÊÇÔòÖ±½Ó·µ»ØoriginalµØÖ·
            if(checkIsVoiceMedia(video)){
                json =  new JSONObject();
                json.put("result", "success");
                json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
                json.put("isConver",false);
                return new Result(true, "", json);
            }
            
            resUrl = this.getM3u8ResUrl(def, video);
            
            //ÅжÏÊÇ·ñÊǼÓÃÜÊÓÆµ£¬Î´¼ÓÃÜÔòÖ±½Ó·µ»ØÊÓÆµµØÖ·
            if(StringUtils.isEmpty(video.getSecretKey())){
                //Èç¹ûδתÂë¼ÓÃÜ£¬ÇÒm3u8ÊÓÆµ²»´æÔÚ£¬Ôò³¢ÊÔ¶ÁÈ¡MP4ÊÓÆµ¡£
                if (StringUtils.isEmpty(resUrl)) {
                    resUrl = StringUtils.isNotEmpty(video.getAndroidHD()) ? video.getAndroidHD() : StringUtils.isNotEmpty(video.getAndroidSD()) ? video.getAndroidSD() : video.getAndroidLD();
                    
                    if (StringUtils.isEmpty(resUrl)) {
                        return new Result(false, "×ÊÔ´²»´æÔÚ");
                    }
                }
                
                json =  new JSONObject();
                json.put("result", "success");
                json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
                json.put("isConver",false);
            }else{
                //Èç¹ûתÂë¼ÓÃܺó£¬ÒÀÈ»²»ÄܶÁÈ¡µ½M3U8ÊÓÆµ»ò¶ÁÈ¡µ½µÄÊÇMP4Ö±½Ó·µ»Ø£¬ÌáʾÊÓÆÁ²»´æÔÚ
                if (StringUtils.isEmpty(resUrl)||resUrl.endsWith(MP4)) {
                    return new Result(false, "×ÊÔ´²»´æÔÚ");
                }
                
                resId = resUrl.substring(resUrl.indexOf("/video/") + 1);
                
                json = new JSONObject();
                json.put("result", "success");
                json.put("url", resId);
                json.put("isConver",true);
                
                
//                String resDir = resId.substring(0, resId.lastIndexOf('/'));
                
                //»ñȡԭÊÓÆµVideoId
//                String keyAddr = video.getOriginVideoId();
//                if(StringUtils.isBlank(keyAddr)){
//                    keyAddr = video.getVideoId();
//                }
                
//                // »ñÈ¡½âÃÜkeyµÄÁÙʱ·ÃÎÊȨÏÞ
//                if(StringUtils.isNotEmpty(video.getM3u8KeyPath())){
//                    json.put("url", video.getM3u8KeyPath().substring(video.getM3u8KeyPath().indexOf("/video/") + 1));
//                }else{
//                    String  m3u8KeyPath = resDir + "/" + keyAddr + ".key";
//                }
                
            
                
//                String m3u8ResKey = resDir + "/" + uuid + ".m3u8";
                
//                InputStream in =  ossService.getObject(resId);
//                String temp=null;
//                byteArrOS=new ByteArrayOutputStream();
//                bf = new BufferedReader(new InputStreamReader(in));
//                
//                while ((temp = bf.readLine()) != null) {
//                    if (temp.startsWith("#EXT-X-KEY")) {
//                        temp = temp.replace("_x.key", ".key");
//                    }
//                    temp= temp.concat("\n");
//                    byteArrOS.write(temp.getBytes());
//                }
                
                
            }
            return new Result(true, "", json);
        } catch (Exception e) {
            log.error(e, e);
            return new Result(false);
        }finally{
            IOUtils.closeQuietly(bf);
            IOUtils.closeQuietly(byteArrOS);
        }
    }
    
    
    /**
     * »ñÈ¡ÊÓÆµµÄm3u8·ÃÎÊȨÏÞ£¬ÓûñÈ¡ÁÙʱ·ÃÎÊȨÏÞµÄkey·¾¶×÷Ϊ½âÃÜÃÜÔ¿£¬ÁÙʱÉú³Ém3u8Îļþ£¬²¢ÉÏ´«µ½°¢ÀïÔÆ£¬²¢·µ»ØÂ·¾¶
     * @param videoId
     * @param definition
     * @return
     */
    @SuppressWarnings("unused")
    @RequestMapping(value="video/mp4", method=RequestMethod.GET, produces = "application/json;charset=utf-8")
    public @ResponseBody Result videoMp4(@RequestParam("videoId") String videoId, String definition) {
        if (StringUtils.isEmpty(videoId)) {
            return new Result(false, "²ÎÊý´íÎó");
        }        
        
        BufferedReader bf = null;
        ByteArrayOutputStream byteArrOS =null;
        try {
            String def = definition;
            MediaVideo video = commonDAO.read(MediaVideo.class, videoId);
            
            if (video == null) {
                return new Result(false, "×ÊÔ´²»´æÔÚ");
            }
            
            String resId = "";
            String resUrl = null;
            JSONObject json = null;
            
            //ÅжÏÊÇ·ñÊÇmp3µÈÒôÀÖ¸ñʽÎļþ£¬Èç¹ûÊÇÔòÖ±½Ó·µ»ØoriginalµØÖ·
            if(checkIsVoiceMedia(video)){
                json =  new JSONObject();
                json.put("result", "success");
                json.put("url", video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1));
                json.put("isConver",false);
                return new Result(true, "", json);
            }
            
            resUrl = this.getMp4ResUrl(def, video);
            
            //Èç¹ûתÂëµØÖ·²»´æÔÚ£¬²¢ÇÒÔ´ÂëµØÖ·ÊÇmp4ÔòÖ±½ÓʹÓÃÔ­ÊÓÆµµØÖ·²¥·Å
            if(StringUtils.isEmpty(resUrl) && video.getOrigUrl().endsWith(MP4)){
                    resUrl =  video.getOrigUrl().substring(video.getOrigUrl().indexOf("/video/") + 1);
            }
            
            //²é¿´Ô­ÊÓÆµÊÇ·ñÊÇMP4
            if (StringUtils.isEmpty(resUrl)) {
                return new Result(false, "×ÊÔ´²»´æÔÚ");
            }
            
            json =  new JSONObject();
            json.put("result", "success");
            json.put("url", resUrl.substring(resUrl.indexOf("/video/") + 1));
            json.put("isConver",false);
            
            return new Result(true, "", json);
        } catch (Exception e) {
            log.error(e, e);
            return new Result(false);
        }finally{
            IOUtils.closeQuietly(bf);
            IOUtils.closeQuietly(byteArrOS);
        }
    }
    
    
    /**
     * »ñÈ¡resurl
     * 
     * @param def
     * @param video
     * @return
     */
    private String getM3u8ResUrl(String def, MediaVideo video){
        return StringUtils.isNotEmpty(video.getIosHD()) ? video.getIosHD() : StringUtils.isNotEmpty(video.getIosSD()) ? video.getIosSD() : video.getIosLD();
    }
    
    /**
     * »ñÈ¡resurl
     * 
     * @param def
     * @param video
     * @return
     */
    private String getMp4ResUrl(String def, MediaVideo video){
        return StringUtils.isNotEmpty(video.getAndroidHD()) ? video.getAndroidHD() : StringUtils.isNotEmpty(video.getAndroidSD()) ? video.getAndroidSD(): video.getAndroidLD();
    }
    
    /**
     * ÅжÏÊÇ·ñÊÇÒôƵmedia,¾«È·ÅжÏÐèҪö¾ÙÒôƵ¸ñʽ
     * @return
     */
    private  boolean checkIsVoiceMedia(MediaVideo media){
        if(media==null || StringUtils.isEmpty(media.getOrigUrl())){
            return false;
        }
        
        return !(StringUtils.isNotEmpty(media.getAndroidHD())
                ||StringUtils.isNotEmpty(media.getAndroidSD())
                ||StringUtils.isNotEmpty(media.getAndroidLD())
                ||StringUtils.isNotEmpty(media.getIosHD())
                ||StringUtils.isNotEmpty(media.getIosSD())
                ||StringUtils.isNotEmpty(media.getIosLD()));
    }
    
    /**
     * ²âÊÔ½Ó¿ÚרÓÃ
     * @param request
     * @return
     */
    @RequestMapping(value="test", method=RequestMethod.GET)
    public @ResponseBody String test(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }    
}