本来以为每多一个线程,速度会提升,实际做了之后,发现速度并没有提升多少,线程多了之后(大于3个),反而会崩溃。
观察总结后,猜想应该是moviepy库太占内存,一个线程就70%的内存占用了,两个线程之后,就100%,速度自然提升不上去
而且多线程后,无法提供进度条,这个就很没有体验感了
import os
import datetime
from moviepy.editor import VideoFileClip
import time
def compute_video_time(path):
type = ('.flv','.mp4','.ts','.AVI','.avi','.rm','3GP','.rmvb','.asf','.mpg','.wmv','mkv','.vob')
filelist = []
for a, b, c in os.walk(path):
for name in c:
fname = os.path.join(a, name)
if fname.endswith(type):
filelist.append(fname)
print("------开始计算时间------")
ftime = 0.0
count = 0
for item in filelist:
try:
clip = VideoFileClip(item)
ftime += clip.duration
count = count + 1
print("已完成%.2f" %((count / filelist.__len__()) * 100)+"%" )
except:
print(item)
print("总共%d个文件" %count)
print("%d seconds: " % ftime,str(datetime.timedelta(seconds=ftime)))
if __name__ == "__main__":
time1 = time.time()
path = r"F:\XXX\XXX\XXX"
compute_video_time(path)
time2 = time.time()
print(time2 - time1)
import os
import datetime
from moviepy.editor import VideoFileClip
import threading
from openpyxl.chart.trendline import Trendline
from asyncio.tasks import sleep
import time
def compute_video_time(path):
type = ('.flv','.mp4','.ts','.AVI','.avi','.rm','3GP','.rmvb','.asf','.mpg','.wmv','mkv','.vob')
filelist = []
for a, b, c in os.walk(path):
for name in c:
fname = os.path.join(a, name)
if fname.endswith(type):
filelist.append(fname)
end_list = split_list(filelist)
thread_list = []
for i in range(len(end_list)):
# for i in range(8):
t1 = threading.Thread(target=compute,args=((end_list[i]), i))
thread_list.append(t1)
for i in range(len(thread_list)):
thread_list[i].start()
for t in thread_list:
t.join()
print("程序结束")
print("%d seconds: " % all_time,str(datetime.timedelta(seconds=all_time)))
def split_list(all_list):
end_list = []
n = len(all_list) // 3 #这里把一个列表切分成3个列表,在这里控制进程数
for i in range(0, len(all_list), n):
name=all_list[i:i + n]
end_list.append(name)
return end_list
def compute(filelist, i):
print("------开始计算时间------")
ftime = 0.0
count = 0
for item in filelist:
clip = VideoFileClip(item)
ftime += clip.duration
count = count + 1
print("线程%d"%i+"已完成%.2f" %((count / filelist.__len__()) * 100)+"%" )
mutex.acquire()
global all_time
all_time += ftime
mutex.release()
if __name__ == "__main__":
time1 = time.time()
all_time = 0.0
mutex = threading.Lock()
path = r"F:\XXX"
compute_video_time(path)
time2 = time.time()
print(time2 - time1)