python读取视频里面爱豆出现的时间_如何用Python获取视频的持续时间?

为了让事情简单一点,下面的代码将输出设置为JSON。

您可以使用probe(filename)来使用它,或者使用duration(filename)来获取持续时间:json_info = probe(filename)

secondes_dot_ = duration(filename) # float number of seconds

它在Ubuntu 14.04上工作,当然是在ffprobe上安装的。代码不是为了速度或美观而优化的,但它在我的机器上工作,希望它能有所帮助。#

# Command line use of 'ffprobe':

#

# ffprobe -loglevel quiet -print_format json \

# -show_format -show_streams \

# video-file-name.mp4

#

# man ffprobe # for more information about ffprobe

#

import subprocess32 as sp

import json

def probe(vid_file_path):

''' Give a json from ffprobe command line

@vid_file_path : The absolute (full) path of the video file, string.

'''

if type(vid_file_path) != str:

raise Exception('Gvie ffprobe a full file path of the video')

return

command = ["ffprobe",

"-loglevel", "quiet",

"-print_format", "json",

"-show_format",

"-show_streams",

vid_file_path

]

pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.STDOUT)

out, err = pipe.communicate()

return json.loads(out)

def duration(vid_file_path):

''' Video's duration in seconds, return a float number

'''

_json = probe(vid_file_path)

if 'format' in _json:

if 'duration' in _json['format']:

return float(_json['format']['duration'])

if 'streams' in _json:

# commonly stream 0 is the video

for s in _json['streams']:

if 'duration' in s:

return float(s['duration'])

# if everything didn't happen,

# we got here because no single 'return' in the above happen.

raise Exception('I found no duration')

#return None

if __name__ == "__main__":

video_file_path = "/tmp/tt1.mp4"

duration(video_file_path) # 10.008

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值