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

Is there a way to merge bag files?

asked 2011-07-21 05:55:19 -0500

I have data across multiple bag files, but I don't care about absolute timestamps, just relative time. I would like to merge the separate bag files into one bag file. For example, if I was recording images to a bag file, then later recorded more images to a different bag file, I would like to combine these bag files into a single one. Right now, I am playing them back and recording them into another bag file, but this seems clunky.

edit retag flag offensive close merge delete


1 doesn't do the time offsetting but does merge two or more bag files

lucasw gravatar image lucasw  ( 2022-06-24 10:33:32 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2011-07-21 07:13:31 -0500

tfoote gravatar image

updated 2011-07-21 07:13:53 -0500

You can checkout the rosbag Code API and associated cookbook you should be able to do exactly what you want pretty quickly.

edit flag offensive delete link more


Thanks! I ended up doing just that. I just felt like there should be a nice way to merge two bag files with the same topics. I can see this being helpful for many different types of data. Something like rosbag merge *.bag /topic
cmansley gravatar image cmansley  ( 2011-07-21 08:59:13 -0500 )edit

answered 2016-02-10 03:43:20 -0500

bchr gravatar image

updated 2016-02-10 03:43:39 -0500

bagedit provides a script that does just that:

$ --help
usage: [-h] [-o output_file] [-t topics] [-i] main_bagfile bagfile

Merges two bagfiles.

positional arguments:
  main_bagfile    path to a bagfile, which will be the main bagfile
  bagfile         path to a bagfile which should be merged to the main bagfile

optional arguments:
  -h, --help      show this help message and exit
  -o output_file  name of the output file
  -t topics       topics which should be merged to the main bag
  -i              reindex bagfile
edit flag offensive delete link more



The link seems to be broken. A copy of this script seems to be here:

moooeeeep gravatar image moooeeeep  ( 2019-09-16 05:33:40 -0500 )edit

In case you want to also add a prefix to the second bag, I've modified the above script slightly to take an optional -p[prefix] argument:

troy gravatar image troy  ( 2019-10-23 11:55:35 -0500 )edit

There's an interesting side effect of this script. The resulting merged bag is much larger than the sum of the two original bags. Maybe originally compressed messages are getting uncompressed implicitly and don't get re-compressed? I'm not sure.

vschmidt gravatar image vschmidt  ( 2021-09-30 19:03:35 -0500 )edit

Question Tools



Asked: 2011-07-21 05:55:19 -0500

Seen: 24,968 times

Last updated: Feb 10 '16