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
相关推荐
Ricardo-Yang14 小时前
SCNP语义分割边缘logits策略
数据结构·人工智能·python·深度学习·算法
soragui15 小时前
【Python】第 4 章:Python 数据结构实现
数据结构·windows·python
和小潘一起学AI15 小时前
CentOS 7安装Anaconda
开发语言·python
kcuwu.15 小时前
Python 正则表达式从入门到实战
数据库·python·正则表达式
不解不惑15 小时前
langchain qwen3 构建一个简单的对话系统
pytorch·python·langchain
努力努力再努力dyx15 小时前
【无标题】
开发语言·python
I疯子15 小时前
2026-04-07 打卡第 4 天
python
数据知道15 小时前
claw-code 源码分析:Tool Pool 组装——默认策略、过滤、MCP 开关如何影响「可用工具面」?
python·claude code·claw code
Zzj_tju15 小时前
Java 从入门到精通(十二):File 与 IO 流基础,为什么程序“读写文件”时总是容易出问题?
java·python·php
汽车搬砖家15 小时前
vSOMEIP系列 -6: vsomeip python版部署,双机跨域通信(vsomeip - davinci AP someip)
python·汽车