How to calculate the bitrate of a video per second?

0
0
No Preview.

If you feel like this example could be improved, you may edit this example here.

# extract the packet size into packets.csv
ffprobe -select_streams v -show_packets -show_entries packet=pts_time,size -of csv input.mp4 > packets.csv

# to calculate the bitrate of each second of video
# write this to calc-bitrate.py
import csv
from collections import defaultdict

# Load packet data from ffprobe output
time_buckets = defaultdict(int)  # key: second (int), value: total size in bytes

with open('packets.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        if len(row) < 3:
            continue
        pts_time = float(row[1])
        size_bytes = int(row[2])
        second = int(pts_time)  # floor to the second
        time_buckets[second] += size_bytes

# Print bitrate per second
print("Second\tBitrate (kbps)")
for sec in sorted(time_buckets.keys()):
    bitrate_kbps = (time_buckets[sec] * 8) / 1000  # bytes to kilobits
    print(f"{sec}\t{bitrate_kbps:.2f}")

Related Issues not found

Please contact @eladg to initialize the comment