from mcap.reader import make_reader
from mcap_ros2.decoder import DecoderFactory
from tqdm import tqdm
mcap_file = "hoge.mcap"
topic_sizes = {}
with open(mcap_file, "rb") as f:
reader = make_reader(f, decoder_factories=[DecoderFactory()])
for schema, channel, message, ros_msg in tqdm(reader.iter_decoded_messages(), dynamic_ncols=True):
topic = channel.topic
msg_size = len(message.data)
if topic not in topic_sizes:
topic_sizes[topic] = 0
topic_sizes[topic] += msg_size
for topic, size in topic_sizes.items():
# バイトからMBに変換
size_mb = size / (1024 * 1024)
print(f"Topic: {topic}, Size: {size_mb:.2f} MB")
print(f"Total size: {sum(topic_sizes.values()) / (1024 * 1024):.2f} MB")