Python爬取视频时长场景实践示例
作者:三爷 时间:2021-08-14 01:32:56
简介:
在视频相关测试场景下,例如:有时需要知道全部视频的汇总时长,显然一个个打开并且手工计算耗时耗力,我们可以通过编写脚本进行快速汇总。
获取视频时长的方式
1、通过subprocess进行获取。
2、通过moviepy库中VideoFileClip获取。
3、通过cv2库获取。
安装
1、subprocess:无需安装,Python内置。
2、moviepy:pip install moviepy。
3、cv2:pip install opencv-python
准备工序:
1、当前项目新增videos目录。
2、you-get 下载几个视频。python:超实用下载工具you-get
如:下载了两个短视频
获取视频时长的3种方式对比
import cv2
import time
import subprocess
from moviepy.editor import VideoFileClip
def video_duration_1(filename):
start = time.time()
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
end = time.time()
spend = end - start
print("获取视频时长方法1耗时:", spend)
return float(result.stdout)
def video_duration_2(filename):
start = time.time()
clip = VideoFileClip(filename)
end = time.time()
spend = end - start
print("获取视频时长方法2耗时:", spend)
return float(clip.duration)
def video_duration_3(filename):
start = time.time()
cap = cv2.VideoCapture(filename)
if cap.isOpened():
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num / rate
end = time.time()
spend = end - start
print("获取视频时长方法3耗时:", spend)
return duration
return -1
if __name__ == '__main__':
file = r".\videos\mda-mkbhvebqej3cw9yh.mp4"
video_time_1 = video_duration_1(file)
print(video_time_1)
print("*" * 100)
video_time_2 = video_duration_2(file)
print(video_time_2)
print("*" * 100)
video_time_3 = video_duration_3(file)
print(video_time_3)
执行源码:
结论:
1、三种方式均可以正常获取视频时长,并且准确。
2、推荐使用cv2获取视频时长,耗时最短。
实践案例:获取文件夹内全部视频总时长
import cv2
import os
def video_duration(dir_name):
sum_duration = 0
for root, dirs, files in os.walk(dir_name, topdown=False):
for filename in files:
cap = cv2.VideoCapture(dir_name + "\\" + filename)
if cap.isOpened():
rate = cap.get(5)
frame_num = cap.get(7)
duration = frame_num / rate
sum_duration += duration
return sum_duration
if __name__ == '__main__':
file = r".\videos"
total_video_time = video_duration(file)
print(f"{file} 目录下全部视频总时长为:{total_video_time}秒")
来源:http://167t.cn/86uD
标签:Python,爬取,视频时长
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
http状态码一览表以及HTTP响应的返回头信息
2010-03-31 14:45:00
CSS浏览器兼容问题整理(IE6.0、IE7.0 与FireFox)
2008-10-27 13:45:00
Oracle 日期的一些简单使用
2009-08-05 20:42:00
python使用socket向客户端发送数据的方法
2023-08-13 00:16:51
一个jquery日期选取插件源码
2009-12-23 19:15:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/2009126213743-92s.png)
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
仿DW的图形菜单 DIV+CSS
2007-08-14 10:07:00
![](https://img.aspxhome.com/file/UploadPic/20078/14/2007814102358206s.gif)
怎样解决MySQL 5.0.16的乱码问题
2008-10-13 12:47:00
js经验分享 JavaScript反调试技巧
2023-07-13 07:53:04
![](https://img.aspxhome.com/file/2023/7/55927_0s.png)
ASP 调用带参数输出的COM接口
2011-03-17 10:59:00
web脚本程序攻击的防范
2007-10-18 13:29:00
Go语言题解LeetCode1266访问所有点的最小时间示例
2023-08-29 08:10:39
![](https://img.aspxhome.com/file/2023/1/99051_0s.jpg)
网页设计布局原则
2010-04-20 17:18:00
ASP显示当前在线人数统计代码
2010-04-24 15:47:00
firebug1.7调试实用技巧指南
2011-06-27 20:17:22
关于python并发编程中的协程
2023-10-18 04:37:44
Python排序算法之堆排序算法
2023-01-17 02:00:55
![](https://img.aspxhome.com/file/2023/3/63933_0s.png)
oracle 合并查询 事务 sql函数小知识学习
2023-07-13 15:07:28
如何在事件代理中正确使用 focus 和 blur 事件
2010-01-30 12:51:00
简单的asp采集代码教程
2011-04-18 10:39:00