ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The rosbag python module has several ways to count the number of messages in a rosbag file. Unfortunately there doesn't seem to be a public API, you often have to just read the comments in the code.

To get the total number of messages:

bag.get_message_count()

You can also filter for a specific topic name

bag.get_message_count('/tf')

will count the number of messages on the '/tf' topic. Note this is the topic name, not the message type.


If you want slightly more information, you can use

bag.get_type_and_topic_info().topics

and you will get a whole lot more information, including the number of messages per topic, the frequency, etc...

{'/joint_states': TopicTuple(msg_type='sensor_msgs/JointState', message_count=2500, connections=1, frequency=500.99187768752984),
 '/rosout': TopicTuple(msg_type='rosgraph_msgs/Log', message_count=35, connections=1, frequency=67108.864),
 '/tf': TopicTuple(msg_type='tf2_msgs/TFMessage', message_count=7697, connections=1, frequency=32768.0)}