ffmpeg-python 任意提取视频帧

Posted anoyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ffmpeg-python 任意提取视频帧相关的知识,希望对你有一定的参考价值。

▶ 环境准备

1、安装 FFmpeg

音/视频工具 FFmpeg 简易安装文档

2、安装 ffmpeg-python

pip3 install ffmpeg-python

3、【可选】安装 opencv-python

pip3 install opencv-python

4、【可选】安装 numpy

pip3 install numpy

▶ 视频帧提取

准备视频素材

抖音视频素材下载:https://anoyi.com/dy/top

基于视频帧数提取任意一帧

import ffmpeg
import numpy
import cv2
import sys
import random


def read_frame_as_jpeg(in_file, frame_num):
    """
    指定帧数读取任意帧
    """
    out, err = (
        ffmpeg.input(in_file)
              .filter(\'select\', \'gte(n,{})\'.format(frame_num))
              .output(\'pipe:\', vframes=1, format=\'image2\', vcodec=\'mjpeg\')
              .run(capture_stdout=True)
    )
    return out


def get_video_info(in_file):
    """
    获取视频基本信息
    """
    try:
        probe = ffmpeg.probe(in_file)
        video_stream = next((stream for stream in probe[\'streams\'] if stream[\'codec_type\'] == \'video\'), None)
        if video_stream is None:
            print(\'No video stream found\', file=sys.stderr)
            sys.exit(1)
        return video_stream
    except ffmpeg.Error as err:
        print(str(err.stderr, encoding=\'utf8\'))
        sys.exit(1)


if __name__ == \'__main__\':
    file_path = \'/Users/admin/Downloads/拜无忧.mp4\'
    video_info = get_video_info(file_path)
    total_frames = int(video_info[\'nb_frames\'])
    print(\'总帧数:\' + str(total_frames))
    random_frame = random.randint(1, total_frames)
    print(\'随机帧:\' + str(random_frame))
    out = read_frame_as_jpeg(file_path, random_frame)
    image_array = numpy.asarray(bytearray(out), dtype="uint8")
    image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
    cv2.imshow(\'frame\', image)
    cv2.waitKey()

基于时间提取任意一帧

import ffmpeg
import numpy
import cv2
import sys
import random


def read_frame_by_time(in_file, time):
    """
    指定时间节点读取任意帧
    """
    out, err = (
        ffmpeg.input(in_file, ss=time)
              .output(\'pipe:\', vframes=1, format=\'image2\', vcodec=\'mjpeg\')
              .run(capture_stdout=True)
    )
    return out


def get_video_info(in_file):
    """
    获取视频基本信息
    """
    try:
        probe = ffmpeg.probe(in_file)
        video_stream = next((stream for stream in probe[\'streams\'] if stream[\'codec_type\'] == \'video\'), None)
        if video_stream is None:
            print(\'No video stream found\', file=sys.stderr)
            sys.exit(1)
        return video_stream
    except ffmpeg.Error as err:
        print(str(err.stderr, encoding=\'utf8\'))
        sys.exit(1)

if __name__ == \'__main__\':
    file_path = \'/Users/admin/Downloads/拜无忧.mp4\'
    video_info = get_video_info(file_path)
    total_duration = video_info[\'duration\']
    print(\'总时间:\' + total_duration + \'s\')
    random_time = random.randint(1, int(float(total_duration)) - 1) + random.random()
    print(\'随机时间:\' + str(random_time) + \'s\')
    out = read_frame_by_time(file_path, random_time)
    image_array = numpy.asarray(bytearray(out), dtype="uint8")
    image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
    cv2.imshow(\'frame\', image)
    cv2.waitKey()

▶ 相关资料

本文由博客一文多发平台 OpenWrite 发布!

以上是关于ffmpeg-python 任意提取视频帧的主要内容,如果未能解决你的问题,请参考以下文章

ffmpeg-python中文文档(三)——API参考

ffmpeg实现视频I帧提取

使用 OpenCV 仅在视频文件开头几秒钟提取帧

python读取视频文件大小,码率,帧率,以及通过码率计算文件大小与流量

无法使用 C++ 和 OpenCV 从视频中提取帧

想要在 AVPlayer 中播放视频时逐帧提取视频中的所有图像