使用说明书:视频分割合并工具
欢迎使用视频生成工具!本工具旨在帮助您将视频文件按照指定的规则分割并合并,以生成您所需的视频。
本程序还自带提高分辨率1920:1080,以及增加10db声音的功能
软件下载地址
https://github.com/cmdch2017/-python-
步骤1:选择视频文件
- 点击 “Select Multiple Input Files” 按钮,选择您要处理的视频文件。您可以同时选择多个视频文件。
- 选择的文件将会显示在下方的文件列表中。
步骤2:设置分割和合并规则
- 在 “Segment Interval (seconds):” 文本框中,输入希望分割视频的时间间隔(以秒为单位)。例如,若要每隔5分钟分割一次,输入300。
- 在 “Segment Duration (seconds):” 文本框中,输入每个分割片段的持续时间(以秒为单位)。例如,若希望每个片段为30秒,输入30。
- 在 “Start Time (seconds):” 文本框中,输入每个分割片段的起始时间(以秒为单位)。
- 在 “End Time (seconds):” 文本框中,输入每个分割片段的结束时间(以秒为单位)。
步骤3:生成视频
- 点击 “Generate Selected Videos” 按钮,工具将会按照您的设置生成分割后的视频。
- 在生成的过程中,工具将在状态栏中显示当前进度。
步骤4:保存生成的视频
- 生成完成后,工具将在指定的输出文件夹中保存生成的视频文件。
- 输出文件的命名规则为 “merged_output.mp4”,若有重名文件,将会自动添加编号。
步骤5:找到视频文件
- 生成的视频放在软件目录的output_videos文件夹下。
注意事项
- 本工具依赖于 FFmpeg 库进行视频处理,请确保您的电脑已安装 FFmpeg。
- 请确保您选择的视频文件格式为 MP4 格式。
联系我们
如有任何问题或建议,请随时联系我们:congminglst@163.com
或者评论区直接提问,如果对您有帮助,请点个赞谢谢
源代码
import subprocess
import os
import tkinter as tk
from tkinter import ttk, filedialog
from ttkthemes import ThemedStyle
def generate_videos(input_file, segment_interval, segment_duration, start_time, end_time):
output_folder = "output_videos"
os.makedirs(output_folder, exist_ok=True)
ffprobe_cmd = [
"ffprobe", "-v", "error", "-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1", input_file
]
total_duration = float(subprocess.check_output(ffprobe_cmd, universal_newlines=True))
merged_segments = []
for index, start_time in enumerate(range(start_time, int(total_duration), segment_interval), start=1):
end_time = start_time + segment_duration
output_segment = os.path.join(output_folder, f"segment_{index}.mp4")
merged_segments.append(output_segment)
cmd = [
"ffmpeg", "-ss", str(start_time), "-i", input_file, "-t", str(segment_duration),
"-vf", "scale=1920:1080", "-b:v", "2048k", "-c:v", "libx264",
"-c:a", "aac", "-b:a", "192k", "-af", "volume=10dB",
output_segment
]
subprocess.run(cmd)
# Construct the list of existing segment files for concatenation
valid_segments = [segment for segment in merged_segments if os.path.exists(segment)]
concat_list_path = os.path.join(output_folder, "concat_list.txt")
with open(concat_list_path, "w") as f:
for segment in valid_segments:
f.write(f"file '{os.path.basename(segment)}'\n")
merged_output_base = "merged_output"
index = 1
merged_output = os.path.join(output_folder, f"{merged_output_base}.mp4")
while os.path.exists(merged_output):
merged_output = os.path.join(output_folder, f"{merged_output_base}{index}.mp4")
index += 1
concat_cmd = ["ffmpeg", "-f", "concat", "-safe", "0", "-i", concat_list_path, "-c", "copy", merged_output]
subprocess.run(concat_cmd)
for segment in merged_segments:
os.remove(segment)
os.remove(concat_list_path)
return merged_output
def select_input_files():
input_files = filedialog.askopenfilenames(filetypes=[("Video files", "*.mp4")])
selected_files_listbox.delete(0, tk.END)
for file in input_files:
selected_files_listbox.insert(tk.END, file)
def generate_selected_videos():
segment_interval = int(segment_interval_var.get())
segment_duration = int(segment_duration_var.get())
start_time = int(start_time_var.get())
end_time = int(end_time_var.get())
selected_files = selected_files_listbox.get(0, tk.END)
total_videos = len(selected_files)
for index, input_file in enumerate(selected_files, start=1):
status_label.config(text=f"Generating video {index} of {total_videos}")
root.update_idletasks()
output_file = generate_videos(input_file, segment_interval, segment_duration, start_time, end_time)
status_label.config(text=f"Generating video {index + 1} of {total_videos}")
root.update_idletasks()
status_label.config(text="All videos generated. Click OK to continue.")
ok_button.pack(fill=tk.BOTH, padx=10, pady=(10, 0))
selected_files_listbox.delete(0, tk.END)
def close_app():
root.destroy()
root = tk.Tk()
root.title("Video Generation Tool")
style = ThemedStyle(root)
style.set_theme("equilux")
style.configure("TFrame", background="#e0e0e0")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 400
window_height = 700
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")
select_multiple_button = ttk.Button(root, text="Select Multiple Input Files", command=select_input_files)
select_multiple_button.pack(fill=tk.BOTH, padx=10)
selected_files_listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
selected_files_listbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
segment_interval_label = ttk.Label(root, text="Segment Interval (seconds):")
segment_interval_label.pack(fill=tk.BOTH, padx=10, pady=(10, 0))
segment_interval_var = tk.StringVar(value="300")
segment_interval_entry = ttk.Entry(root, textvariable=segment_interval_var)
segment_interval_entry.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
segment_duration_label = ttk.Label(root, text="Segment Duration (seconds):")
segment_duration_label.pack(fill=tk.BOTH, padx=10, pady=(10, 0))
segment_duration_var = tk.StringVar(value="30")
segment_duration_entry = ttk.Entry(root, textvariable=segment_duration_var)
segment_duration_entry.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
start_time_label = ttk.Label(root, text="trimming video duration from the start (seconds):")
start_time_label.pack(fill=tk.BOTH, padx=10, pady=(10, 0))
start_time_var = tk.StringVar(value="0")
start_time_entry = ttk.Entry(root, textvariable=start_time_var)
start_time_entry.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
end_time_label = ttk.Label(root, text="trimming video duration from the end (seconds):")
end_time_label.pack(fill=tk.BOTH, padx=10, pady=(10, 0))
end_time_var = tk.StringVar(value="0")
end_time_entry = ttk.Entry(root, textvariable=end_time_var)
end_time_entry.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
generate_selected_button = ttk.Button(root, text="Generate Selected Videos", command=generate_selected_videos)
generate_selected_button.pack(fill=tk.BOTH, padx=10, pady=(10, 0))
status_label = ttk.Label(root, text="", foreground="blue")
status_label.pack(fill=tk.BOTH, padx=10, pady=10)
ok_button = ttk.Button(root, text="OK", command=close_app)
root.protocol("WM_DELETE_WINDOW", close_app)
root.mainloop()