▶ 环境准备
1、安装 FFmpeg
2、安装 ffmpeg-python
pip3 install ffmpeg-python
3、【可选】安装 opencv-python
pip3 install opencv-python
4、【可选】安装 numpy
pip3 install numpy
▶ 视频帧提取
准备视频素材
基于视频帧数提取任意一帧
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):
"""
获取视频基本信息
"""