ffmpeg获取视频时长和分辨率

文章目录

  • ffmpeg获取视频文件时长和分辨率
    • 获取视频时长
    • 获取视频分辨率

ffmpeg获取视频文件时长和分辨率

获取视频时长

import os
import subprocess

# video_path--视频文件所在位置--'C:\\Users\\user\\Desktop\\1.mp4'
def get_video_duration(video_path: str):
    ext = os.path.splitext(video_path)[-1]
    if ext != '.mp4' and ext != '.avi' and ext != '.flv' and ext != '.ts':
        raise Exception('format not support')
    # -show_entries stream=width,height
    ffprobe_cmd = 'ffprobe -i {} -show_entries format=duration -v quiet -of csv="p=0"'
    p = subprocess.Popen(
        ffprobe_cmd.format(video_path),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True)
    out, err = p.communicate()
    duration_info = float(str(out, 'utf-8').strip())
    return int(duration_info)

获取视频分辨率

import subprocess
import json

def get_video_width_height(video_path):
    command = 'ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json {video_path}'.format(
        video_path=video_path)

    value = subprocess.check_output(command)
    data = json.loads(value)
    out = data['streams'][0]
    # width = out['width']
    # height = out['height']
    return out['width'],out['height']

width,height = get_video_width_height('C:\\Users\\user\\Desktop\\1.mp4')
print(width,height)

你可能感兴趣的:(视频剪辑,ffmpeg,视频时长,视频分辨率,Python,ffprobe)