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 分钟前
基于Python实现LSTM对股票走势的预测
开发语言·python·lstm
机器学习之心31 分钟前
小波增强型KAN网络 + SHAP可解释性分析(Pytorch实现)
人工智能·pytorch·python·kan网络
JavaEdge在掘金37 分钟前
MySQL 8.0 的隐藏索引:索引管理的利器,还是性能陷阱?
python
站大爷IP1 小时前
Python办公自动化实战:手把手教你打造智能邮件发送工具
python
chao_7891 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
zdw1 小时前
fit parse解析佳明.fit 运动数据,模仿zwift数据展示
python
剑桥折刀s2 小时前
Python打卡:Day46
python
巴里巴气2 小时前
Python爬虫图片验证码和滑块验证码识别总结
爬虫·python
sword devil9003 小时前
PYQT实战:智能家居中控
python·智能家居·pyqt
NetX行者3 小时前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源