First, you need to install FFmpeg to process media files, and ffmpy to call FFmpeg
sudo apt install ffmpeg
pip install ffmpy
FFmpeg's ffprobe command is used to parse media file information, such as:
ffprobe -show_streams -select_streams v -i /home/root/example.mp4
Where -i represents the media file path, -show_streams
used to select the overall information of the media stream, -select_streams v
used to filter video information, and filter audio information. For
detailed parameters, please refer to ffprobe Documentation
Call ffmpeg code in python:
from ffmpy import FFmpeg, FFprobe
from subprocess import Popen, PIPE
import json
def video_info(video_path):
"""
接受视频路径, 读取视频信息
:param video_path: 视频路径
:return: 视频帧率
"""
# 构建FFmpeg命令行
ff = FFprobe(
global_options='-of json -show_streams -select_streams v',
inputs={video_path: None},
)
# ffmpeg默认输出到终端, 但我们需要在进程中获取媒体文件的信息
# 所以将命令行的输出重定向到管道, 传递给当前进程
res = ff.run(stdout=PIPE, stderr=PIPE)
video_stream = res[0]
# 解析视频流信息
video_detail = json.loads(video_stream).get('streams')[0]
# 获取视频实际帧率, 计算取整
frame_rate = round(eval(video_detail.get('r_frame_rate')))
# 返回码率
return frame_rate
In addition, you can get the average frame rate/avg_frame_rate, bit rate/bit_rate, maximum bit rate/max_bit_rate, number of frames/nb_frames and other information as needed