公司需要剪辑,近期混剪特别多,所以开发了这个,有兴趣的研究研究,如果有好的代码补充优化或者分享麻烦艾特我
`import os
import hashlib
import threading
import random
import threading
import moviepy.editor as mpy
from moviepy.editor import AudioFileClip, VideoFileClip, concatenate_videoclips
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from lxml import etree
import requests
初始化函数
def initialize():
# 检查目录分级,不存在就创建
if not os.path.exists("素材目录"):
os.makedirs("素材目录", exist_ok=True)
if not os.path.exists("素材目录/半成品目录"):
os.makedirs("素材目录/半成品目录", exist_ok=True)
if not os.path.exists("素材目录/查重目录"):
os.makedirs("素材目录/查重目录", exist_ok=True)
if not os.path.exists("素材目录/成品目录"):
os.makedirs("素材目录/成品目录", exist_ok=True)
if not os.path.exists("素材目录/纯音乐"):
os.makedirs("素材目录/纯音乐", exist_ok=True)
if not os.path.exists("素材目录/音乐目录"):
os.makedirs("素材目录/音乐目录", exist_ok=True)
功能菜单选择函数
def menu_selection():
print("功能选择提示与输入:")
print("功能 1:视频自动混剪")
print("功能 2:添加随机 bgm")
print("功能 3:视频查重")
print("功能 4:敬请期待")
print("功能 5:退出程序")
return int(input("请输入要执行的功能编号:"))
混剪线程
def auto_video_mix(input_dir, output_dir, num_parts=2, num_videos=1):
# 创建分镜目录列表
parts = []
for i in range(1, num_parts + 1):
part = f"分镜{i}"
parts.append(part)
print("分镜头合并顺序如下:\n" + str(parts))
with ThreadPoolExecutor(max_workers=4) as executor:
for i in range(num_videos):
executor.submit(merge_and_save_video, output_dir, i)
混剪程序
def merge_and_save_video(output_path, i):
# 选择每个分镜头中的一个视频
parts = []
for j in range(1, num_parts + 1):
part = f"素材目录/分镜目录/分镜{j}"
parts.append(part)
selected_videos = []
for folder in parts:
videos_in_folder = [os.path.join(folder, video) for video in os.listdir(folder)]
selected_video = random.choice(videos_in_folder)
selected_videos.append(selected_video)
# 合并视频
clips = [VideoFileClip(video) for video in selected_videos]
merged_clip = concatenate_videoclips(clips)
# 保存合并后的视频
merged_video_path = f"{output_path}/{datetime.now().strftime('%m%d')}_{i}.mp4"
merged_clip.write_videofile(merged_video_path)
添加随机 bgm 函数线程
def add_random_bgm(input_dir, output_dir, music_dir):
with ThreadPoolExecutor(max_workers=4) as executor:
videos_in_folder = [os.path.join(input_dir, video) for video in os.listdir(input_dir)]
for i in range(len(videos_in_folder)):
executor.submit(combine_music_and_video, input_dir, music_dir, output_dir, i)
添加随机 bgm 函数程序
def combine_music_and_video(input_dir, music_dir, output_dir, i):
# 随机选择一个音乐文件
music_file = random.choice(os.listdir(music_dir))
# 加载音乐和视频
music = mpy.AudioFileClip(os.path.join(music_dir, music_file))
videos_in_folder = [os.path.join(input_dir, video) for video in os.listdir(input_dir)]
# print(videos_in_folder[i])
semi_finished_video = mpy.VideoFileClip(os.path.join(videos_in_folder[i]))
# 截取音乐与视频长度一致的片段
music_duration = semi_finished_video.duration
start_time = random.uniform(20, music.duration - music_duration)
music_clip = music.subclip(start_time, start_time + music_duration)
# 合成视频和音乐
final_video = semi_finished_video.set_audio(music_clip)
# 生成文件名
now = datetime.now()
formatted_date = now.strftime("%m-%d")
filename = f"{formatted_date}_纯音乐_{i}.mp4"
# 保存合成后的视频到成品文件夹
final_video.write_videofile(os.path.join(output_dir, filename), fps=semi_finished_video.fps)
哈希查重函数
def hash_check(input_dir, threshold):
# 获取输入目录下的所有文件
files = os.listdir(input_dir)
# 创建输出文件列表
output_files = []
for file in files:
output_file = f"{input_dir}/{file}_C_{hashlib.md5(open(file, 'rb').read()).hexdigest()}"
output_files.append(output_file)
# 启动线程进行哈希计算
threads = []
for i in range(len(output_files)):
thread = threading.Thread(target=check_hash, args=(output_files[i]))
threads.append(thread)
thread.start()
# 等待所有线程完成
for thread in threads:
thread.join()
# 输出重复率大于阈值的文件列表
print("重复率大于阈值的文件:")
for file in output_files:
if os.path.exists(file):
print(file)
def verify():
url = "https://docs.qq.com/doc/DTkZuYUlXT3dnRmVr"
# 发送 HTTP 请求获取网页内容
response = requests.get(url)
# 将响应内容转换为 HTML 树
html_tree = etree.HTML(response.text)
# 根据 XPath 获取特定元素的值
target_value = html_tree.xpath('/html/head/title/text()')[0] # 替换为实际的 XPath 表达式
return target_value
主程序
if name == "main":
print("开始网络验证,请稍等")
a = verify()
if a == "1":
print("验证通过,小疯还在职")
else:
input("验证失败,小疯已离职")
exit()
initialize()
while True:
choice = menu_selection()
if choice == 1:
input_dir = f"素材目录/分镜目录"
output_dir = "素材目录/半成品目录"
num_parts = int(input("请输入分镜目录的数量:"))
for i in range(1, num_parts + 1):
os.makedirs(f"素材目录/分镜目录/分镜{i}", exist_ok=True)
num_videos = int(input("请输入要合成的视频数量:"))
auto_video_mix(input_dir, output_dir, num_parts, num_videos)
elif choice == 2:
music_dir = "素材目录/音乐目录"
input_dir = "素材目录/半成品目录"
output_dir = "素材目录/纯音乐"
add_random_bgm(input_dir, output_dir, music_dir)
elif choice == 3:
input_dir = "素材目录/查重目录"
threshold = float(input("请输入重复率阈值:"))
hash_check(input_dir, threshold)
elif choice == 4:
print("敬请期待")
elif choice == 5:
exit()
`
标签:视频,Python,混剪,video,output,input,os,目录,dir From: https://www.cnblogs.com/ruler-madman/p/18196209