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 小时前
基于YOLO的火灾烟雾检测系统~Python+目标检测+算法模型+2026原创
python·yolo·目标检测
立莹Sir1 小时前
Spring Bean 生命周期详解
java·python·spring
JHC0000007 小时前
基于Ollama,Milvus构建的建议知识检索系统
人工智能·python·milvus
mOok ONSC7 小时前
SpringBoot项目中读取resource目录下的文件(六种方法)
spring boot·python·pycharm
AI周红伟8 小时前
AI自动盯盘与定时行情分析:OpenClaw股票辅助Agent集成完整使用指南-周红伟
运维·服务器·人工智能·音视频·火山引擎
GIS兵墩墩8 小时前
postgis--PostgreSQL16及其plpython3u扩展
python·postgis
new Object ~8 小时前
LangChain的短期记忆存储实现
python·langchain
魔都吴所谓8 小时前
【Python】从零构建:IP地理位置查询实战指南
开发语言·python·tcp/ip
测试19988 小时前
使用Python自动化生成接口测试用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
智算菩萨9 小时前
【Pygame】第10章 游戏状态管理与场景切换机制
python·游戏·pygame