Python实现获取视频时长功能
作者:剑客阿良_ALiang 时间:2021-11-25 03:33:57
前言
本文提供获取视频时长的python代码,精确到毫秒,一如既往的实用主义。
环境依赖
ffmpeg环境安装,可以参考:windows ffmpeg安装部署
本文主要使用到的不是ffmpeg,而是ffprobe也在上面这篇文章中的zip包中。
代码
不废话,上代码。
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author : 剑客阿良_ALiang
@file : get_video_duration.py
@ide : PyCharm
@time : 2021-12-23 13:52:33
"""
import os
import subprocess
def get_video_duration(video_path: str):
ext = os.path.splitext(video_path)[-1]
if ext != '.mp4' and ext != '.avi' and ext != '.flv':
raise Exception('format not support')
ffprobe_cmd = 'ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0"'
p = subprocess.Popen(
ffprobe_cmd.format(video_path),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
out, err = p.communicate()
print("subprocess 执行结果:out:{} err:{}".format(out, err))
duration_info = float(str(out, 'utf-8').strip())
return int(duration_info * 1000)
if __name__ == '__main__':
print('视频的duration为:{}ms'.format(get_video_duration('D:/tmp/100.mp4')))
代码说明:
1、对视频的后缀格式做了简单的校验,如果需要调整可以自己调整一下。
2、对输出的结果做了处理,输出int类型的数据,方便使用。
验证一下
准备的视频如下:
验证一下
补充
Python实现获取视频fps
#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author : 剑客阿良_ALiang
@file : get_video_fps.py
@ide : PyCharm
@time : 2021-12-23 11:21:07
"""
import os
import subprocess
def get_video_fps(video_path: str):
ext = os.path.splitext(video_path)[-1]
if ext != '.mp4' and ext != '.avi' and ext != '.flv':
raise Exception('format not support')
ffprobe_cmd = 'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate {}'
p = subprocess.Popen(
ffprobe_cmd.format(video_path),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
out, err = p.communicate()
print("subprocess 执行结果:out:{} err:{}".format(out, err))
fps_info = str(out, 'utf-8').strip()
if fps_info:
if fps_info.find("/") > 0:
video_fps_str = fps_info.split('/', 1)
fps_result = int(int(video_fps_str[0]) / int(video_fps_str[1]))
else:
fps_result = int(fps_info)
else:
raise Exception('get fps error')
return fps_result
if __name__ == '__main__':
print('视频的fps为:{}'.format(get_video_fps('D:/tmp/100.mp4')))
代码说明:
1、首先对视频格式做了简单的判断,这部分可以按照需求自行调整。
2、通过subprocess进行命令调用,获取命令返回的结果。注意范围的结果为字节串,需要调整格式处理。
验证一下
下面是准备的素材视频,fps为25,看一下执行的结果。
执行结果
到此这篇关于Python实现获取视频时长功能的文章就介绍到这了,更多相关Python获取视频时长内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://blog.csdn.net/zhiweihongyan1/article/details/122107663
标签:Python,视频,时长
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python图形界面教程Tkinter详解
2021-01-08 04:27:47
页面链接方式的统一性
2008-03-24 17:02:00
SQL Server 安装出错:以前的某个程序安装已在安装计算机上创建挂起的文件操作
2010-02-23 14:48:00
javascript判断一个元素是否数组
2009-07-31 12:48:00
你是真正的用户体验设计者吗? Ⅱ
2008-03-21 12:30:00
Oracle AS关键字 提示错误
2011-04-18 12:42:00
20个常用Python运维库和模块
2023-05-11 21:44:13
解决Python requests 报错方法集锦
2023-05-05 20:21:27
Python基于time模块求程序运行时间的方法
2023-07-27 15:59:03
![](https://img.aspxhome.com/file/2023/0/61220_0s.png)
如何理解PHP程序执行的过程原理
2023-10-08 14:45:10
对pandas中iloc,loc取数据差别及按条件取值的方法详解
2021-06-15 01:58:05
Python将主机名转换为IP地址的方法
2023-09-06 21:30:42
asp超强的Server Application Error 的解决方法
2008-11-13 13:04:00
Python使用psutil获取进程信息的例子
2021-11-03 07:29:47
细化解析:MySQL 服务器究竟有多稳定?
2009-01-14 11:55:00
跟老齐学Python之集合的关系
2023-02-04 22:32:27
![](https://img.aspxhome.com/file/2023/8/77028_0s.png)
解决Django中checkbox复选框的传值问题
2023-09-11 17:15:22
![](https://img.aspxhome.com/file/2023/5/70205_0s.jpg)
网页布局的位置重心与位置间的对比关系
2007-10-15 19:20:00
![](https://img.aspxhome.com/file/UploadPic/200710/15/20071015192410224s.jpg)
在Python中使用Mako模版库的简单教程
2021-11-08 12:33:45
何处安放的Loading
2011-08-10 19:11:20