为什么想做这个
- 有时候学习的时候,离线看视频,想知道总视频的时间,从而盘算好每天看多少个视频,或者每天看多久。
代码展示
pip install moviepy
import os
from moviepy.editor import VideoFileClip as vfc
import time
def folder_movie_files(folder: str) -> list[str]:
movie_type = ('.flv', '.mp4', '.avi')
file_list = []
for a, b, c in os.walk(folder):
for filename in c:
if filename.endswith(movie_type):
file_path = os.path.join(a, filename)
file_list.append(file_path)
print(folder, ": 有", len(file_list), "个视频文件")
return file_list
def single_movie_time(movie_path: str) -> int:
time = 0
try:
movie = vfc(movie_path)
time += movie.duration
movie.close()
except:
print(movie_path, '该文件出现异常')
return time
def folders_movie_times(folders: list[str]) -> int:
print("正在计算...")
movie_files = []
for folder in folders:
movie_files += folder_movie_files(folder)
time = 0
movie_files_len = len(movie_files)
count = 0
for movie_path in movie_files:
time += single_movie_time(movie_path)
count += 1
print("已完成%.2f" % (count * 100 / movie_files_len), "%")
return time
if __name__ == '__main__':
time_start = time.time()
path = ["目录"]
time_count = folders_movie_times(path)
print("总视频时间:", round(time_count, 2), "秒")
print("总视频时间:", round(time_count / 60, 2), "分钟")
print("总视频时间:", round(time_count / 60 / 60, 2), "小时")
time_end = time.time()
print("程序运行时间:", round(time_end - time_start, 2), "秒")