详解Python如何获取视频文件的大小和时长
作者:黄伟呢 时间:2021-12-08 14:50:13
前段时间,微信群有一个朋友问了下面这个问题,虽然很简单,但是觉得有必要和大家做一个知识分享。
这个问题一共有两个需求:
① 获取视频的文件大小;
② 获取视频的时长;
基于上述两个问题,我来教大家如何将获取到的信息存储到Excel中。
1. 前置知识
这里一共需要用到3个Python库,分别是os、pandas、moviepy。
① os:用于获取某个目录下的文件和文件大小;
② pandas:用于将提取到的信息,保存到Excel中;
③ moviepy:用于获取视频文件中的时长;
注意:pandas和moviepy属于第三方库,因此需要我们提前使用如下命令进行安装。
pip install pandas
pip install moviepy
2. 完整代码
① 导入所有库
import os
import pandas as pd
from moviepy.editor import VideoFileClip
② 获取指定目录下符合条件的文件
useful_dir = []
for i in os.listdir():
if i.endswith("wmv"):
useful_dir.append(i)
useful_dir
结果如下:
③ 自定义时间转化函数
def time_convert(seconds):
M,H = 60,3600
if seconds < M:
return f'00:00:0{seconds}' if seconds < 10 else f'00:00:{str(seconds)}'
elif seconds < H:
_M = int(seconds//M)
_S = int(seconds%M)
return f'00:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
else:
_H = int(seconds//H)
_M = int(seconds%H//M)
_S = int(seconds%H%M)
return f'{f"0{_H}" if _H < 10 else str(_H)}:{f"0{_M}" if _M < 10 else str(_M)}:{f"0{_S}" if _S < 10 else str(_S)}'
由于下面介绍的VideoFileClip()
方法,获取时长的单位是秒。因此,需将秒换成合适的"时分秒"格式。若时间超过一分钟,换算成"分钟:秒",若超过一小时,就换算成"小时:分钟:秒"格式。
④ 获取文件大小和时长
x = []
y = []
for i in useful_dir:
dir_size = str(round(os.path.getsize(i)/1024/1024,1)) + "M"
clip = VideoFileClip(i)
dir_time = time_convert(clip.duration)
x.append(dir_size)
y.append(dir_time)
df = pd.DataFrame({"文件大小":x,"文件时长":y})
df
结果如下:
⑤ 将得到的数据存储到Excel中
df.to_excel("info.xlsx",index=False)
最终效果如图所示:
来源:https://mp.weixin.qq.com/s/aiyBh_VJRBh9Hgx70V8b8Q
标签:Python,视频,大小,时长
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用python实现可视化大屏
2023-08-17 17:29:17
![](https://img.aspxhome.com/file/2023/9/65489_0s.png)
Python实现自动添加脚本头信息的示例代码
2022-07-02 18:12:42
Flask 让jsonify返回的json串支持中文显示的方法
2022-01-23 04:20:31
![](https://img.aspxhome.com/file/2023/8/93468_0s.jpg)
django 发送邮件和缓存的实现代码
2021-05-09 20:53:09
Python用户自定义异常的实现
2023-06-10 10:06:40
![](https://img.aspxhome.com/file/2023/0/107980_0s.png)
Python入门基础之import机制
2023-12-25 11:39:12
![](https://img.aspxhome.com/file/2023/4/115534_0s.png)
Python时间序列缺失值的处理方法(日期缺失填充)
2023-04-13 07:39:18
用FrontPage制作缩略图和图片重叠效果
2007-11-18 14:45:00
Python 读写文件和file对象的方法(推荐)
2022-01-09 08:36:03
Python中ROC曲线绘制
2023-05-28 16:38:15
![](https://img.aspxhome.com/file/2023/8/97358_0s.jpg)
Oracle常用dump命令,记录一下备查。
2009-03-04 10:27:00
使用Python写一个小游戏
2023-07-20 17:31:07
![](https://img.aspxhome.com/file/2023/1/60501_0s.jpg)
jQuery 让人恋恋不舍的秘密
2010-01-20 10:09:00
Python聚类算法之DBSACN实例分析
2021-03-26 00:11:10
![](https://img.aspxhome.com/file/2023/7/120607_0s.png)
asp如何删除数据库中的表或索引?
2010-06-26 12:23:00
八条常见的CSS错误及修复方法
2010-04-08 16:54:00
![](https://img.aspxhome.com/file/UploadPic/20104/8/spec-18s.jpg)
python实现划词翻译
2023-04-28 19:36:54
![](https://img.aspxhome.com/file/2023/1/84131_0s.jpg)
Python数据可视化之Seaborn的使用详解
2022-10-25 21:57:31
![](https://img.aspxhome.com/file/2023/2/89292_0s.png)
jQuery+php简单实现全选删除的方法
2023-11-05 20:23:38
Python实现获取照片拍摄日期并重命名的方法
2023-04-14 03:26:37
![](https://img.aspxhome.com/file/2023/4/100864_0s.jpg)