【计算机视觉】ffmpeg获取视频详细信息含视频时长

首先安装ffmpeg,以下函数返回的就是视频时长了,格式是295.760000秒。需要自己用divmod()转换为00:00:00的格式。

win环境下

def getLenTime(filename):
    # ffprobe.exe这是在win环境下
    command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
    result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    out = result.stdout.read()
    #print(str(out))
    temp = str(out.decode('utf-8'))
    data = json.loads(temp)["format"]['duration']
    return float(data)

linux环境下,做部分修改

def getLenTime(filename):
    cmd = "ffprobe -v error -loglevel quiet -print_format json -show_format -show_streams -i {}".format(video_path)
    result = subprocess.Popen(cmd,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    temp = str(result.stdout.read().decode('utf-8'))
    data = json.loads(temp)["format"]['duration']
    return float(data)

 视频的详细信息,以json格式保存。想取什么信息可以直接取~ 比如 "display_aspect_ratio": "16:9"

{
    "streams": [
        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "High",
            "codec_type": "video",
            "codec_time_base": "1/50",
            "codec_tag_string": "avc1",
            "codec_tag": "0x31637661",
            "width": 640,
            "height": 368,
            "coded_width": 640,
            "coded_height": 368,
            "closed_captions": 0,
            "has_b_frames": 2,
            "sample_aspect_ratio": "46:45",
            "display_aspect_ratio": "16:9",
            "pix_fmt": "yuv420p",
            "level": 30,
            "chroma_location": "left",
            "refs": 1,
            "is_avc": "true",
            "nal_length_size": "4",
            "r_frame_rate": "25/1",
            "avg_frame_rate": "25/1",
            "time_base": "1/12800",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 128512,
            "duration": "10.040000",
            "bit_rate": "609025",
            "bits_per_raw_sample": "8",
            "nb_frames": "251",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "language": "und",
                "handler_name": "VideoHandler"
            }
        },
        {
            "index": 1,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "HE-AAC",
            "codec_type": "audio",
            "codec_time_base": "1/44100",
            "codec_tag_string": "mp4a",
            "codec_tag": "0x6134706d",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/44100",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 438272,
            "duration": "9.938141",
            "bit_rate": "48231",
            "max_bit_rate": "48231",
            "nb_frames": "214",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "language": "eng",
                "handler_name": "SoundHandler"
            }
        }
    ],
    "format": {
        "filename": "../data/vid_input/demo.mp4",
        "nb_streams": 2,
        "nb_programs": 0,
        "format_name": "mov,mp4,m4a,3gp,3g2,mj2",
        "format_long_name": "QuickTime / MOV",
        "start_time": "0.000000",
        "duration": "10.040000",
        "size": "831676",
        "bit_rate": "662690",
        "probe_score": 100,
        "tags": {
            "major_brand": "isom",
            "minor_version": "512",
            "compatible_brands": "isomiso2avc1mp41",
            "title": "xiashangju_10s_B_MAND",
            "encoder": "Lavf56.40.101"
        }
    }
}

在Stack Overflow中,How to get the duration of a video in Python?

参考:

python获取音频的长度ffmpeg或ffprobe 代码来源,其中还实现了获取视频宽度的函数,以及时间格式转换,如下。

How to get the duration of a video in Python?

# -*- coding: utf-8 -*-
import subprocess
import json
def main():
    mp3ss=getLenTime("03.wav")
    print(mp3ss)
    mp3ss,mp3ms=mp3ss.split(".")
    # 原本输出格式0.000S
    # 视频时间格式转换,目标00:00:00
    m, s = divmod(int(mp3ss), 60)
    h, m = divmod(m, 60)
    hmsms="%02d:%02d:%02d" % (h, m, s) + "." + mp3ms
    print (hmsms)

# 获取视频宽度
def getLength(filename):
    command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
    result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    out = result.stdout.read()
    #print(str(out))
    temp = str(out.decode('utf-8'))
    try:
        data = json.loads(temp)['streams'][1]['width']
    except:
        data = json.loads(temp)['streams'][0]['width']
    return data

# 获取视频时间
def getLenTime(filename):
    command = ["ffprobe.exe","-loglevel","quiet","-print_format","json","-show_format","-show_streams","-i",filename]
    result = subprocess.Popen(command,shell=True,stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    out = result.stdout.read()
    #print(str(out))
    temp = str(out.decode('utf-8'))
    data = json.loads(temp)["format"]['duration']
    return data

if __name__ == '__main__':
    main()

主要还是要看官网

http://ffmpeg.org/ffprobe.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值