Ask Your Question

Renaming a topic inside a bag file

asked 2011-03-11 23:59:03 -0600

dlmypr gravatar image

When we record a bag file for example X.bag and it contains the topic data /a_topic

Is there any way to rename the topics name inside the X.bag /a_topic to /b_topic?

Best regards..

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted

answered 2011-03-15 19:06:26 -0600

Tim Field gravatar image

I'd use the rosbag Python API:

from rosbag import Bag
with Bag('Y.bag', 'w') as Y:
    for topic, msg, t in Bag('X.bag'):
        Y.write('/b_topic' if topic == '/a_topic' else topic, msg, t)
edit flag offensive delete link more


Awesome...saved me a bunch of time finding this. Though I do have one oddity as a result - the bag is now 2x the size. Anyone know why?

Daniel Stonier gravatar image Daniel Stonier  ( 2014-07-19 09:02:10 -0600 )edit

Every time you use rosbag Python API, and I think also rosbag filter, the output bag is uncompressed. You have to rosbag compress after that. I'd say that's what happened to you.

Enrique gravatar image Enrique  ( 2016-01-23 20:38:40 -0600 )edit

how can you actually replace a topic name by another? how could one change the code for that?

rubot gravatar image rubot  ( 2018-07-25 14:40:04 -0600 )edit

Here is my code:

from rosbag import Bag
with Bag('Y.bag', 'w') as Y:
for topic, msg, t in Bag('X.bag'):
    if topic == '/a_topic':
        Y.write('/b_topic', msg, t)
        Y.write(topic, msg, t)
kentam gravatar image kentam  ( 2018-10-31 18:00:37 -0600 )edit

answered 2015-04-16 14:34:18 -0600

Enrique gravatar image

updated 2016-01-23 20:36:50 -0600

I know this is quite old, but there's also this option:

rosrun rosbag <in topic> <in bag> <out topic> <out bag>

Another option is to simply remap the topics when you use rosbag play. For example, if you have the topic /foo inside the bag, you can to this to rename it (when published) into /bar:

rosbag play file.bag /foo:=/bar

The leading '/' might not be needed.

edit flag offensive delete link more


yes. very good! it's very convenient!

asimay_y gravatar image asimay_y  ( 2016-06-07 04:34:22 -0600 )edit

This info is very good!

harderthan gravatar image harderthan  ( 2018-08-15 20:28:09 -0600 )edit

answered 2011-03-13 11:22:12 -0600

William gravatar image

You might consider using the tf_remap node that comes with the tf package:

From the wiki:


tf_remap listens to the /tf_old topic and republishes transforms on the /tf topic. This is mainly used with out-of-date bag files that need their coordinate frame IDs updated. The typical operation for this node is to play the bag file with /tf:=/old_tf. The tf_remap node is run with a ~mappings parameter that describes the mapping of frame IDs from old to new.

edit flag offensive delete link more


This will only work if @dlmpyr is trying to do stuff with /tf. If it's some other topic, tf_remap probably won't work.
Eric Perko gravatar image Eric Perko  ( 2011-03-13 15:37:06 -0600 )edit

answered 2011-03-12 00:16:00 -0600

Eric Perko gravatar image

Not sure if rosbag filter can do it, but you should be able to rename the topics using the rosbag API. For an example that reads in data on the tf topic and then writes it back out, see rosbag cookbook. That should be a good example to get you started.

edit flag offensive delete link more


Thank you for the response. I also couldn't find anything about the filter. I changed the topic names of openni_camera directly as another approach.
dlmypr gravatar image dlmypr  ( 2011-03-12 03:32:31 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2011-03-11 23:59:03 -0600

Seen: 12,187 times

Last updated: Jan 23 '16