├── .DS_Store ├── 1080横屏视频文件夹 └── .DS_Store ├── 1920竖屏视频文件夹 └── .DS_Store ├── README.md ├── music ├── .DS_Store ├── converted_music.mp3 └── 下载 (3).mp4 ├── run.py ├── sizeVideosGroup.py └── videos └── .DS_Store /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/.DS_Store -------------------------------------------------------------------------------- /1080横屏视频文件夹/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/1080横屏视频文件夹/.DS_Store -------------------------------------------------------------------------------- /1920竖屏视频文件夹/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/1920竖屏视频文件夹/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 将所有视频打碎重组成新的20个视频 2 | 3 | 4 | 5 | 6 | # prompt 7 | # 帮我用python写一段批量剪辑视频的代码, 8 | # 1. 批量识别videos文件下的所有mp4/MP4或者mov/MOV 9 | # 2. 读取音频的文件夹music,该文件夹中的第一个文件就是该音乐,该文件可能是一个mp4/MP4格式或者是mov/MOV格式或者是mp3格式,不管什么格式都需要转成mp3音频 10 | # 3. 将videos文件下的所有视频按照2秒剪切成若干短视频 并去掉音频(不要频繁创建文件,尽量在内存中完成),比如video.without_audio() 11 | # 4. 将这些尺寸相同的短视频随机打乱并重新组合在一起组成新的5个长视频,时长与mp3音频的时长一致 12 | # 比如 13 | # # 打乱顺序 14 | # random.shuffle(video_clips) 15 | 16 | # 5. 5个视频的音频需要全部都替换成music中的这个mp3 ,比如 video.set_audio(music) 17 | 18 | # # 最后将这替换好的5个长视频进行水平镜像翻转后保存到新的文件夹中,新的文件夹命名为日期,比如12月15日 19 | 20 | # # 请只用以下工具包完成代码,不要新增库 21 | # import os 22 | # import random 23 | # from moviepy.editor import VideoFileClip, concatenate_videoclips,AudioFileClip, CompositeVideoClip, vfx 24 | # from moviepy.audio.fx.volumex import volumex 25 | # from datetime import datetime 26 | 27 | # 请把关键的变量拿出来,然后注释好中文,比如:做成多少个新视频,多少秒剪切 28 | # 在运行过程中请在命令行中展示进度条 -------------------------------------------------------------------------------- /music/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/music/.DS_Store -------------------------------------------------------------------------------- /music/converted_music.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/music/converted_music.mp3 -------------------------------------------------------------------------------- /music/下载 (3).mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/music/下载 (3).mp4 -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | """prompt 帮我用python写一段批量剪辑视频的代码, 2 | 1. 批量识别videos文件下的所有mp4/MP4或者mov/MOV 3 | 2. 读取音频的文件夹music,该文件夹中的第一个文件就是该音乐,该文件可能是一个mp4/MP4格式或者是mov/MOV格式或者是mp3格式,不管什么格式都需要转成mp3音频 4 | 3. 将videos文件下的所有视频按照2秒剪切成若干短视频 并去掉音频(不要频繁创建文件,尽量在内存中完成),比如video.without_audio() 5 | 4. 将这些尺寸相同的短视频随机打乱并重新组合在一起组成新的5个长视频,时长与mp3音频的时长一致 6 | 比如 7 | # 打乱顺序 8 | random.shuffle(video_clips) 9 | 10 | 5. 5个视频的音频需要全部都替换成music中的这个mp3 ,比如 video.set_audio(music) 11 | 12 | # 最后将这替换好的5个长视频进行水平镜像翻转后保存到新的文件夹中,新的文件夹命名为日期,比如12月15日 13 | 14 | # 请只用以下工具包完成代码,不要新增库 15 | import os 16 | import random 17 | from moviepy.editor import VideoFileClip, concatenate_videoclips,AudioFileClip, CompositeVideoClip, vfx 18 | from moviepy.audio.fx.volumex import volumex 19 | from datetime import datetime 20 | from tqdm import tqdm 21 | 22 | 请把关键的变量拿出来,然后注释好中文,比如:做成多少个新视频,多少秒剪切 23 | 在运行过程中请在命令行中展示进度条 24 | """ 25 | 26 | import os 27 | import random 28 | from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, vfx 29 | from moviepy.audio.fx.volumex import volumex 30 | from datetime import datetime 31 | from tqdm import tqdm 32 | 33 | # 生成的视频数量 34 | NUM_VIDEOS = 5 35 | # 视频剪切时长(秒) 36 | CUT_DURATION = 2 37 | # 旋转 38 | my_angle = -0.1 39 | # 对比度 40 | my_contrast = -0.1 41 | # 是否开启镜像 42 | is_mirror = True 43 | 44 | # 文件夹路径 45 | videos_folder = '1080横屏视频文件夹/' # 选择不同文件夹根据需求 46 | music_folder = 'music/' 47 | output_folder = datetime.now().strftime('%m月%d日') 48 | 49 | # 创建输出文件夹 50 | os.makedirs(output_folder, exist_ok=True) 51 | 52 | """寻找音频文件""" 53 | def convert_to_mp3(): 54 | music_files = [file for file in os.listdir(music_folder) if file.endswith('.mp4') or file.endswith('.MP4') or file.endswith('.mov') or file.endswith('.MOV') or file.endswith('.mp3')] 55 | if music_files: 56 | # 读取并转换音频文件为mp3格式 57 | music_file = music_files[0] 58 | music_path = os.path.join(music_folder, music_file) 59 | music = AudioFileClip(music_path) 60 | music_mp3_path = os.path.join(music_folder, 'converted_music.mp3') 61 | music.write_audiofile(music_mp3_path) 62 | return music, music_mp3_path 63 | else: 64 | print("音频文件夹中未找到音频文件") 65 | return None, None 66 | 67 | """读取视频并进行剪切和去音频""" 68 | def process_videos(video_files, music, music_mp3_path): 69 | video_clips = [] 70 | for video_file in tqdm(video_files, desc='处理视频-切分碎片'): 71 | video_path = os.path.join(videos_folder, video_file) 72 | clip = VideoFileClip(video_path) 73 | duration = int(clip.duration) 74 | cut_clips = [clip.subclip(i, i + CUT_DURATION).without_audio() for i in range(0, duration, CUT_DURATION)] 75 | video_clips.extend(cut_clips) 76 | 77 | n = len(video_clips) 78 | for i in tqdm(range(n - 1, 0, -1), desc='洗牌视频-随机组合'): 79 | j = random.randint(0, i) 80 | video_clips[i], video_clips[j] = video_clips[j], video_clips[i] 81 | 82 | videos = [] 83 | for i in tqdm(range(NUM_VIDEOS), desc='生成视频-进行合并'): 84 | selected_clips = random.choices(video_clips, k=len(video_clips)) 85 | selected_clips.sort(key=lambda x: video_clips.index(x)) 86 | final_clip = concatenate_videoclips(selected_clips) 87 | final_clip = final_clip.set_audio(AudioFileClip(music_mp3_path)) 88 | 89 | if final_clip.duration > music.duration: 90 | final_clip = final_clip.subclip(0, music.duration) 91 | 92 | videos.append(final_clip) 93 | 94 | return videos 95 | 96 | """调节视频""" 97 | def apply_video_effects(videos): 98 | processed_videos = [] 99 | for video in tqdm(videos, desc='调节视频-镜像滤镜旋转'): 100 | if is_mirror: 101 | video = video.fx(vfx.mirror_x) 102 | video = video.fx(vfx.rotate, angle=my_angle) 103 | video = video.fx(vfx.lum_contrast, contrast=my_contrast) 104 | processed_videos.append(video) 105 | 106 | return processed_videos 107 | 108 | """保存视频""" 109 | def save_videos(processed_videos): 110 | for i, video in tqdm(enumerate(processed_videos), desc='保存视频-输出mp4'): 111 | output_path = os.path.join(output_folder, f'final_video_{i}.mp4') 112 | video.write_videofile(output_path, codec='libx264', fps=24, audio_codec='aac') 113 | 114 | # 获取音频文件信息 115 | music, music_mp3_path = convert_to_mp3() 116 | 117 | # 如果有音频文件,则处理视频 118 | if music: 119 | video_files = [file for file in os.listdir(videos_folder) if file.endswith('.mp4') or file.endswith('.MP4') or file.endswith('.mov') or file.endswith('.MOV')] 120 | videos = process_videos(video_files, music, music_mp3_path) 121 | processed_videos = apply_video_effects(videos) 122 | save_videos(processed_videos) 123 | 124 | 125 | 126 | 127 | # 备份 128 | 129 | # import os 130 | # import random 131 | # from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, vfx 132 | # from moviepy.audio.fx.volumex import volumex 133 | # from datetime import datetime 134 | # # tqdm显示进度条用的 135 | # from tqdm import tqdm 136 | 137 | 138 | # # 生成的视频数量 139 | # NUM_VIDEOS = 5 140 | # # 视频剪切时长(秒) 141 | # CUT_DURATION = 2 142 | # # 旋转 143 | # my_angle = -0.1 144 | # # 对比度 145 | # my_contrat = -0.1 146 | # # 是否开启镜像 147 | # is_mirror = True 148 | 149 | # # 文件夹路径 150 | # # videos_folder = '1920竖屏视频文件夹/' 151 | # videos_folder = '1080横屏视频文件夹/' 152 | # # videos_folder = 'videos/' 153 | # music_folder = 'music/' 154 | # output_folder = datetime.now().strftime('%m月%d日') 155 | 156 | # # 创建输出文件夹 157 | # os.makedirs(output_folder, exist_ok=True) 158 | 159 | # # 寻找视频和音频文件 160 | # video_files = [file for file in os.listdir(videos_folder) if file.endswith('.mp4') or file.endswith('.MP4') or file.endswith('.mov') or file.endswith('.MOV')] 161 | # music_files = [file for file in os.listdir(music_folder) if file.endswith('.mp4') or file.endswith('.MP4') or file.endswith('.mov') or file.endswith('.MOV') or file.endswith('.mp3')] 162 | 163 | 164 | # # 如果音频文件夹中存在音频文件,则处理视频 165 | # if music_files: 166 | # # 读取并转换音频文件为mp3格式 167 | # music_file = music_files[0] 168 | # music_path = os.path.join(music_folder, music_file) 169 | # music = AudioFileClip(music_path) 170 | # music_mp3_path = os.path.join(music_folder, 'converted_music.mp3') 171 | # music.write_audiofile(music_mp3_path) 172 | 173 | # # 读取视频并进行剪切和去音频 174 | # video_clips = [] 175 | # for video_file in tqdm(video_files, desc='处理视频-切分碎片'): 176 | # video_path = os.path.join(videos_folder, video_file) 177 | # clip = VideoFileClip(video_path) 178 | # duration = int(clip.duration) 179 | # cut_clips = [clip.subclip(i, i + CUT_DURATION).without_audio() for i in range(0, duration, CUT_DURATION)] 180 | # video_clips.extend(cut_clips) 181 | 182 | # # # 打乱视频顺序1 183 | # # random.shuffle(video_clips) 184 | 185 | # # # 打乱视频顺序2 - Fisher-Yates 算法打乱视频顺序 186 | # n = len(video_clips) 187 | # for i in tqdm(range(n - 1, 0, -1), desc='洗牌视频-随机组合'): 188 | # j = random.randint(0, i) 189 | # video_clips[i], video_clips[j] = video_clips[j], video_clips[i] 190 | 191 | 192 | # # 创建5个长视频 193 | # videos = [] 194 | # for i in tqdm(range(NUM_VIDEOS), desc='生成视频-进行合并'): 195 | # selected_clips = random.choices(video_clips, k=len(video_clips)) 196 | # selected_clips.sort(key=lambda x: video_clips.index(x)) 197 | # final_clip = concatenate_videoclips(selected_clips) 198 | # final_clip = final_clip.set_audio(AudioFileClip(music_mp3_path)) 199 | 200 | # # 裁剪多余部分 201 | # if final_clip.duration > music.duration: 202 | # final_clip = final_clip.subclip(0, music.duration) 203 | 204 | # videos.append(final_clip) 205 | 206 | # # 保存水平镜像翻转后的视频到新文件夹 207 | # for i, video in tqdm(enumerate(videos), desc='保存视频-镜像滤镜旋转'): 208 | # """ 209 | # 视频调节 - 开始 210 | # """ 211 | # # 镜像 212 | # if is_mirror: 213 | # video = video.fx(vfx.mirror_x) 214 | # # 旋转10度 215 | # video = video.fx(vfx.rotate, angle=my_angle) 216 | # # 增加对比度10 217 | # video = video.fx(vfx.lum_contrast, contrast=my_contrat) 218 | # """ 219 | # 视频调节 - 结束 220 | # """ 221 | # # 路径 222 | # output_path = os.path.join(output_folder, f'final_video_{i}.mp4') 223 | # # 导出 224 | # video.write_videofile(output_path, codec='libx264', fps=24, audio_codec='aac') 225 | # else: 226 | # print("音频文件夹中未找到音频文件") -------------------------------------------------------------------------------- /sizeVideosGroup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from moviepy.editor import VideoFileClip 3 | 4 | # 定义文件夹路径和尺寸信息 5 | videos_folder = 'videos/' 6 | output_folder_1920_vertical = '1920竖屏视频文件夹/' 7 | output_folder_1080_horizontal = '1080横屏视频文件夹/' 8 | output_folder_other = '其他尺寸视频文件夹/' 9 | 10 | # 创建输出文件夹 11 | os.makedirs(output_folder_1920_vertical, exist_ok=True) 12 | os.makedirs(output_folder_1080_horizontal, exist_ok=True) 13 | os.makedirs(output_folder_other, exist_ok=True) 14 | 15 | # 寻找视频文件 16 | video_files = [file for file in os.listdir(videos_folder) if file.lower().endswith('.mp4') or file.lower().endswith('.mov')] 17 | 18 | # 分类处理视频文件 19 | for video_file in video_files: 20 | video_path = os.path.join(videos_folder, video_file) 21 | clip = VideoFileClip(video_path) 22 | width, height = clip.size 23 | 24 | # 根据尺寸放入不同的文件夹 25 | if width == 1920 and height == 1080: 26 | output_path = os.path.join(output_folder_1920_vertical, video_file) 27 | os.rename(video_path, output_path) 28 | elif width == 1080 and height == 1920: 29 | output_path = os.path.join(output_folder_1080_horizontal, video_file) 30 | os.rename(video_path, output_path) 31 | else: 32 | output_path = os.path.join(output_folder_other, video_file) 33 | os.rename(video_path, output_path) 34 | 35 | print("视频分类完成!") 36 | -------------------------------------------------------------------------------- /videos/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BassZou/pyToVideo2/f39b6847d5c01d1a919a953517a153274c5ba7ef/videos/.DS_Store --------------------------------------------------------------------------------