欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3獲取視頻文件播放時(shí)長(zhǎng)的三種方法

 更新時(shí)間:2024年04月19日 11:22:12   作者:小龍?jiān)谏綎|  
這篇文章主要介紹了python3獲取視頻文件播放時(shí)長(zhǎng)的三種方法,VideoFileClip,CV2以及FFmpeg這三種方法,文章通過(guò)代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下

方法一: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/

到此這篇關(guān)于python3獲取視頻文件播放時(shí)長(zhǎng)的三種方法的文章就介紹到這了,更多相關(guān)python3獲取視頻播放時(shí)長(zhǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)將JSON文件中的數(shù)據(jù)格式化處理

    python實(shí)現(xiàn)將JSON文件中的數(shù)據(jù)格式化處理

    JSON是一種輕量級(jí)的數(shù)據(jù)交換格式,常用于Web服務(wù)間的數(shù)據(jù)傳輸,Python內(nèi)置了??json??模塊,能夠方便地進(jìn)行JSON數(shù)據(jù)的解析與格式化,本文將通過(guò)具體的Python代碼實(shí)例,深入探討如何將JSON文件中的數(shù)據(jù)進(jìn)行格式化處理,需要的朋友可以參考下
    2024-03-03
  • 在tensorflow中設(shè)置使用某一塊GPU、多GPU、CPU的操作

    在tensorflow中設(shè)置使用某一塊GPU、多GPU、CPU的操作

    今天小編就為大家分享一篇在tensorflow中設(shè)置使用某一塊GPU、多GPU、CPU的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • Python實(shí)現(xiàn)博客快速備份的腳本分享

    Python實(shí)現(xiàn)博客快速備份的腳本分享

    本文針對(duì)博客園實(shí)現(xiàn)了一個(gè)自動(dòng)備份腳本,可以快速將自己的文章備份成Markdown格式的獨(dú)立文件,備份后的md文件可以直接放入到hexo博客中,感興趣的可以了解一下
    2022-09-09
  • python類方法中的self關(guān)鍵字使用

    python類方法中的self關(guān)鍵字使用

    這篇文章主要介紹了python類方法中的self關(guān)鍵字使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 基于python的itchat庫(kù)實(shí)現(xiàn)微信聊天機(jī)器人(推薦)

    基于python的itchat庫(kù)實(shí)現(xiàn)微信聊天機(jī)器人(推薦)

    這篇文章主要介紹了基于python的itchat庫(kù)實(shí)現(xiàn)微信聊天機(jī)器人,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Pyqt助手安裝PyQt5幫助文檔過(guò)程圖解

    Pyqt助手安裝PyQt5幫助文檔過(guò)程圖解

    這篇文章主要介紹了Pyqt助手安裝PyQt5幫助文檔過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python判斷變量是否為int、字符串、列表、元組、字典的方法詳解

    python判斷變量是否為int、字符串、列表、元組、字典的方法詳解

    這篇文章主要介紹了python判斷變量是否為int、字符串、列表、元組、字典的方法詳解,需要的朋友可以參考下
    2020-02-02
  • 一文讓你秒懂精通pip并快速體驗(yàn)深度學(xué)習(xí)應(yīng)用【建議收藏】

    一文讓你秒懂精通pip并快速體驗(yàn)深度學(xué)習(xí)應(yīng)用【建議收藏】

    在使用python的時(shí)候,經(jīng)常使用到pip這個(gè)工具,可以很方便的線上安裝依賴庫(kù),當(dāng)然pip還有很多參數(shù)都可以幫我們?nèi)ゲ樵円恍?kù)信息,這篇文章主要給大家介紹了如何通過(guò)一篇文章讓你秒懂精通pip并快速體驗(yàn)深度學(xué)習(xí)應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Python函數(shù)常見(jiàn)幾種return返回值類型

    Python函數(shù)常見(jiàn)幾種return返回值類型

    本文主要介紹了Python函數(shù)常見(jiàn)幾種return返回值類型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Keras 多次加載model出錯(cuò)的解決方案

    Keras 多次加載model出錯(cuò)的解決方案

    這篇文章主要介紹了Keras 多次加載model出錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論