import os,random,time,math
from moviepy.editor import *
from moviepy.video.fx import resize
from PIL import Image
file1=r"D:\xy_fs_try\video_to_deal\spider_video\file\vedeo3.mp4"
file2=r"D:\xy_fs_try\video_to_deal\spider_video\file\vedeo4.mp4"
file4=r'D:\xy_fs_try\video_to_deal\spider_video\file\qianlong.mp4'
file3=r'D:\xy_fs_try\video_to_deal\source.mp4'
file5=r'D:\xy_fs_try\video_to_deal\spider_video\file\source1.mp4'
file6=r'D:\xy_fs_try\video_to_deal\spider_video\file\source2.mp4'
def extract_frames_on_original(video_path, output_path, fps=1):
original_clip = VideoFileClip(video_path)
frames = original_clip.iter_frames(fps=fps)
extracted_clips = []
for frame in frames:
extracted_clip = VideoFileClip(video_path).set_duration(1/fps).set_start(len(extracted_clips) / fps)
extracted_clips.append(extracted_clip)
# 将原视频和抽帧视频拼接起来
final_clip = clips_array([[original_clip], [clips_array([extracted_clips])]])
final_clip.write_videofile(output_path, codec='libx264')
# 使用示例
# video_file = r"D:\xy_fs_try\video_to_deal\spider_video\file\vedeo3.mp4" # 替换为你的视频文件路径
# output_video = "./extracted_video.mp4" # 替换为你想要保存的抽帧后的视频路径
# extract_frames_on_original(video_file, output_video, fps=1) # 抽取帧并生成视频,每秒一帧
def concatenate_videos(video1_path, video2_path, output_path):
'''视频拼接'''
audio=VideoFileClip(file3).audio
video = VideoFileClip(file3).without_audio()
video2 = VideoFileClip(file5).without_audio()
video3 = VideoFileClip(file6).without_audio()
re_video=video.subclip(1,25)
ba_video=video.subclip(26,56.7)
# # 拼接两个视频剪辑
final_clip = concatenate_videoclips([re_video, video2,video3,ba_video],method='compose')
# 计算需要保留的帧数
new_fps = final_clip.duration / audio.duration
final_clip=final_clip.set_fps(new_fps)
final_clip=final_clip.set_audio(audio)
final_clip.write_videofile(output_path, codec='libx264')
final_clip.close()
# video1_file = file3 # 替换为第一个视频文件路径
# video2_file = file4 # 替换为第二个视频文件路径
# output_video = "./concatenated_video.mp4" # 替换为输出的合并视频路径
# concatenate_videos(video1_file, video2_file, output_video) # 将两个视频拼接成一个新视频
def concatenate_option_five(source_path,source_files_path):
'''方案5 假设从素材库提取的都是4秒的视频'''
if not os.path.exists(source_files_path):
return '暂无素材库'
dir_filename=[source_files_path+"\\"+item for item in os.listdir(source_files_path)]
audio=VideoFileClip(file3).audio
video = VideoFileClip(file3).without_audio()
video_duration=video.duration
latest_video=[]
max_index=[]
for index in range(4,int(video_duration),8):
cut_video=video.subclip(index-4,index)
random.shuffle(dir_filename)
add_vedio=dir_filename.pop()
update_vedio=VideoFileClip(add_vedio).without_audio()
latest_video.append(cut_video)
latest_video.append(update_vedio)
max_index.append(index)
update_vedio.close()
latest_vedio = video.subclip(max(max_index),video_duration)
latest_video.append(latest_vedio)
final_clip = concatenate_videoclips(latest_video,method='compose')
final_clip=final_clip.set_audio(audio)
final_clip.write_videofile('./localstore.mp4', codec='libx264')
video.close()
final_clip.close()
# video1_file = file3 # 替换为第一个视频文件路径
# video2_file = r'D:\xy_fs_try\video_to_deal\spider_video\file' # 替换为第二个视频文件路径
# now_time=time.time()
# concatenate_option_five(video1_file,video2_file)
# print(time.time()-now_time)
def Material_Segmentation(material_path):
source_clips=VideoFileClip(material_path).without_audio()
print(source_clips.duration)
_idx=0
for timer in range(4,math.ceil(source_clips.duration),4):
_idx+=1
new_clips=source_clips.subclip(timer-4,timer)
new_clips.write_videofile(r'D:\xy_fs_try\video_to_deal\temperary_file1\{:0>4d}.mp4'.format(_idx))
new_clips.close()
source_clips.close()
def concatenate_option_five_last(source_path,source_files_path):
'''方案5 假设从素材库提取的都是4秒的视频'''
if not os.path.exists(source_files_path):
return '暂无素材库'
dir_filename=[source_files_path+"\\"+item for item in os.listdir(source_files_path)]
random.shuffle(dir_filename)
audio=VideoFileClip(source_path).audio
video = VideoFileClip(source_path).without_audio()
video_duration=video.duration
latest_video=[]
max_index=[]
for index in range(4,int(video_duration),8):
cut_video=video.subclip(index-4,index)
if not dir_filename:
print('素材已完')
max_index.append(index-4)
break
add_vedio=dir_filename.pop()
update_vedio=VideoFileClip(add_vedio).without_audio()
latest_video.append(cut_video)
latest_video.append(update_vedio)
max_index.append(index)
update_vedio.close()
latest_vedio = video.subclip(max(max_index),video_duration)
latest_video.append(latest_vedio)
final_clip = concatenate_videoclips(latest_video,method='compose')
final_clip=final_clip.set_audio(audio)
final_clip.write_videofile('./localstore.mp4', codec='libx264')
video.close()
final_clip.close()
# Material_Segmentation(r'D:\xy_fs_try\video_to_deal\spider_video\file\source2.mp4')
video1_file = file3 # 替换为第一个视频文件路径
video2_file = r'D:\xy_fs_try\video_to_deal\temperary_file' # 替换为第二个视频文件路径
now_time=time.time()
concatenate_option_five_last(video1_file,video2_file)
print(time.time()-now_time)
moviepy 视频剪辑拼接
最新推荐文章于 2024-08-31 21:39:38 发布