feat: 添加视频处理相关脚本
- 新增 VideoZip.py: 实现了一个简单的图形界面,用于选择文件夹并进行视频转换 - 新增 compress.py: 包含视频转换和文件列表获取功能 - 新增 video_image.py: 实现了视频截图和视频转换功能 - 在 .gitignore 中添加 .DS_Store 文件忽略项
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -160,3 +160,4 @@ cython_debug/
|
|||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
66
VideoZip.py
Normal file
66
VideoZip.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
from tkinter import *
|
||||||
|
from tkinter import ttk, filedialog, messagebox
|
||||||
|
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess as sp
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
# 使用原生的Tk替代TkinterDnD的Tk
|
||||||
|
root = Tk()
|
||||||
|
frm = ttk.Frame(root, padding=10)
|
||||||
|
frm.grid()
|
||||||
|
|
||||||
|
folder_path = ''
|
||||||
|
# 1. 拖拽事件触发区域(改为文件夹选择按钮)
|
||||||
|
def select_folder():
|
||||||
|
global folder_path
|
||||||
|
folder_path = filedialog.askdirectory()
|
||||||
|
if folder_path:
|
||||||
|
path_listbox.insert(END, folder_path)
|
||||||
|
log_text.insert(END, f"添加文件夹: {folder_path}\n")
|
||||||
|
|
||||||
|
folder_button = ttk.Button(frm, text="选择文件夹", command=select_folder)
|
||||||
|
folder_button.grid(column=0, row=0, columnspan=3)
|
||||||
|
|
||||||
|
# 2. 文件和文件夹路径的列表
|
||||||
|
path_listbox = Listbox(frm, height=10, width=50)
|
||||||
|
path_listbox.grid(column=0, row=1, columnspan=3)
|
||||||
|
|
||||||
|
# 3. 进度条
|
||||||
|
progress = ttk.Progressbar(frm, orient=HORIZONTAL, length=400, mode='determinate')
|
||||||
|
progress.grid(column=0, row=2, columnspan=3)
|
||||||
|
|
||||||
|
# 4. 日志输出窗口
|
||||||
|
log_text = Text(frm, height=10, width=50)
|
||||||
|
log_text.grid(column=0, row=3, columnspan=3)
|
||||||
|
|
||||||
|
|
||||||
|
def start_conversion():
|
||||||
|
# 开始转换的逻辑
|
||||||
|
log_text.insert(END, "开始转换...\n")
|
||||||
|
progress['value'] = 0
|
||||||
|
for i in range(100):
|
||||||
|
progress['value'] += 1
|
||||||
|
root.update_idletasks()
|
||||||
|
time.sleep(0.05) # 模拟转换进度
|
||||||
|
log_text.insert(END, "转换完成\n")
|
||||||
|
|
||||||
|
global folder_path
|
||||||
|
list = os.listdir(folder_path) # 列出文件夹下所有的目录与文件
|
||||||
|
for i in range(0, len(list)):
|
||||||
|
print("----------------------------")
|
||||||
|
path = os.path.join(args[1], list[i])
|
||||||
|
if os.path.isfile(path):
|
||||||
|
print("FileName: " + list[i])
|
||||||
|
matcher1 = re.search(pattern2, list[i])
|
||||||
|
if matcher1 != None:
|
||||||
|
# get_image_from_video(args[1], list[i])
|
||||||
|
conver_video(args[1], list[i])
|
||||||
|
|
||||||
|
# 5. 按钮
|
||||||
|
ttk.Button(frm, text="开始转换", command=start_conversion).grid(column=1, row=4)
|
||||||
|
ttk.Button(frm, text="关闭", command=root.destroy).grid(column=2, row=4)
|
||||||
|
|
||||||
|
root.mainloop()
|
||||||
46
compress.py
Normal file
46
compress.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def convert(from_file, to_file):
|
||||||
|
command = (
|
||||||
|
"ffmpeg -i "
|
||||||
|
+ from_file
|
||||||
|
+ " -s 1280x720 -b:v 3M -vcodec h264 -r 29.97 -acodec aac -ab 48k -ac 2 "
|
||||||
|
+ to_file
|
||||||
|
)
|
||||||
|
|
||||||
|
os.system(command)
|
||||||
|
|
||||||
|
|
||||||
|
def get_file_list(fpath):
|
||||||
|
# 定义视频文件的常见扩展名
|
||||||
|
video_extensions = {".mp4", ".m4v", ".avi", ".mov", ".mkv", ".flv", ".wmv", ".webm"}
|
||||||
|
|
||||||
|
# 用来存储视频文件名的列表
|
||||||
|
video_files = []
|
||||||
|
|
||||||
|
dir_path = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
base_path = dir_path + "/"
|
||||||
|
print(dir_path)
|
||||||
|
|
||||||
|
# 遍历指定目录
|
||||||
|
for root, dirs, files in os.walk(fpath):
|
||||||
|
for file in files:
|
||||||
|
# 获取文件扩展名
|
||||||
|
_, ext = os.path.splitext(file)
|
||||||
|
# 如果扩展名是视频格式,则添加到列表中
|
||||||
|
if ext.lower() in video_extensions:
|
||||||
|
from_file = "./in/"+file
|
||||||
|
name, ext = os.path.splitext(file)
|
||||||
|
to_file = "z_" + name + ".mp4"
|
||||||
|
convert(from_file, to_file)
|
||||||
|
# video_files.append(dir_path + "/" + file)
|
||||||
|
|
||||||
|
# return video_files
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# 使用示例
|
||||||
|
directory = "./" # 请替换为你想要遍历的文件夹路径
|
||||||
|
get_file_list(directory)
|
||||||
|
|
||||||
74
video_image.py
Normal file
74
video_image.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
# 视频截图的python文件
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess as sp
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
p1 = '[\u4e00-\u9fa5_a-zA-Z0-9]*.mp4'
|
||||||
|
p2 = '([\u4e00-\u9fa5_a-zA-Z0-9]*.)*((mp4)|(MP4)|(mov)|(MOV)|(m4v)|(M4V)|(mpg)|(MPG))'
|
||||||
|
pattern1 = re.compile(p1)
|
||||||
|
pattern2 = re.compile(p2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
runpath = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
#sys.path[0]
|
||||||
|
|
||||||
|
def runCommend(commend):
|
||||||
|
f = os.popen(commend)
|
||||||
|
for i in f.readlines():
|
||||||
|
pass
|
||||||
|
# print(i)
|
||||||
|
print('run complite')
|
||||||
|
|
||||||
|
def get_image_from_video(file_path, file_name):
|
||||||
|
print('into \" get_image_from_video \" function')
|
||||||
|
print(file_name, file_path)
|
||||||
|
video_path = file_path + '/'
|
||||||
|
video_name = file_name
|
||||||
|
image_path = file_path + '/'
|
||||||
|
image_name = file_name[:-4] + '.jpg'
|
||||||
|
comm = 'ffmpeg -i ' + video_path + video_name + ' -ss 1 -f image2 ' + image_path + image_name
|
||||||
|
runCommend(comm)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def conver_video(file_path, file_name):
|
||||||
|
input_path = file_path + '/'
|
||||||
|
input_name = file_name
|
||||||
|
output_path = file_path + '/'
|
||||||
|
output_name = ""
|
||||||
|
sp_input_name = file_name.split('.')
|
||||||
|
ext_pos = len(sp_input_name) - 1
|
||||||
|
print('---------------' + sp_input_name[ext_pos])
|
||||||
|
output_name = 'z_' + file_name[:-4] + '.mp4'
|
||||||
|
# if sp_input_name[ext_pos] == 'mp4':
|
||||||
|
# output_name = file_name[:-4] + '_z.mp4'
|
||||||
|
# else:
|
||||||
|
# output_name = file_name[:-4] + '.mp4'
|
||||||
|
|
||||||
|
comm = 'ffmpeg -i \'' + input_path + input_name + '\' -r 60 -vcodec libx264 -tune film -b:v 6M -fs 200MB -s 1920*1080 \'' + output_path + output_name + '\''
|
||||||
|
# comm = 'ffmpeg -i \'' + input_path + input_name + '\' -vcodec libx264 \'' + output_path + output_name + '\''
|
||||||
|
runCommend(comm)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = sys.argv
|
||||||
|
|
||||||
|
list = os.listdir(args[1]) # 列出文件夹下所有的目录与文件
|
||||||
|
for i in range(0, len(list)):
|
||||||
|
print('----------------------------')
|
||||||
|
path = os.path.join(args[1], list[i])
|
||||||
|
if os.path.isfile(path):
|
||||||
|
print('FileName: ' + list[i])
|
||||||
|
matcher1 = re.search(pattern2, list[i])
|
||||||
|
if matcher1 != None:
|
||||||
|
# get_image_from_video(args[1], list[i])
|
||||||
|
conver_video(args[1], list[i])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user