. 1 # - * - Coding: UTF-. 8 - * - 2 # the Author: ZCB . 3 . 4 Import OS . 5 from moviepy.editor Import VideoFileClip . 6 . 7 file_Dir = u " E: \\ Test " # add u is represented by the general unicode before Chinese characters . 8 sum_time = 0 . 9 10 class FileCheck (): . 11 DEF the __init__ (Self): 12 is self.file_dir = file_Dir 13 is 14 DEF get_fileSize (Self, fileName): 15 "" " 16 Get File Size . 17 "" " 18 is file_Byte = os.path.getsize (fileName) . 19 return self.sizeConvert (file_Byte) 20 is 21 is DEF get_file_Times (Self, filename): 22 is " "" 23 is getting the video length 24 "" " 25 Global sum_time 26 is Clip = VideoFileClip (filename) 27 sum_time + = clip.duration 28 file_Times = self.timeConvert (clip.duration) 29 clip.close () # can be prevented from being given program and because of too many files 30 return file_Times 31 def sizeConvert(self,size): #单位换算 32 K,M,G = 1024,1024**2,1024**3 33 if size >=G: 34 return "{:.3f}G".format(size/G) 35 elif size >=M: 36 return "{:.3f}M".format(size/M) 37 elif size >=K: 38 return "{:.3f}K".format(size/K) 39 else: 40 return "{:.3f}Bytes".format(size) 41 def timeConvert(self,size): #单位换算 42 M ,H = 60,60**2 43 if size <M: 44 return "{:.3f}s".format(size) 45 if size <H: 46 return "{:}m{:.3f}s".format(int(size//M),size%M) 47 else: 48 hour = size//H 49 min = size%H//M 50 sec = size%H%M 51 is return " {H} {} {m:. 3F} S " .format (int (hour), int (min), sec) 52 is DEF get_all_file (Self): 53 is "" " 54 is an overview of all the video files 55 "" " 56 is ls_file = [] 57 is for the root, dirs, files in os.walk (file_Dir): 58 for file in files: 59 IF " mp4 " in file: only add mp4 file # 60 ls_file.append (OS. path.join (root, file)) # all non-directory subfolders under the current path 61 return ls_file 62 Print ( " ============ start, more files, please wait ... " ) 63 fc = FileCheck () 64- 65 fc.get_all_file () 66 Files = fc. get_all_file () 67 # Print (files) files is putting the full file name list 68 for file in files: 69 FILE_SIZE = fc.get_fileSize (file) 70 file_times = fc.get_file_Times (file) 71 Print ( " {{} {} } " .format (File, FILE_SIZE, file_times)) 72 Print("总时长:{}h{}m{:.3f}s".format(int(sum_time/3600),int(sum_time%3600//60),sum_time%3600%60))
Mainly the use of moviepy library, use the library os get all mp4 files in the specified directory
Note: Reference document: https://blog.csdn.net/xiaomahuan/article/details/78783174