import hashlib
import time
import uuid as uu
import datetime as dt
import pathlib
from threading import Thread
import numpy as np
from selenium import webdriver
from moviepy.editor import *
from PIL import ImageEnhance, Image
# 基本信息 Google\ Chrome --remote-debugging-port=19222 --user-data-dir="~/ChromeProfile"
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
options.add_experimental_option("debuggerAddress", "127.0.0.1:19222")
driver = webdriver.Chrome(chrome_options=options)
def myasync(f):
def wrapper(*args, **kwargs):
thr = Thread(target=f, args=args, kwargs=kwargs)
thr.start()
return wrapper
def str_size(s):
a = s.encode('utf-8')
return len(a)
def get_file_md5(file_path):
m = hashlib.md5() # 创建md5对象
with open(file_path, 'rb') as fobj:
while True:
data = fobj.read(4096)
if not data:
break
m.update(data)
return m.hexdigest()
def modifyVedio(file_path):
uid = str(uu.uuid4())
uuid = ''.join(uid.split('-'))
with open(file_path, 'a') as file:
file.write("******" + uuid)
def strDate(diff=0):
d = dt.date.today() + dt.timedelta(days=diff)
return '{0:04}-{1:02}-{2:02}'.format(d.year, d.month, d.day)
def invert_green_blue(image):
image = Image.fromarray(np.uint8(image)) # 转换成PIL可以处理的格式
# # 亮度增强
# enh_bri = ImageEnhance.Brightness(image)
# image = enh_bri.enhance(2)
# 色度增强
enh_col = ImageEnhance.Color(image)
image = enh_col.enhance(1.1)
# 对比度增强
enh_con = ImageEnhance.Contrast(image)
image = enh_con.enhance(1.1)
# 锐度增强
enh_sha = ImageEnhance.Sharpness(image)
image = enh_sha.enhance(1.1)
return np.asarray(image)
# def scroll(image):
# """
# This function returns a 'region' of the current frame.
# The position of this region depends on the time.
# """
# frame = get_frame(t)
# frame_region = frame[int(t): int(t) + 360, :]
# return frame_region
# # 反转色相
def increase_video_brightness2(file_path, stem):
uid = str(uu.uuid4())
uuid = ''.join(uid.split('-'))
mp4_name = stem + "-" + uuid + "x.mp4"
root = "/Users/xiaohangmac/Desktop/douyin/shudanhao/"
if os.access(root + mp4_name, os.F_OK):
os.remove(os.path.join(root, mp4_name))
initial_color = (125, 125, 125) # fadein 用
final_color = (0, 0, 0) # fadeout 用
duration = 4 # 持续时间
lum = -20 # 亮度要增加或减少的值[-255,255]
contrast = 0 # 对比度
# 增速1.1
audioclip = AudioFileClip("/Users/xiaohangmac/Desktop/douyin/shudanhao/22.mp3")
# audioclip = afx.audio_loop(audioclip, duration=audioclip.duration)
video = VideoFileClip(file_path).fx(vfx.speedx, 1) \
.fx(vfx.colorx, 1.1) \
.fx(vfx.lum_contrast, lum=lum, contrast=contrast) \
.fx(vfx.fadein, duration, final_color) \
.volumex(1.2) \
.fl_image(invert_green_blue)
# .fl(scroll)
# video.set_audio(audioclip)
width, height = video.size
logo = (ImageClip("/Users/xiaohangmac/Desktop/douyin/shudanhao/WechatIMG22.png")
.set_duration(video.duration) # 时长
.resize(height=150) # 水印高度,等比缩放
.margin(left=0, top=0, opacity=0.1) # 水印边距和透明度
.set_position(
lambda pos: (max(width / 30, int(width - 0.5 * width * pos)), max(5 * height / 6, int(100 * pos))))) # 水印位置
result = CompositeVideoClip([video, logo])
result.write_videofile("/Users/xiaohangmac/Desktop/douyin/shudanhao/" + mp4_name, audio_codec='aac', codec="libx264",
bitrate="10000000")
@myasync
def go(stem, path_mp4):
print("原始MD5" + get_file_md5(path_mp4))
modifyVedio(path_mp4)
print("修改后MD5" + get_file_md5(path_mp4))
increase_video_brightness2(path_mp4, stem)
os.system('cls')
# 视频地址获取
# path_mp4 = "/Users/xiaohangmac/Desktop/douyin/shudanhao/1.mp4"
# 视频存放路径
catalog_mp4 = "/Users/xiaohangmac/Desktop/douyin/shudanhao"
path = pathlib.Path(catalog_mp4)
def publish_douyin():
# while not os.access(path_mp4, os.F_OK):
# print("视频正在生成中···")
# time.sleep(1)
j = 0
for i in path.iterdir():
if ".mp4" in str(i) and "x.mp4" not in str(i) and str_size(i.stem) <= 5:
j = j + 1
if (j % 5) == 0:
time.sleep(300)
go(i.stem, str(i))
driver.get("https://creator.douyin.com/creator-micro/content/publish")
# driver.find_element(By.NAME, "upload-btn").send_keys(str(i))
# # 等待视频上传完成
# while True:
# time.sleep(1)
# try:
# if driver.find_element(By.XPATH, "//*[text()='重新上传']").text == '重新上传':
# break
# else:
# print("视频还在上传中···")
# except Exception as e:
# print("视频还在上传中···")
# print("视频已上传完成!")
# # 添加封面
# driver.find_element_by_xpath('//*[text()="编辑封面"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//div[text()="上传封面"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@type="file"]').send_keys(path_cover)
# time.sleep(3)
# driver.find_element_by_xpath('//*[text()="裁剪封面"]/..//*[text()="确定"]').click()
# time.sleep(3)
# driver.find_element_by_xpath('//*[text()="设置封面"]/..//*[contains(@class,"upload")]//*[text()="确定"]').click()
#
# time.sleep(5)
# 输入视频描述
# driver.find_element(By.XPATH,
# '//div[@contenteditable="true" and @data-placeholder="写一个合适的标题,能让更多人看到"]').send_keys(
# describe + " #我要上热门dou抖音小助手 #人性智慧 #金句良言")
# 设置选项
# time.sleep(1)
# 视频选项
# driver.find_element(By.XPATH, '//*[@class="semi-cascader-selection"]//span').send_keys("园")
# 发布时间 定时发布
# driver.find_element(By.XPATH, '//*[text()="定时发布"]/preceding-sibling::input').click()
# 发布时间
# driver.find_element(By.XPATH, '//*[@format="yyyy-MM-dd HH:mm"]').send_keys(strDate(1) + " 16:50")
# os.remove(os.path.join(root, mp4_name))
# 是否同步到今日头条
# driver.find_element(By.XPATH, '//input[@class="semi-switch-native-control"]').click()
# driver.find_element_by_xpath('//*[@class="radio--4Gpx6"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//*[@class="semi-select-selection"]//span[contains(text(),"输入")]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//*[@class="semi-select-selection"]//input').send_keys("中关村人工智能科技")
# time.sleep(1)
# driver.find_element_by_xpath('//*[@class="semi-select-selection"]//input').send_keys("园")
# time.sleep(5)
# driver.find_element_by_xpath('//*[@class="semi-popover-content"]//*[text()="中关村人工智能科技园"]').click()
#
# # 同步到西瓜视频
# time.sleep(1)
# # driver.find_element_by_xpath('//div[@class="preview--27Xrt"]//input').click() # 默认启用一次后,后面默认启用了。
# time.sleep(1)
# driver.find_element_by_xpath('//*[@class="card-pen--2P8rh"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//*[@class="DraftEditor-root"]//br').send_keys(describe + " #上热门")
# time.sleep(1)
# driver.find_element_by_xpath('//button[text()="确定"]').click()
# time.sleep(10)
# 点击发布
# driver.find_element(By.XPATH, '//button[text()="发布"]').click()
# 开始执行视频发布
publish_douyin()
print("----------ok----------")
备份20221201
于 2022-12-01 00:21:26 首次发布