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

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

 更新時(shí)間:2024年04月19日 11:22:12   作者:小龍?jiān)谏綎|  
這篇文章主要介紹了python3獲取視頻文件播放時(shí)長的三種方法,VideoFileClip,CV2以及FFmpeg這三種方法,文章通過代碼示例給大家講解的非常詳細(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í)長的三種方法的文章就介紹到這了,更多相關(guān)python3獲取視頻播放時(shí)長內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論