chanjunkai 5 năm trước cách đây
mục cha
commit
a76856fca2
2 tập tin đã thay đổi với 83 bổ sung9 xóa
  1. 65 6
      Controller/CloudStorage.py
  2. 18 3
      Object/m3u8generate.py

+ 65 - 6
Controller/CloudStorage.py

@@ -35,6 +35,8 @@ from Object.UidTokenObject import UidTokenObject
 from Service.CommonService import CommonService
 from Service.ModelService import ModelService
 from Object.ETkObject import ETkObject
+from boto3.session import Session
+import boto3
 
 SERVER_DOMAIN = 'http://cloudstroage.dvema.com/'
 
@@ -230,15 +232,72 @@ class CloudStorageView(View):
                                     content_type="application/json,charset=utf-8")
 
     def do_query_vod_list(self, request_dict, userID, response):
-        startTime = int(request_dict.get('startTime',None))
-        endTime = int(request_dict.get('endTime',None))
-        uid =request_dict.get('uid',None)
-        channel = request_dict.get('channel',None)
-        dv_qs = Device_Info.objects.filter(UID=uid,userID_id=userID,isShare=False)
+        startTime = int(request_dict.get('startTime', None))
+        endTime = int(request_dict.get('endTime', None))
+        uid = request_dict.get('uid', None)
+        channel = request_dict.get('channel', None)
+        dv_qs = Device_Info.objects.filter(UID=uid, userID_id=userID, isShare=False)
         if not dv_qs.exists():
             return response.json(12)
         now_time = int(time.time())
-        vh_qs = VodHlsModel.objects.filter(uid=uid,channel=channel,time__range=(startTime,endTime))
+        vh_qs = VodHlsModel.objects.filter(uid=uid, channel=channel, time__range=(startTime, endTime),
+                                           endTime__gte=now_time). \
+            values("time", "sec", "bucket__bucket", "bucket__endpoint", "bucket__region", "bucket__mold")
+        vod_play_list = []
+        auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
+        for vod in vh_qs:
+            bucket__mold = vod["bucket__mold"]
+            bucket_name = vod["bucket__bucket"]
+            endpoint = vod["bucket__endpoint"]
+            bucket__region = vod["bucket__region"]
+            if bucket__mold == 0:
+                bucket = oss2.Bucket(auth, endpoint, bucket_name)
+                m3u8 = '{uid}/vod{channel}/{time}/{time}.m3u8'. \
+                    format(uid=uid, channel=channel, time=vod['time'])
+                ts = '{uid}/vod{channel}/{time}/ts0.ts'. \
+                    format(uid=uid, channel=channel, time=vod['time'])
+                url = bucket.sign_url('GET', m3u8, 3600, params={'x-oss-process': 'hls/sign'})
+                urllst = url.split('?')
+                url_start = urllib.parse.unquote(urllst[0])
+                url_end = urllst[1]
+                vod_play_url = '{url_start}?{url_end}'. \
+                    format(url_start=url_start, url_end=url_end)
+                thumb = bucket.sign_url('GET', ts, 3600,
+                                        params={'x-oss-process': 'video/snapshot,t_10000,m_fast,w_300'})
+                vod_play_list.append({'name': vod['time'], 'sign_url': vod_play_url, 'thumb': thumb, 'sec': vod['sec']})
+            elif bucket__mold == 1:
+                aws_access_key_id = 'AKIA2E67UIMD45Y3HL53'
+                aws_secret_access_key = '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'
+                session = Session(
+                    aws_access_key_id=aws_access_key_id,
+                    aws_secret_access_key=aws_secret_access_key,
+                    region_name=bucket__region
+                )
+                conn = session.client('s3')
+                thumbspng = '{uid}/vod{channel}/{time}/thumbs.png'. \
+                    format(uid=uid, channel=channel, time=vod['time'])
+                response_url = conn.generate_presigned_url(
+                    'get_object',
+                    Params={
+                        'Bucket': bucket_name,
+                        'Key': thumbspng
+                    },
+                    ExpiresIn=3600
+                )
+                # m3u8 = '{uid}/vod{channel}/{time}/{time}.m3u8'. \
+                #     format(uid=uid, channel=channel, time=vod['time'])
+                thumb_url = response_url
+                vod_url = 'http://cloudstroage.dvema.com/getsignurl/' \
+                          'play.m3u8?uid={uid}&channel={channel}&time={time}'. \
+                    format(uid=uid, channel=channel, time=time)
+                vod_play_list.append({
+                    'name': vod['time'],
+                    'sign_url': vod_url,
+                    'thumb': thumb_url,
+                    'sec': vod['sec']})
+        return response.json(0, vod_play_list)
+
+
 
     def do_store_playlist(self, request_dict, response):
         uidToken = request_dict.get('uidToken', None)

+ 18 - 3
Object/m3u8generate.py

@@ -67,6 +67,21 @@ class PlaylistGenerator(object):
         return duration_total
 
     def generate(self):
-        """ This is a proxy for _generate makes it
-        difficult to edit the real method for future."""
-        return self._generate()
+        """
+        This is a proxy for _generate makes it
+        difficult to edit the real method for future.
+        """
+        return self._generate()
+
+
+playlist_entries = [
+    {
+        'name':  "Awesomevideo_001.mp4",
+        'duration': '10.04',
+    }
+
+]
+playlist = PlaylistGenerator(playlist_entries).generate()
+
+print(playlist)
+# from m3u8_generator import PlaylistGenerator