确定视频比特率Python



我有一个从Telegram下载的视频,我需要确定它的比特率。我有moviepy(pipinstallmoviepy,不是开发者版本(。此外,我有ffmpeg,但我不知道如何在python中使用它。此外,任何其他图书馆都可以为我工作。

http://timivanov.ru/kak-uznat-bitrate-i-fps-video-ispolzuya-python-i-ffmpeg/试试这个:

def get_bitrate(file):
try:
probe = ffmpeg.probe(file)
video_bitrate = next(s for s in probe['streams'] if s['codec_type'] == 'video')
bitrate = int(int(video_bitrate['bit_rate']) / 1000)
return bitrate

例外情况除外,如er:返回

import moviepy.editor as mp
video = mp.VideoFileClip('vid.mp4')
mp3 = video.audio
if mp3 is not None:
mp3.write_audiofile("vid_audio.mp3")
mp3_size =  os.path.getsize("vid_audio.mp3")
vid_size = os.path.getsize('vid.mp4')
duration = video.duration

bitrate = int((((vid_size - mp3_size)/duration)/1024*8))

以下是使用FFprobe:的解决方案

  • 执行ffprobe(命令行工具(作为子进程,并读取stdout的内容
    使用参数-print_format json获取JSON格式的输出
    若要仅获取bit_rate条目,请添加参数-show_entries stream=bit_rate
  • 使用dict = json.loads(data)将返回的字符串转换为字典
  • 从字典中获取比特率,并将其转换为int:bit_rate = int(dict['streams'][0]['bit_rate'])

代码示例创建一个用于测试的示例视频文件(使用FFmpeg(,并获得比特率(使用FFprobe(:

import subprocess as sp
import shlex
import json
input_file_name = 'test.mp4'
# Build synthetic video for testing:
################################################################################
sp.run(shlex.split(f'ffmpeg -y -f lavfi -i testsrc=size=320x240:rate=30 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -filter_complex amerge -vcodec libx264 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -t 10 {input_file_name}'))
################################################################################
# Use FFprobe for 
# Execute ffprobe (to get specific stream entries), and get the output in JSON format
data = sp.run(shlex.split(f'ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -print_format json {input_file_name}'), stdout=sp.PIPE).stdout
dict = json.loads(data)  # Convert data from JSON string to dictionary
bit_rate = int(dict['streams'][0]['bit_rate'])  # Get the bitrate.
print(f'bit_rate = {bit_rate}')

备注:

  • 对于像MKV这样的一些视频容器,没有bit_rate信息,因此需要不同的解决方案
  • 代码示例假设ffmpeg和ffprobe(命令行工具(在执行路径中

没有bit_rate信息的容器(如MKV(的解决方案:

根据下面的帖子,我们可以求出所有视频包的大小
我们还可以对所有数据包的持续时间求和
平均比特率等于:total_size_in_bits / total_duration_in_seconds

以下是计算MKV视频文件平均比特率的代码示例:

import subprocess as sp
import shlex
import json
input_file_name = 'test.mkv'
# Build synthetic video for testing (MKV video container):
################################################################################
sp.run(shlex.split(f'ffmpeg -y -f lavfi -i testsrc=size=320x240:rate=30 -f lavfi -i sine=frequency=400 -f lavfi -i sine=frequency=1000 -filter_complex amerge -vcodec libx264 -crf 17 -pix_fmt yuv420p -acodec aac -ar 22050 -t 10 {input_file_name}'))
################################################################################
# https://superuser.com/questions/1106343/determine-video-bitrate-using-ffmpeg
# Calculating the bitrate by summing all lines except the last one, and dividing by the value in the last line.
data = sp.run(shlex.split(f'ffprobe -select_streams v:0 -show_entries packet=size,duration -of compact=p=0:nk=1 -print_format json {input_file_name}'), stdout=sp.PIPE).stdout
dict = json.loads(data)  # Convert data from JSON string to dictionary
# Sum total packets size and total packets duration.
sum_packets_size = 0
sum_packets_duration = 0
for p in dict['packets']:
sum_packets_size += float(p['size'])  # Sum all the packets sizes (in bytes)
sum_packets_duration += float(p['duration'])  # Sum all the packets durations (in mili-seconds).
# bitrate is the total_size / total_duration (multiply by 1000 because duration is in msec units, and by 8 for converting from bytes to bits).
bit_rate = (sum_packets_size / sum_packets_duration) * 8*1000
print(f'bit_rate = {bit_rate}')

最新更新