Browse Source

add counting, mail, line notify features

aaron-1015 3 years ago
parent
commit
bbc988db50
1 changed files with 93 additions and 8 deletions
  1. 93 8
      OpenshotService/openshot_video_generator.py

+ 93 - 8
OpenshotService/openshot_video_generator.py

@@ -19,6 +19,8 @@ import math
 import dataset
 from datetime import datetime
 from gtts import gTTS
+import cv2
+from mail import mail_to_users
 
 dir_sound = 'mp3_track/'
 dir_photo = 'photo/'
@@ -32,15 +34,33 @@ video_sub_folder = 'ai_anchor_video/'
 
 dir_list = [dir_sound,dir_photo,dir_text,dir_video,dir_title,dir_subtitle,dir_anchor,tmp_video_dir]
 
-def notify_group(msg):
-    glist=['7vilzohcyQMPLfAMRloUawiTV4vtusZhxv8Czo7AJX8','WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD','1dbtJHbWVbrooXmQqc4r8OyRWDryjD4TMJ6DiDsdgsX','HOB1kVNgIb81tTB4Ort1BfhVp9GFo6NlToMQg88vEhh']
+def notify_group(msg, glist=['7vilzohcyQMPLfAMRloUawiTV4vtusZhxv8Czo7AJX8','WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD','1dbtJHbWVbrooXmQqc4r8OyRWDryjD4TMJ6DiDsdgsX','HOB1kVNgIb81tTB4Ort1BfhVp9GFo6NlToMQg88vEhh']):
     for gid in glist:
         headers = {
                 "Authorization": "Bearer " + gid,
                 "Content-Type": "application/x-www-form-urlencoded"
         }
-        params = {"message": msg}   
-        r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
+    params = {"message": msg}   
+    r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
+
+
+def get_mp4_duration(video_name='mp4-test-file.mp4'): # 可輸入url
+    cap = cv2.VideoCapture(video_name)
+    # 幀率
+    fps = int(round(cap.get(cv2.CAP_PROP_FPS)))
+    # 分辨率-寬度
+    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
+    # 分辨率-高度
+    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
+    # 總幀數
+    frame_counter = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
+
+    cap.release()
+    cv2.destroyAllWindows()
+    # 時長,單位:秒(s)
+    duration = int(frame_counter / fps)
+    return duration
+
 
 def cKey(r,g,b,fuzz):
     col=openshot.Color()
@@ -253,6 +273,7 @@ def generate_subtitle_image_ENG(name_hash,text_content):
     return img_list
 
 def anchor_video_v2(name_hash,name,text_content, image_urls,avatar):
+    """ 影片產生主程式。 """
     print(os.getcwd())
     print('sub image made')
     file_prepare(name, name_hash, text_content,image_urls,0)
@@ -389,8 +410,23 @@ def anchor_video_v2(name_hash,name,text_content, image_urls,avatar):
     for n in range(frames):
         f=t.GetFrame(n)
         w.WriteFrame(f)
-        
-    notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
+    
+    # 更新剩下時間、duration
+    video_link = f"www.choozmo.com:8168/{video_sub_folder}{name_hash}.mp4"
+    duration, user_id = update_hisotry_duration(video_link)
+    is_left_time = update_user_left_time(user_id, duration)
+
+    # 1.餘額足 2. 餘額不足
+    if is_left_time:
+        notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
+        contents = f"""  影片下載網址: {video_link}"""
+        mail_to_users(user_id, f'您好,您的影片 "{name}" 已經完成', contents=contents)
+    else:
+        notify_group(msg="您的餘額不足,請去...儲值,才能取得影片唷!")
+        contents = f"""  餘額不足,請往: ...儲值url儲值 """
+        mail_to_users(user_id, f'您好,您的餘額不足', contents)
+    
+    
     t.Close()
     w.Close()
     print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
@@ -528,8 +564,23 @@ def anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
     for n in range(frames):
         f=t.GetFrame(n)
         w.WriteFrame(f)
-        
-    notify_group(name+"(ENG)的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
+    
+
+    # 更新剩下時間、duration
+    video_link = f"www.choozmo.com:8168/{video_sub_folder}{name_hash}.mp4"
+    duration, user_id = update_hisotry_duration(video_link)
+    is_left_time = update_user_left_time(user_id, duration)
+
+    # 1.餘額足 2. 餘額不足
+    if is_left_time:
+        notify_group(name+", Your video is complete! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
+        contents = f"""  The download address: {video_link}"""
+        mail_to_users(user_id, f'Hi, your video "{name}" is complete', contents=contents)
+    else:
+        notify_group(msg="The left money is not enough, please deposit to get the video!")
+        contents = f"""  The left money is not enough, please go to ... to deposit. """
+        mail_to_users(user_id, f'Hi, your remain deposit is not enough', contents)
+
     t.Close()
     w.Close()
     print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4")
@@ -543,6 +594,40 @@ class video_service(rpyc.Service):
         anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar)
 
 
+def update_hisotry_duration(video_link):
+    """  更新資訊影片長度資訊。  """
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    table = db['history_input']
+    duration = get_mp4_duration(video_link)
+    data = dict(link=video_link, duration=duration)
+    table.update(data, ['link'])
+    
+    rows = db.query(f'SELECT * FROM history_input WHERE link="{video_link}"')
+    for row in rows:
+        user_id = row['user_id']
+
+    return duration, user_id
+
+
+def update_user_left_time(user_id, duration):
+    """  更新使用者剩下時間,如果為負,接著提醒。 """
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    table = db['users']
+    rows = db.query(f"SELECT * FROM user WHERE id={user_id}")
+    for row in rows:
+        left_time = row['left_time']
+        line_token = row['line_token']
+    left_time -= duration
+    data = dict(id=user_id, left_time=left_time-duration)
+    table.update(data, ['id'])
+
+    if left_time < 0:       
+        return False
+    else:
+        return True
+
+
+
 
 from rpyc.utils.server import ThreadedServer
 t = ThreadedServer(video_service, port=8878)