广告位联系
返回顶部
分享到

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

python 来源:互联网 作者:佚名 发布时间:2024-04-19 22:34:14 人浏览
摘要

方法一:VideoFileClip 1 2 3 4 5 6 from moviepy.editor import VideoFileClip def get_duration_from_moviepy(url): clip = VideoFileClip(url) return clip.duration 方法二:CV2 最快。 下载安装:https://github.com/opencv/opencv/releases 1 pip

方法一:VideoFileClip

1

2

3

4

5

6

from moviepy.editor import VideoFileClip

 

 

def get_duration_from_moviepy(url):

    clip = VideoFileClip(url)

    return clip.duration

方法二:CV2

最快。

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

1

pip install opencv-python

1

2

3

4

5

6

7

8

9

10

11

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

1

pip install ffmpy3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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']

速度比较

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

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()


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计