视频横屏批量改竖屏
本帖最后由 rootcup 于 2024-9-23 14:37 编辑# 横版视频改竖版 v1.1
软件运行环境:Win10 及以上,只支持调用 CPU 处理。
## 软件功能
批量将横屏视频缩放为竖屏,并随机从壁纸文件夹中选择壁纸作为背景。导出的视频保持竖屏 1080x1920 分辨率,输出到视频文件夹中的 output 文件夹。
![](https://img.meituan.net/csc/9e320bce1158741b3d4505aad0e75777853847.jpg)
![](https://img.meituan.net/csc/851662e374622a25add03a9e9ccb5fb3605907.jpg)
![效果图](https://img.meituan.net/csc/2048d5ee95f7ae9e9cc12fca0b7d3f1c537154.jpg)
## 安装说明
在运行之前,你需要先安装 ImageMagick,MoviePy 会调用它进行处理。同时,ImageMagick 中已经包含 ffmpeg
## 代码
```
import os
import random
import tkinter as tk
from ctypes import windll
from tkinter import filedialog, messagebox
from moviepy.editor import VideoFileClip, ImageClip, CompositeVideoClip
from PIL import Image
import concurrent.futures
import threading
# 设置应用程序的默认DPI,确保高DPI屏幕上的显示效果
windll.shcore.SetProcessDpiAwareness(2)
# 创建主窗口
root = tk.Tk()
root.title('横版视频改竖版 v1.1')
root.geometry('600x650')
root.configure(bg='#f0f0f0')
root.attributes("-alpha", 0.9)# 设置窗口为半透明
# 选择视频文件夹和壁纸文件夹的变量
video_folder = ""
wallpaper_folder = ""
output_folder = ""
# 默认码率
bitrate_value = "1800k"
# 选择视频文件夹
def select_video_folder():
global video_folder
video_folder = filedialog.askdirectory()
if video_folder:
text_video_paths.delete(1.0, tk.END)
video_files =
for video_file in video_files:
text_video_paths.insert(tk.END, video_file + '\n', "tag_1")
# 选择壁纸文件夹
def select_wallpaper_folder():
global wallpaper_folder
wallpaper_folder = filedialog.askdirectory()
if wallpaper_folder:
print("壁纸文件夹选择:", wallpaper_folder)
# 裁剪和缩放壁纸为1080x1920,保持背景铺满无黑边,并覆盖原文件
def prepare_wallpaper(wallpaper_path):
with Image.open(wallpaper_path) as img:
original_width, original_height = img.size
if original_width == 1080 and original_height == 1920:
return wallpaper_path
scale_ratio = max(1080 / original_width, 1920 / original_height)
new_width = int(original_width * scale_ratio)
new_height = int(original_height * scale_ratio)
img = img.resize((new_width, new_height), Image.LANCZOS)
left = (new_width - 1080) // 2
top = (new_height - 1920) // 2
right = left + 1080
bottom = top + 1920
img = img.crop((left, top, right, bottom))
img.save(wallpaper_path)
return wallpaper_path
# 处理视频的线程函数
def process_videos_thread():
process_videos()
# 处理视频
def process_videos():
global output_folder
if not video_folder or not wallpaper_folder:
messagebox.showerror("错误", "请先选择视频和壁纸文件夹。")
return
output_folder = os.path.join(video_folder, "output")
os.makedirs(output_folder, exist_ok=True)
video_files =
# 根据视频文件数量设置线程池大小
num_files = len(video_files)
if num_files < 2:
num_threads = 1
elif 2 <= num_files < 4:
num_threads = 2
else:
num_threads = 4
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = {executor.submit(process_single_video, video_file, output_folder): video_file for video_file in video_files}
for future in concurrent.futures.as_completed(futures):
video_file = futures
try:
future.result()
except Exception as e:
messagebox.showerror("处理错误", f"处理视频 {video_file} 时出错: {e}")
text_video_paths.insert(tk.END, '已处理完成全部\n', "tag_1")
# 处理单个视频
def process_single_video(video_file, output_folder):
video_path = os.path.join(video_folder, video_file)
with VideoFileClip(video_path) as video:
if video.size > video.size:# 判断是否是横版
wallpaper_file = random.choice(os.listdir(wallpaper_folder))
wallpaper_path = os.path.join(wallpaper_folder, wallpaper_file)
prepared_wallpaper_path = prepare_wallpaper(wallpaper_path)
with ImageClip(prepared_wallpaper_path) as wallpaper:
video_resized = video.resize(width=1080)
x_position = (1080 - video_resized.size) / 2
y_position = (1920 - video_resized.size) / 2
final_clip = CompositeVideoClip([
wallpaper.set_duration(video.duration).set_position("center"),
video_resized.set_position((x_position, y_position))
])
output_path = os.path.join(output_folder, f"{os.path.splitext(video_file)}_竖.mp4")
# 从输入框获取码率,如果为空则使用默认码率
selected_bitrate = bitrate_entry.get() or bitrate_value
final_clip.write_videofile(output_path, codec='libx264', bitrate=selected_bitrate)
text_video_paths.insert(tk.END, f'已处理完成{video_file}\n', "tag_2")
# 打开输出文件夹
def open_output_folder():
global output_folder
if os.path.exists(output_folder):
os.startfile(output_folder)
# 创建按钮布局
button_frame1 = tk.Frame(root, bg='#f0f0f0')
button_frame1.pack(pady=5)
# 第一行按钮:选择视频和壁纸文件夹
btn_select_video = tk.Button(button_frame1, text='选择视频文件夹', bg='#4CAF50', fg='white', command=select_video_folder, font=('simhei.ttf', 12, 'bold'), relief=tk.FLAT)
btn_select_video.pack(side=tk.LEFT, padx=10)
btn_select_wallpaper = tk.Button(button_frame1, text='选择壁纸文件夹', bg='#4CAF50', fg='white', command=select_wallpaper_folder, font=('simhei.ttf', 12, 'bold'), relief=tk.FLAT)
btn_select_wallpaper.pack(side=tk.LEFT, padx=10)
# 第二行按钮:开始处理和打开输出文件夹
button_frame2 = tk.Frame(root, bg='#f0f0f0')
button_frame2.pack(pady=5)
btn_start = tk.Button(button_frame2, text='点击此开始处理', bg='#2196F3', fg='white', command=lambda: threading.Thread(target=process_videos_thread).start(), font=('simhei.ttf', 12, 'bold'), relief=tk.FLAT)
btn_start.pack(side=tk.LEFT, padx=10)
btn_open_folder = tk.Button(button_frame2, text='打开输出文件夹', bg='#FFC107', fg='white', command=open_output_folder, font=('simhei.ttf', 12, 'bold'), relief=tk.FLAT)
btn_open_folder.pack(side=tk.LEFT, padx=10)
# 第三行:码率选框
button_frame3 = tk.Frame(root, bg='#f0f0f0')
button_frame3.pack(pady=5)
# 增加码率输入框
bitrate_label = tk.Label(button_frame3, text="导出码率:", bg='#f0f0f0', font=('simhei.ttf', 12))
bitrate_label.pack(side=tk.LEFT, padx=10)
bitrate_entry = tk.Entry(button_frame3, width=10, font=('simhei.ttf', 12))
bitrate_entry.pack(side=tk.LEFT, padx=10)
bitrate_entry.insert(0, bitrate_value)# 默认值
readme = '''
2024.09.21 吾爱破解 rootcup
批量将竖版视频缩放成横屏,背景图片随机抽取选择文件夹内的图片
使用方法:先选中要批量处理的视频文件夹,再选择背景壁纸文件夹
导出的视频在选择的视频文件夹 output里
2024.09.23 更新:增加选框,可自定义填写导出码率
'''
# 视频路径显示文本框
text_video_paths = tk.Text(root, height=30, width=80, bg='#f0f0f0', font=('simhei.ttf', 10))
text_video_paths.pack(padx=5, pady=10)
text_video_paths.tag_config("tag_1", foreground="green")
text_video_paths.tag_config("tag_2", foreground="blue")
text_video_paths.insert(tk.END, readme, "tag_1")
# 运行主循环
root.mainloop()
```
#### 2024.09.23 更新:增加选框,可自定义填写导出码率
![](https://img.meituan.net/csc/0ba64fcae7ca5e8b72c6600ef430c8fc134769.jpg)
使用PyInstaller打包成exe文件
### 打包命令:
```
"C:\Users\...\横转竖视频\venv\Scripts\python.exe" -m PyInstaller --onefile -F"C:\Users\...\横转竖视频\横版视频改竖版1.1.py"
```
打包好的exe下载地址
https://wwmd.lanzouv.com/b03fkjyc5a
密码52pj 谢谢作者,下面有这个工具箱也挺实用。
基于 FFmpeg 开发编译的视频剪辑工具箱V1.1
https://pan.lanzoub.com/b0f2qcpfg WXJYXLWMH 发表于 2024-9-21 18:33
支持原创作品 谢谢分享
记得以前绿色版旧版内ffmpeg 后来取消了 安装版不知还有没有内置ffmpeg
ffmpeg本 ...
对的,安装版本是自带ffmpeg的,装了以后就不用环境变量指定fmpeg了。就是套个图形界面,让操作图形化,方便不熟悉的人使用 我想把竖屏改为横屏,有办法吗 支持原创作品 谢谢分享
记得以前绿色版旧版内ffmpeg 后来取消了 安装版不知还有没有内置ffmpeg
ffmpeg本身就支持视频横竖处理吧 好用的软件,支持一下,下载了 这个对短视频创作者应该很不错 好东西,收藏了,感谢分享 视频横屏批量改竖屏,感谢楼主分享哈! 不错的工具,感谢楼主原创分享!学习