python获取视频时长方法
生活随笔
收集整理的這篇文章主要介紹了
python获取视频时长方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.使用subprocess和re
import re import subprocess video = r"work/train/video/a8b96f016a28d8f3836f7cbb7734ecde.mp4" import subprocessdef get_length(filename):result = subprocess.run(["ffprobe", "-v", "error", "-show_entries","format=duration", "-of","default=noprint_wrappers=1:nokey=1", filename],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)return float(result.stdout) t=get_length(video) print(t)2.使用VideoFileClip
from moviepy.editor import VideoFileClip clip=VideoFileClip(video) print(clip.duration)使用cv2
import cv2 def get_video_duration(filename):cap = cv2.VideoCapture(filename)if cap.isOpened():rate = cap.get(5)frame_num =cap.get(7)duration = frame_num/ratereturn durationreturn -1 t=get_video_duration(video) print(t)
從結(jié)果可以看出,cv2的運行時長是最短的,推薦使用cv2
https://blog.csdn.net/weixin_42076509/article/details/107127147
總結(jié)
以上是生活随笔為你收集整理的python获取视频时长方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3-pandas 数据结构
- 下一篇: Integer缓存池