Browse Source

上傳檔案到 'AI_anchor2'

oak 2 years ago
parent
commit
f3511aab7d
1 changed files with 62 additions and 38 deletions
  1. 62 38
      AI_anchor2/main1.py

+ 62 - 38
AI_anchor2/main1.py

@@ -17,6 +17,7 @@ from bs4 import BeautifulSoup
 import zipfile
 import csv
 from datetime import datetime
+import rarfile
 def cKey(r,g,b,fuzz):
     col=openshot.Color()
     col.red=openshot.Keyframe(r)
@@ -101,46 +102,59 @@ def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導
 
     files=os.listdir("input/")
     print(files)
-    for i in files:
-        if i[-4:]==".zip":
-            input_zip="input/"+i
-            break
+    if len(files)==0:
+        print("input資料夾沒有檔案,請放入")
+        return 
+    elif len(files)>1:
+        print("檔案只能有一個,且為壓縮檔")
+        return 
+    if files[0][-4:]==".zip":
+        input_zip="input/"+files[0]
 
-    try:
+
+        
         with zipfile.ZipFile(input_zip, 'r') as zf:
             for fn in zf.namelist():
 
                 right_fn = fn.encode('cp437').decode('big5')  # 將檔名正確編碼
                 check_p=right_fn.split("/")
-                try:
-                    if right_fn[-1]=="/" :
-                        os.mkdir("input/"+right_fn[:-1])
-                        pwd_use=right_fn
-                        break
-                    if len(check_p)==2:
-                        os.mkdir("input/"+check_p[0])
-                        pwd_use=check_p[0]+"/"
-                        break
-                except:
-                    print("裡面應該只包含zip檔案,請只留下zip")
-                    return
-    except:
-        print("裡面無檔案,請放入zip")
+                
+                if right_fn[-1]=="/" :
+                    os.mkdir("input/"+right_fn[:-1])
+                    pwd_use=right_fn
+                    break
+                if len(check_p)==2:
+                    os.mkdir("input/"+check_p[0])
+                    pwd_use=check_p[0]+"/"
+                    break
+                    
+
+        read_csv_use = 0
+        with zipfile.ZipFile(input_zip, 'r') as zf:
+            for fn in zf.namelist():
+                right_fn = fn.encode('cp437').decode('big5')  # 將檔名正確編碼
+                check_p=right_fn.split("/")
+                if right_fn[-1]=="/" :
+                    continue
+                if right_fn[-4:]==".csv" or right_fn[-5:]==".xlsx":
+                    read_csv_use = right_fn
+                with open("input/"+right_fn, 'wb') as output_file:  # 建立並開啟新檔案
+                    with zf.open(fn, 'r') as origin_file:  # 開啟原檔案
+                        shutil.copyfileobj(origin_file, output_file)  # 將原檔案內容複製到新檔案
+
+    elif files[0][-4:]==".rar":
+        rar_ = rarfile.RarFile("input/"+files[0])
+        names = rar_.namelist()
+        for name in names:
+            if name[-4:]==".csv" or name[-5:]==".xlsx":
+                    read_csv_use = name
+            pwd_use=name.split("/")[0]+"/"
+            rar_.extract(name,"input/")
+        rar_.close()
+        
+    else:
+        print("目前不支援此壓縮格式或不為壓縮檔")
         return
-    read_csv_use = 0
-    with zipfile.ZipFile(input_zip, 'r') as zf:
-        for fn in zf.namelist():
-            right_fn = fn.encode('cp437').decode('big5')  # 將檔名正確編碼
-            check_p=right_fn.split("/")
-            if right_fn[-1]=="/" :
-                continue
-            if right_fn[-4:]==".csv" :
-                read_csv_use = right_fn
-            with open("input/"+right_fn, 'wb') as output_file:  # 建立並開啟新檔案
-                with zf.open(fn, 'r') as origin_file:  # 開啟原檔案
-                    shutil.copyfileobj(origin_file, output_file)  # 將原檔案內容複製到新檔案
-
-
 
 
 
@@ -150,11 +164,19 @@ def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導
     ck_anchor=None
     #時間
     time_= 0
-    
-    csv_use=pd.read_csv("input/"+read_csv_use)
+    try:
+        if read_csv_use[-4:]==".csv":
+            csv_use=pd.read_csv("input/"+read_csv_use)
+        elif read_csv_use[-5:]==".xlsx":
+            csv_use = pd.read_excel("input/"+read_csv_use)
+    except:
+        print("excel不存在或excel格式不支援或損毀")
+        return
+
+
     csv_use=csv_use.dropna(how='all')
     csv_use.reset_index(inplace=True)
-
+    # print(csv_use)
 
 
     t1 = openshot.Timeline(1280, 720, openshot.Fraction(30000, 1000), 44100, 2, openshot.LAYOUT_STEREO)
@@ -359,6 +381,7 @@ def text_to_short_vedio_create(read_csv_use="導盲犬協會.csv",pwd_use="導
     os.mkdir('input')
     shutil.rmtree('input_self/AI_girl')
     os.mkdir('input_self/AI_girl')
+    return True
 #文字轉圖片
 def txt2image(content, save_target,lang='zh',size=26,fon="input_self/font/DFT_B7.ttc"):
     unicode_text = trim_punctuation(content)
@@ -506,7 +529,8 @@ def vedio_to_csv(sound_file):
     srt_to_csv(sound_srt_file)
 
 if __name__ == '__main__':
-    text_to_short_vedio_create()
-    vedio_to_csv("output/no_captions/test.mp4")
+    down=text_to_short_vedio_create()
+    if down:
+        vedio_to_csv("output/no_captions/test.mp4")