#安装如下相关模块
#安装格式如下:pip install ***——“***”所需的模块
#例如:在终端输入pip install pyautogui
import pyautogui
import pyperclip
import glob
from pymouse import PyMouse
import cv2
import numpy as np
import os
import time
#打开快手
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#打开剪映#小说
# book='围城 出版七十周年纪念版 '
# num=15
m =PyMouse()
#模拟坐标
pyautogui.click(875, 1027)#点击剪映
#剪辑文本’文本路径‘
txt_path = r'D:\xiaoshuo'
# Use glob to find all txt files in the folder
files = glob.glob(f"{txt_path}/*.txt")
# Loop through each file and process its content
for file in files:
name = os.path.basename(file)
name=name.split('(')[0]
# if name==book:
#运行前清空文件夹下的内容
import shutil
folder_path = r'D:\z7z8\剪映视频'
# 删除文件夹中的内容
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(e)
result_list = [] #记录本文次序
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
# Split the content by dot
sentences = content.split('。')
# Combine sentences to meet the length requirement
combined_sentence = ''
for sentence in sentences:
if len(combined_sentence + sentence) >= 800 and len(combined_sentence + sentence) <= 1500:
combined_sentence += sentence
result_list.append(combined_sentence)
# if len(result_list)>num:
pyperclip.copy(combined_sentence)
print("字数:"+str(len(combined_sentence)))
#打开图文成片导出视频
pyautogui.click((1494, 122))
search_region = (298, 62,290, 120)
template = cv2.imread(r'D:\xiaoshuo\jianying.png', cv2.IMREAD_GRAYSCALE)
timer=0
while True:
# 截取屏幕区域
screenshot = pyautogui.screenshot(region=search_region)
# 将屏幕截图和指定图像转换为灰度图像
screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.6)
if len(loc[0]) > 0 and len(loc[1]) > 0:
# 找到指定图像,退出循环
break
# 没有找到指定图像,等待一段时间后继续搜索
time.sleep(1)
timer += 1
if timer > 100:
break
print('图文')
pyautogui.click((617, 229))#图文成片
time.sleep(0.5)
pyperclip.copy(combined_sentence)
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.5)
pyautogui.click((799, 203))#点击标题
pyperclip.copy("#小说 《"+ name +'》第'+str(len(result_list))+'节')
pyautogui.hotkey('ctrl', 'v')
pyautogui.click((743, 843)) #选择最常用的解说。
pyautogui.click((802, 619))
#生成视频
start_time = time.time()
pyautogui.click((1221, 870))
#time.sleep(120)
search_region = (210, 650, 1300, 350)
template = cv2.imread(r'D:\xiaoshuo\test.png', cv2.IMREAD_GRAYSCALE)
timer=0
while True:
# 截取屏幕区域
screenshot = pyautogui.screenshot(region=search_region)
# 将屏幕截图和指定图像转换为灰度图像
screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.4)
if len(loc[0]) > 0 and len(loc[1]) > 0:
# 找到指定图像,退出循环
break
# 没有找到指定图像,等待一段时间后继续搜索
time.sleep(1)
timer += 1
if timer > 100:
break
end_time = time.time()
run_time = end_time - start_time
print(f"生成视频时间:{run_time:.2f}秒")
time.sleep(20)
pyautogui.click((1735, 21))#点击导出
pyautogui.click((1186, 889))#导出视频
search_region = (913, 366,350, 100)
template = cv2.imread(r'D:\xiaoshuo\fb.png', cv2.IMREAD_GRAYSCALE)
timer=0
time.sleep(15)#画面太黑 经常误判
while True:
# 截取屏幕区域
screenshot = pyautogui.screenshot(region=search_region)
# 将屏幕截图和指定图像转换为灰度图像
screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.95)
if len(loc[0]) > 0 and len(loc[1]) > 0:
# 找到指定图像,退出循环
break
# 没有找到指定图像,等待一段时间后继续搜索
time.sleep(1)
timer += 1
if timer > 100:
break
#time.sleep(20)
pyautogui.click((1184, 718))#发布视频
print('发布')
search_region = (0, 200, 1100, 800)
template = cv2.imread(r'D:\xiaoshuo\dy2.png', cv2.IMREAD_GRAYSCALE)
timer=0
while True:
# 截取屏幕区域
screenshot = pyautogui.screenshot(region=search_region)
# 将屏幕截图和指定图像转换为灰度图像
screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.65)
if len(loc[0]) > 0 and len(loc[1]) > 0:
# 找到指定图像,退出循环
break
# 没有找到指定图像,等待一段时间后继续搜索
time.sleep(0.3)
timer += 0.3
if timer > 100:
break
pyautogui.hotkey('ctrl', 'W')#关闭网页
print('抖音网页关闭')
search_region = (210, 650, 1300, 350)
template = cv2.imread(r'D:\xiaoshuo\test.png', cv2.IMREAD_GRAYSCALE)
timer=0
while True:
# 截取屏幕区域
screenshot = pyautogui.screenshot(region=search_region)
# 将屏幕截图和指定图像转换为灰度图像
screenshot_gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
res = cv2.matchTemplate(screenshot_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.4)
if len(loc[0]) > 0 and len(loc[1]) > 0:
# 找到指定图像,退出循环
break
# 没有找到指定图像,等待一段时间后继续搜索
time.sleep(0.3)
timer += 0.3
if timer > 100:
break
time.sleep(0.5)
# m.move(0,0)
# pyautogui.click((1904, 10))#关闭视频导出界面
pyautogui.hotkey('ctrl', 'w')
time.sleep(2)
#快手
#视频上传
mp4name="#小说 《"+ name +'》第'+str(len(result_list))+'节.mp4'
profileDir = r'C:/Users/hasee/AppData/Roaming/Mozilla/Firefox/Profiles/5faj6rkv.default-release'
profile = webdriver.FirefoxProfile(profileDir)
driver = webdriver.Firefox(executable_path = './geckodriver', firefox_profile = profile)
driver.get("https://cp.kuaishou.com/article/publish/video")
for root, dirs, mp4s in os.walk(r"D:\z7z8\剪映视频"):
for mp4 in mp4s:
if mp4 == mp4name:
# 构造完整路径
mp4path = os.path.join(root, mp4)
# 使用selenium上传文件
button = WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CSS_SELECTOR, \
"button[class='SOCr7n1uoqI-']")))
file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
file_input.send_keys(mp4path)
time.sleep(5)#等待渲染不能删
while "上传成功" != driver.find_element(By.CSS_SELECTOR, "span[class='DqNkLCyIyfQ-']").get_attribute("innerHTML"):
time.sleep(3)
title_input = WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='clGhv3UpdEo-']")))
title_input.send_keys("#小说 《"+ name +'》第'+str(len(result_list))+'节')
time.sleep(10)#等待渲染不能删
WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CSS_SELECTOR, \
"button[class='ant-btn ant-btn-primary GncXo-rrppc-']"))).click()
time.sleep(5)
pyautogui.click(1891, 27)
print('快手结束')
time.sleep(0.5)
#pyautogui.click(875, 1027)再次点击剪映
combined_sentence = ''
else:
combined_sentence += sentence
小说变成视频脚本化,并自动上传快手
最新推荐文章于 2023-08-02 00:01:27 发布