python如何调用ffprobe_Python3 FFprobe获取视频参数[亲测有效]

python如何调用ffprobe_Python3 FFprobe获取视频参数[亲测有效]”’getparameterfromffmpegfunctioncheck_ffmepg_existfunctionreturn_all_parameterfunctionreturn_video_height_wigthfunctionreturn_video_filesizefunctionreturn_video_full_framefunct…

大家好,欢迎来到IT知识分享网。

”’get parameter from ffmpegfunction check_ffmepg_existfunction return_all_parameterfunction return_video_height_wigthfunction return_video_filesizefunction return_video_full_framefunction return_video_time_length”’

import os

import subprocess

import json

class ffprboe_parameter():

def __init__(self,env=0):

”’env 1 代表使用本地环境变量env 2 代表使用bin目录下的:param env:”’

self.check_ffmepg_exist() if env == 1 else print(end=”)

def check_ffmepg_exist(self):

base_dir = os.getcwd() + os.sep + ‘bin’

assert os.path.exists(base_dir + os.sep + ‘ffmpeg.exe’), FileNotFoundError(‘The bin dir not have ffmpeg.exe’)

assert os.path.exists(base_dir + os.sep + ‘ffprobe.exe’), FileNotFoundError(‘The bin dir not have ffprobe.exe’)

def return_all_parameter(self,video_path=None):

video_path=self.video_path_check(video_path=video_path)

assert os.path.exists(video_path),FileNotFoundError(‘video not found’)

assert os.path.isfile(video_path),FileExistsError(‘video is not file,please check it ‘)

try:

all_parameter=subprocess.check_output([‘ffprobe’,’-i’,video_path,’-print_format’,’json’,’-show_format’,’-show_streams’,’-v’,’quiet’])

all_parameter=all_parameter.decode(‘utf8’)

all_parameter=json.loads(all_parameter)

except Exception:

raise Exception(‘error for get video info’)

return all_parameter

def return_video_height_wigth(self,video_path=None):

video_path = self.video_path_check(video_path=video_path)

tmp=self.return_all_parameter(video_path=video_path)[‘streams’][0]

return (tmp[‘width’],tmp[‘height’])

def return_video_filesize(self,format=’GB’,video_path=None):

video_path = self.video_path_check(video_path=video_path)

tmp = self.return_all_parameter(video_path=video_path)[‘format’]

if format.lower()==’gb’:

return float(‘%.4f’%(int(tmp[‘size’])/1024/1024/1024)),format.upper()

if format.lower()==’mb’:

return float(‘%.4f’%(int(tmp[‘size’])/1024/1024)),format.upper()

if format.lower()==’kb’:

return float(‘%.4f’ % (int(tmp[‘size’]) / 1024 )), format.upper()

def return_video_full_frame(self,video_path=None):

video_path=self.video_path_check(video_path=video_path)

tmp = self.return_all_parameter(video_path=video_path)[‘streams’][0]

return tmp[‘nb_frames’]

def init_video_path(self,video_path):

self.video_path=video_path

return self.video_path

def video_path_check(self,video_path):

if video_path == None:

try:

video_path=self.video_path

except:

raise BaseException(‘video_path is not define ‘)

return video_path

def return_video_time_length(self,video_path=None):

video_path=self.video_path_check(video_path=video_path)

tmp = self.return_all_parameter(video_path=video_path)[‘format’]

return str(int(float(tmp[‘duration’])/3600)).__add__(‘小时’).__add__(str(int(float(tmp[‘duration’])%3600/60))).__add__(‘分钟’)

def return_all_items(self,video_path=None):

video_path=self.video_path_check(video_path=video_path)

tmp={

‘video_height_wigth’:list(self.return_video_height_wigth()),

‘video_filesize’:”.join([”.join(str(x)) for x in self.return_video_filesize()]),

‘video_full_frame’:self.return_video_full_frame(),

‘video_time_length’:self.return_video_time_length()

}

return tmp

if __name__ == ‘__main__’:

video_info_api=ffprboe_parameter()

video_info_api.init_video_path(video_path=r’d:\\test.mp4′)

video_info_api.return_all_items()

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25034.html

(0)
上一篇 2023-08-04 14:00
下一篇 2023-08-05 09:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信