安装moviepy库
from moviepy.editor import *
import os,time
import shutil
file_path = r'本地路径'
def cutfilm(softlist):
for fromfilm in softlist:
my_clip = VideoFileClip(fromfilm).subclip(90,-85) #视频切割,保留从90s到结束前85秒
newname = fromfilm.replace('.FLV','.mp4') #新保存视频名称,
my_clip.write_videofile(newname) #开始将剪切视频生成文件
print('----------保存成功-------------\n',newname)
try:
time.sleep(30)
shutil.move(fromfilm,r'本地路径') # 移动已经转换好的文件
time.sleep(30)
except:
print('文件'+fromfilm+'already exists')
try:
time.sleep(30)
os.remove(fromfilm) # 删除已经转换好的文件
except PermissionError:
print('另一个程序正在使用此文件,进程无法访问。')
#获取本地路径下的文件列表,只读取flv格式的
def get_file_list(file_path):
softlist = []
dir_list = os.listdir(file_path)
if not dir_list:
pass
else:
#按创建时间进行排序获取
dir_list = sorted(dir_list,key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
for i in dir_list:
if '.FLV' in i:
filename = file_path+i
creattime = time.ctime(os.path.getctime(filename))
print(filename,creattime)
softlist.append(filename)
return softlist
def main():
softlist = get_file_list(file_path)
cutfilm(softlist)
if __name__ == '__main__':
main()