| 
					
				 | 
			
			
				@@ -19,8 +19,6 @@ 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/' 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -31,36 +29,18 @@ dir_subtitle = 'subtitle/' 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 dir_anchor = 'anchor_raw/' 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 tmp_video_dir = 'tmp_video/' 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 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) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-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 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        params = {"message": msg}    
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 def cKey(r,g,b,fuzz): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     col=openshot.Color() 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -132,7 +112,6 @@ def file_prepare(name, name_hash,text_content,image_urls,lang='zh'): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     make_dir(name_hash) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     img_num = 1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     for imgu in image_urls: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if get_url_type(imgu) =='video/mp4': 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             r=requests.get(imgu) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             f=open(dir_photo+name_hash+"/"+str(img_num)+".mp4",'wb') 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -167,7 +146,7 @@ def file_prepare(name, name_hash,text_content,image_urls,lang='zh'): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     print("mp3 file made") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     #make title as image 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     txt2image_title(name, dir_title+name_hash+".png",lang) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 def txt2image(content, save_target,lang='zh'): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     unicode_text = trim_punctuation(content) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     font = '' 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -273,7 +252,6 @@ 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) 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -410,26 +388,8 @@ def anchor_video_v2(name_hash,name,text_content, image_urls,avatar): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     for n in range(frames): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         f=t.GetFrame(n) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         w.WriteFrame(f) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    # 更新剩下時間、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. 餘額不足 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    # 找到user-line群組token 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    email, line_token = get_user_line_token_email(user_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if is_left_time: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4", glist=[line_token, 'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        contents = f"""  影片下載網址: {video_link}""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        mail_to_users(user_id, f'您好,您的影片 "{name}" 已經完成', contents=contents) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    else: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        notify_group(msg="您的餘額不足,請去...儲值,才能取得影片唷!", glist=[line_token, 'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        contents = f"""  餘額不足,請往: ...儲值url儲值 """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        mail_to_users(user_id, f'您好,您的餘額不足', contents) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    #notify_group(name+"的影片已經產生完成囉! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     t.Close() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     w.Close() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     print("video at : www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4") 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -567,26 +527,8 @@ 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) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    # 更新剩下時間、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. 餘額不足 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    # 找到user-line群組token 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    email, line_token = get_user_line_token_email(user_id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if is_left_time: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        notify_group(name+", Your video is complete! www.choozmo.com:8168/"+video_sub_folder+name_hash+".mp4", glist=[line_token, 'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        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!", glist=[line_token, 'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        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") 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -600,62 +542,9 @@ 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 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-def get_user_line_token_email(user_id): 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    """ 取得使用者line-token、email用以通知用。 """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    query = f""" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        select  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            A.user_id, A.link, B.email, B.line_token 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        from 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ((select link, user_id,  duration 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            from AI_anchor.history_input ) as A 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        inner join (select  id, email, line_token from AI_anchor.users where id={user_id}) as B 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        on A.user_id=B.id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    """ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    rows = db.query(query) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    for row in rows: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        line_token = row['line_token'] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        email = row['email'] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        break 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    return line_token, email 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 from rpyc.utils.server import ThreadedServer 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 t = ThreadedServer(video_service, port=8858) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 print('service started') 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-t.start() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+t.start() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 |