当前位置:   article > 正文

小说变成视频脚本化,并自动上传快手_把小说改成脚本

把小说改成脚本
  1. #安装如下相关模块
  2. #安装格式如下:pip install ***——“***”所需的模块
  3. #例如:在终端输入pip install pyautogui
  4. import pyautogui
  5. import pyperclip
  6. import glob
  7. from pymouse import PyMouse
  8. import cv2
  9. import numpy as np
  10. import os
  11. import time
  12. #打开快手
  13. from selenium import webdriver
  14. from selenium.webdriver.support.ui import WebDriverWait
  15. from selenium.webdriver.common.by import By
  16. from selenium.webdriver.support import expected_conditions as EC
  17. #打开剪映#小说
  18. # book='围城 出版七十周年纪念版 '
  19. # num=15
  20. m =PyMouse()
  21. #模拟坐标
  22. pyautogui.click(875, 1027)#点击剪映
  23. #剪辑文本’文本路径‘
  24. txt_path = r'D:\xiaoshuo'
  25. # Use glob to find all txt files in the folder
  26. files = glob.glob(f"{txt_path}/*.txt")
  27. # Loop through each file and process its content
  28. for file in files:
  29. name = os.path.basename(file)
  30. name=name.split('(')[0]
  31. # if name==book:
  32. #运行前清空文件夹下的内容
  33. import shutil
  34. folder_path = r'D:\z7z8\剪映视频'
  35. # 删除文件夹中的内容
  36. for filename in os.listdir(folder_path):
  37. file_path = os.path.join(folder_path, filename)
  38. try:
  39. if os.path.isfile(file_path):
  40. os.remove(file_path)
  41. elif os.path.isdir(file_path):
  42. shutil.rmtree(file_path)
  43. except Exception as e:
  44. print(e)
  45. result_list = [] #记录本文次序
  46. with open(file, 'r', encoding='utf-8') as f:
  47. content = f.read()
  48. # Split the content by dot
  49. sentences = content.split('。')
  50. # Combine sentences to meet the length requirement
  51. combined_sentence = ''
  52. for sentence in sentences:
  53. if len(combined_sentence + sentence) >= 800 and len(combined_sentence + sentence) <= 1500:
  54. combined_sentence += sentence
  55. result_list.append(combined_sentence)
  56. # if len(result_list)>num:
  57. pyperclip.copy(combined_sentence)
  58. print("字数:"+str(len(combined_sentence)))
  59. #打开图文成片导出视频
  60. pyautogui.click((1494, 122))
  61. search_region = (298, 62,290, 120)
  62. template = cv2.imread(r'D:\xiaoshuo\jianying.png', cv2.IMREAD_GRAYSCALE)
  63. timer=0
  64. while True:
  65. # 截取屏幕区域
  66. screenshot = pyautogui.screenshot(region=search_region)
  67. # 将屏幕截图和指定图像转换为灰度图像
  68. screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
  69. res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
  70. loc = np.where(res >= 0.6)
  71. if len(loc[0]) > 0 and len(loc[1]) > 0:
  72. # 找到指定图像,退出循环
  73. break
  74. # 没有找到指定图像,等待一段时间后继续搜索
  75. time.sleep(1)
  76. timer += 1
  77. if timer > 100:
  78. break
  79. print('图文')
  80. pyautogui.click((617, 229))#图文成片
  81. time.sleep(0.5)
  82. pyperclip.copy(combined_sentence)
  83. pyautogui.hotkey('ctrl', 'v')
  84. time.sleep(0.5)
  85. pyautogui.click((799, 203))#点击标题
  86. pyperclip.copy("#小说 《"+ name +'》第'+str(len(result_list))+'节')
  87. pyautogui.hotkey('ctrl', 'v')
  88. pyautogui.click((743, 843)) #选择最常用的解说。
  89. pyautogui.click((802, 619))
  90. #生成视频
  91. start_time = time.time()
  92. pyautogui.click((1221, 870))
  93. #time.sleep(120)
  94. search_region = (210, 650, 1300, 350)
  95. template = cv2.imread(r'D:\xiaoshuo\test.png', cv2.IMREAD_GRAYSCALE)
  96. timer=0
  97. while True:
  98. # 截取屏幕区域
  99. screenshot = pyautogui.screenshot(region=search_region)
  100. # 将屏幕截图和指定图像转换为灰度图像
  101. screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
  102. res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
  103. loc = np.where(res >= 0.4)
  104. if len(loc[0]) > 0 and len(loc[1]) > 0:
  105. # 找到指定图像,退出循环
  106. break
  107. # 没有找到指定图像,等待一段时间后继续搜索
  108. time.sleep(1)
  109. timer += 1
  110. if timer > 100:
  111. break
  112. end_time = time.time()
  113. run_time = end_time - start_time
  114. print(f"生成视频时间:{run_time:.2f}秒")
  115. time.sleep(20)
  116. pyautogui.click((1735, 21))#点击导出
  117. pyautogui.click((1186, 889))#导出视频
  118. search_region = (913, 366,350, 100)
  119. template = cv2.imread(r'D:\xiaoshuo\fb.png', cv2.IMREAD_GRAYSCALE)
  120. timer=0
  121. time.sleep(15)#画面太黑 经常误判
  122. while True:
  123. # 截取屏幕区域
  124. screenshot = pyautogui.screenshot(region=search_region)
  125. # 将屏幕截图和指定图像转换为灰度图像
  126. screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
  127. res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
  128. loc = np.where(res >= 0.95)
  129. if len(loc[0]) > 0 and len(loc[1]) > 0:
  130. # 找到指定图像,退出循环
  131. break
  132. # 没有找到指定图像,等待一段时间后继续搜索
  133. time.sleep(1)
  134. timer += 1
  135. if timer > 100:
  136. break
  137. #time.sleep(20)
  138. pyautogui.click((1184, 718))#发布视频
  139. print('发布')
  140. search_region = (0, 200, 1100, 800)
  141. template = cv2.imread(r'D:\xiaoshuo\dy2.png', cv2.IMREAD_GRAYSCALE)
  142. timer=0
  143. while True:
  144. # 截取屏幕区域
  145. screenshot = pyautogui.screenshot(region=search_region)
  146. # 将屏幕截图和指定图像转换为灰度图像
  147. screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
  148. res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
  149. loc = np.where(res >= 0.65)
  150. if len(loc[0]) > 0 and len(loc[1]) > 0:
  151. # 找到指定图像,退出循环
  152. break
  153. # 没有找到指定图像,等待一段时间后继续搜索
  154. time.sleep(0.3)
  155. timer += 0.3
  156. if timer > 100:
  157. break
  158. pyautogui.hotkey('ctrl', 'W')#关闭网页
  159. print('抖音网页关闭')
  160. search_region = (210, 650, 1300, 350)
  161. template = cv2.imread(r'D:\xiaoshuo\test.png', cv2.IMREAD_GRAYSCALE)
  162. timer=0
  163. while True:
  164. # 截取屏幕区域
  165. screenshot = pyautogui.screenshot(region=search_region)
  166. # 将屏幕截图和指定图像转换为灰度图像
  167. screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
  168. res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
  169. loc = np.where(res >= 0.4)
  170. if len(loc[0]) > 0 and len(loc[1]) > 0:
  171. # 找到指定图像,退出循环
  172. break
  173. # 没有找到指定图像,等待一段时间后继续搜索
  174. time.sleep(0.3)
  175. timer += 0.3
  176. if timer > 100:
  177. break
  178. time.sleep(0.5)
  179. # m.move(0,0)
  180. # pyautogui.click((1904, 10))#关闭视频导出界面
  181. pyautogui.hotkey('ctrl', 'w')
  182. time.sleep(2)
  183. #快手
  184. #视频上传
  185. mp4name="#小说 《"+ name +'》第'+str(len(result_list))+'节.mp4'
  186. profileDir = r'C:/Users/hasee/AppData/Roaming/Mozilla/Firefox/Profiles/5faj6rkv.default-release'
  187. profile = webdriver.FirefoxProfile(profileDir)
  188. driver = webdriver.Firefox(executable_path = './geckodriver', firefox_profile = profile)
  189. driver.get("https://cp.kuaishou.com/article/publish/video")
  190. for root, dirs, mp4s in os.walk(r"D:\z7z8\剪映视频"):
  191. for mp4 in mp4s:
  192. if mp4 == mp4name:
  193. # 构造完整路径
  194. mp4path = os.path.join(root, mp4)
  195. # 使用selenium上传文件
  196. button = WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CSS_SELECTOR, \
  197. "button[class='SOCr7n1uoqI-']")))
  198. file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
  199. file_input.send_keys(mp4path)
  200. time.sleep(5)#等待渲染不能删
  201. while "上传成功" != driver.find_element(By.CSS_SELECTOR, "span[class='DqNkLCyIyfQ-']").get_attribute("innerHTML"):
  202. time.sleep(3)
  203. title_input = WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='clGhv3UpdEo-']")))
  204. title_input.send_keys("#小说 《"+ name +'》第'+str(len(result_list))+'节')
  205. time.sleep(10)#等待渲染不能删
  206. WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CSS_SELECTOR, \
  207. "button[class='ant-btn ant-btn-primary GncXo-rrppc-']"))).click()
  208. time.sleep(5)
  209. pyautogui.click(1891, 27)
  210. print('快手结束')
  211. time.sleep(0.5)
  212. #pyautogui.click(875, 1027)再次点击剪映
  213. combined_sentence = ''
  214. else:
  215. combined_sentence += sentence

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/368107
推荐阅读
相关标签
  

闽ICP备14008679号