python 获取视频的时长

以下是几种获取视频时长的实现方法:

方法一:使用moviepy库

python 复制代码
from moviepy.editor import VideoFileClip
 
def get_video_duration(file_path):
    video = VideoFileClip(file_path)
    duration = video.duration
    video.close()
    return duration

方法二:使用cv2库

python 复制代码
import cv2
 
def get_video_duration(file_path):
    video = cv2.VideoCapture(file_path)
    frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = video.get(cv2.CAP_PROP_FPS)
    duration = frame_count / fps
    video.release()
    return duration

方法三:使用ffprobe命令行工具

python 复制代码
import subprocess
 
def get_video_duration(file_path):
    result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path], capture_output=True, text=True)
    duration = float(result.stdout)
    return duration
相关推荐
独隅1 分钟前
PyTorch转TFLite动态形状处理技巧
人工智能·pytorch·python
Shorasul9 分钟前
Go语言goroutine调度原理_Go语言GMP调度模型教程【高效】
jvm·数据库·python
Absurd58711 分钟前
Navicat导出JSON数据为空如何解决_过滤条件与权限排查
jvm·数据库·python
m0_7164300713 分钟前
SQL如何高效统计分类下的多项指标_善用CASE WHEN与SUM聚合
jvm·数据库·python
m0_5887584814 分钟前
PHP源码运行受主板供电影响吗_供电相数重要性说明【技巧】
jvm·数据库·python
qq_4138474016 分钟前
如何处理MongoDB跨分片事务报错_4.2+分布式事务的限制与两阶段提交延迟
jvm·数据库·python
InfinteJustice17 分钟前
HTML函数在超频CPU上更流畅吗_超频对HTML函数影响【技巧】
jvm·数据库·python
心易行者17 分钟前
代码写好了,然后呢?——手把手教你把Python脚本变成能赚钱的Web应用
开发语言·前端·python
站大爷IP21 分钟前
Python 秒杀系统实战:库存预扣 + 防超卖 极致优化实现
python
Dshuishui22 分钟前
Locust 压测网站小工具
python·pip