import os import shutil from moviepy.editor import VideoFileClip # 获取所有文件 def getAllFiles(fire_dir,houzui=".mp4"): filepath_list = [] for root,folder_names,file_names in os.walk(fire_dir): for file_name in file_names: file_path = root+os.sep+file_name if file_path.endswith(houzui): #判断文件是否以指定后缀结尾 filepath_list.append(file_path) print(file_path) print(filepath_list) return filepath_list #获取指定文件夹下所有MP4文件的时长 def getFileNames(path,houzui=".mp4"): return [os.path.join(path,f) for f in os.listdir(path) if f.endswith(houzui)] #获取视频时长 def getTimeLong(videoFile): clip = VideoFileClip(videoFile) sicahng = clip.duration print(sicahng) # seconds clip.close() return sicahng #获取视频分辨率 def getKuanAndGao(videoFile): clip = VideoFileClip(videoFile) fenbianlv = clip.size # 获取分辨率 print(fenbianlv) clip.close() return fenbianlv #如果不存在就创建 def createDir(file_dir): # 如果不存在文件夹,就创建 if not os.path.isdir(file_dir): os.mkdir(file_dir) #移动文件到指定文件夹下的yiqiege目录 def moveFile(zhidingmulu,file_path): # # 将已经切割的移动到已经切割文件夹gen_dir # zhumulu = str(gen_dir).strip(gen_dir.split("\\")[-1]) # # 创建文件夹 # qiegewenjianjia = r"%s\yiqiege" % zhumulu # print("切割文件夹名字:") # print(qiegewenjianjia) # createDir(qiegewenjianjia) try: # 移动文件到文件夹目录中 shutil.move(file_path, zhidingmulu) print("移动文件%s到已切割文件夹" % file_path) except Exception as e: print("移动出错:%s" % str(e)) #获取指定路径下MP4文件的时长 def mp4FIleTime(path,houzui=".mp4"): file_list = getFileNames(path,houzui) for one in file_list: try: one_shichang = getTimeLong(one) print("%s文件的时长为 %s 秒"%(one,one_shichang)) zhengshu = int(one_shichang) zhengshijia1 = zhengshu+1 #移动到指定时间的文件夹 xiaoyu1miao = "%s/大于%s秒小于%s秒"% (path,str(zhengshu),str(zhengshijia1)) createDir(file_dir=xiaoyu1miao) moveFile(zhidingmulu=xiaoyu1miao, file_path=one) except Exception as e: print("%s文件的时长失败,原因:%s"%(one,str(e))) #移动到指定时间的文件夹 jieshishibai = "%s/解析失败"% path createDir(file_dir=jieshishibai) moveFile(zhidingmulu=jieshishibai, file_path=one) continue #获取指定路径下MP4文件的分辨率 def mp4FIleFenBianLv(path,houzui=".mp4"): file_list = getAllFiles(path,houzui) for one in file_list: try: one_fenbianlv = getKuanAndGao(one) print("%s文件的分辨率为 %s "%(one,one_fenbianlv)) #移动到指定时间的文件夹 xiaoyu1miao = "%s/%sx%s"% (path,one_fenbianlv[0],one_fenbianlv[1]) createDir(file_dir=xiaoyu1miao) moveFile(zhidingmulu=xiaoyu1miao, file_path=one) except Exception as e: print("%s文件的分辨率失败,原因:%s"%(one,str(e))) #移动到指定时间的文件夹 jieshishibai = "%s/解析失败"% path createDir(file_dir=jieshishibai) moveFile(zhidingmulu=jieshishibai, file_path=one) continue if __name__ == '__main__': filePath =r"F:\BaiduNetdiskDownload" mp4FIleFenBianLv(path=filePath,houzui=".mp4")
标签:python,分辨率,houzui,文件夹,file,print,path,dir From: https://www.cnblogs.com/jingzaixin/p/18121844