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
相关推荐
ZhengEnCi1 小时前
电脑操作 Agent 一直点不中:截图没更新时,提示词该怎么改才能停止瞎试
人工智能·python·agent
Wellijohn3 小时前
dict、tuple、dataclass 还是 slots?我把 6 种数据载体的 8 项开销全测了一遍
后端·python·性能优化
bufengwangluo4 小时前
爬虫和调用 API 有什么区别?为什么优先选 API
python·网络爬虫·ip·api调用
Dmz丶4 小时前
前端转 AI 100 天 Day 54:RAG 项目实战(三)——多轮对话 + 会话管理
python
console.log('npc')4 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
FlightYe4 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
geovindu5 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu7 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
真上帝的左手7 小时前
2. 编程语言-Python-Anaconda
开发语言·python