脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - python3获取视频文件播放时长的三种方法

python3获取视频文件播放时长的三种方法

2024-04-19 15:24小龙在山东 Python

这篇文章主要介绍了python3获取视频文件播放时长的三种方法,VideoFileClip,CV2以及FFmpeg这三种方法,文章通过代码示例给大家讲解的非常详细,需要的朋友可以参考下

方法一:VideoFileClip

from moviepy.editor import VideoFileClip


def get_duration_from_moviepy(url):
    clip = VideoFileClip(url)
    return clip.duration

方法二:CV2

最快。

下载安装:https://github.com/opencv/opencv/releases

pip install opencv-python
import cv2


def get_duration_from_cv2(filename):
  cap = cv2.VideoCapture(filename)
  if cap.isOpened():
    rate = cap.get(5)
    frame_num =cap.get(7)
    duration = frame_num/rate
    return duration
  return -1

方法三:FFmpeg

pip install ffmpy3
import ffmpy3


def get_duration_from_ffmpeg(url):
    tup_resp = ffmpy3.FFprobe(
        inputs={url: None},
        global_options=[
            '-v', 'quiet',
            '-print_format', 'json',
            '-show_format', '-show_streams'
        ]
    ).run(stdout=subprocess.PIPE)

    meta = json.loads(tup_resp[0].decode('utf-8'))
    return meta['format']['duration']

速度比较

import json
import subprocess
import time
import cv2
import ffmpy3
from moviepy.editor import VideoFileClip


ls = [
"https://test/1.mp4",
"http://test/2.mp4",
"https://test/3.mp4"
]


def get_duration_from_cv2(filename):
  cap = cv2.VideoCapture(filename)
  if cap.isOpened():
    rate = cap.get(5)
    frame_num =cap.get(7)
    duration = frame_num/rate
    return duration
  return -1


def get_duration_from_moviepy(url):
    clip = VideoFileClip(url)
    return clip.duration


def get_duration_from_ffmpeg(url):
    tup_resp = ffmpy3.FFprobe(
        inputs={url: None},
        global_options=[
            '-v', 'quiet',
            '-print_format', 'json',
            '-show_format', '-show_streams'
        ]
    ).run(stdout=subprocess.PIPE)

    meta = json.loads(tup_resp[0].decode('utf-8'))
    return meta['format']['duration']


for u in ls:
    t1 = time.time()
    p = get_duration_from_cv2(u)
    t2 = time.time()
    print('CV2 Duration: ', p, ' Time: ', t2 - t1)

    t1 = time.time()
    p = get_duration_from_moviepy(u)
    t2 = time.time()
    print('Moviepy Duration: ', p, ' Time: ', t2 - t1)

    t1 = time.time()
    p = get_duration_from_ffmpeg(u)
    t2 = time.time()
    print('FFMPEG Duration: ', p, ' Time: ', t2 - t1)
    print()

参考

https://ffmpy3.readthedocs.io/en/latest/

到此这篇关于python3获取视频文件播放时长的三种方法的文章就介绍到这了,更多相关python3获取视频播放时长内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://lilongsy.blog.csdn.net/article/details/121206810

延伸 · 阅读

精彩推荐
  • PythonPython中Playwright 与 pyunit 结合使用详解

    Python中Playwright 与 pyunit 结合使用详解

    这篇文章主要介绍了Python中Playwright 与 pyunit 结合使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...

    田辛 | 田豆芽8312023-03-05
  • PythonPython中的tkinter库简单案例详解

    Python中的tkinter库简单案例详解

    tkinter 是 Python 的标准 GUI 库,Python 使用 tkinter 可以快速的创建 GUI 应用程序,今天通过本文给大家分享Python中的tkinter库简单案例详解,需要的朋友可以参考...

    A-L-Kun4202022-08-29
  • PythonPython中常见的导入方式总结

    Python中常见的导入方式总结

    这篇文章主要介绍了Python中常见的导入方式总结,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下...

    Villanelle#5052021-10-25
  • PythonPython实现把json格式转换成文本或sql文件

    Python实现把json格式转换成文本或sql文件

    这篇文章主要介绍了Python实现把json格式转换成文本或sql文件,本文直接给出代码实例,需要的朋友可以参考下...

    Python教程网9302020-07-21
  • Pythonpython实现简单的贪吃蛇游戏

    python实现简单的贪吃蛇游戏

    这篇文章主要为大家详细介绍了python实现简单的贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    carrot_interrupt10922021-09-25
  • Pythonpython安装读取grib库总结(推荐)

    python安装读取grib库总结(推荐)

    这篇文章主要介绍了python安装读取grib库总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参...

    毛发浓密的猿工科研备忘11282020-06-26
  • Pythonpython对指定目录下文件进行批量重命名的方法

    python对指定目录下文件进行批量重命名的方法

    这篇文章主要介绍了python对指定目录下文件进行批量重命名的方法,涉及Python中replace及join方法的使用技巧,非常具有实用价值,需要的朋友可以参考下...

    work245532020-06-09
  • Python在Python下利用OpenCV来旋转图像的教程

    在Python下利用OpenCV来旋转图像的教程

    这篇文章主要介绍了在Python下利用OpenCV来旋转图像的教程,代码和核心的算法都非常简单,需要的朋友可以参考下 ...

    脚本之家5892020-06-08