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

How to split a recorded rosbag file ?

asked 2013-11-10 21:36:33 -0500

sai gravatar image

updated 2021-11-07 09:43:49 -0500

lucasw gravatar image

Ubuntu 12.04 and fuerte

I have a bag file of 400 seconds length.

I would like to split it into two bag files of 150 seconds and 250 seconds.

There is an option to split bag files while recording using option --split, but now I already have a recorded one.

What can be done?


edit retag flag offensive close merge delete



Is it not possible to just play back the bag and rerecord the topics using the --split option?

jdlangs gravatar image jdlangs  ( 2013-11-11 06:46:14 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2016-05-09 22:57:34 -0500

M@t gravatar image

updated 2016-05-09 23:27:04 -0500

My answer is pretty redundant coming three years late, but for anyone else who asks this another option is to use the rosbag filter command, e.g.

rosbag filter input.bag output.bag "t.secs <= 1284703931.86"

You can use the standard greater-than-or-equal-to (>=) and less-than-or-qual-to (<=) operators to keep all data before or after your cutoff point. Keep in mind that ROS time-stamps each message with Unix time, rather than the time of the message relative to the start of the record. So you'll need to find the start and end times of your record and do a bit of basic math to find the 150 sec point.

For other filter commands, take a look at rosbag/Commandline

edit flag offensive delete link more


I think the misleading part could be the use of seconds in this case because I believe the time-stamp is given in the computer's epoch time.

ubuntuslave gravatar image ubuntuslave  ( 2018-02-20 10:40:53 -0500 )edit

Is Unix time and a computer's epoch time not one and the same?

M@t gravatar image M@t  ( 2018-02-22 14:42:49 -0500 )edit

for bounding with a startpoint and an endpoint, the expression looks like this:

rosbag filter input.bag output.bag "t.secs >= 1531425960 and t.secs <= 1531426140"
KH - gravatar image KH -  ( 2018-07-13 13:13:39 -0500 )edit

Please consider changing t.secs to t.to_sec(), because your version will be rounded and if you need higher time precision, you will and up having some issues.

max11gen gravatar image max11gen  ( 2019-12-06 04:39:19 -0500 )edit

Thanks @max11gen, I just ran into this exact issue.

lukehutch gravatar image lukehutch  ( 2020-08-15 01:07:11 -0500 )edit

answered 2013-11-11 06:46:48 -0500

lindzey gravatar image

I don't know if there's an existing utility for doing this, but it would be pretty simple to do using rosbag's python API. Check out the examples in the cookbook.

You could also try replaying it, and recording all messages with rosbag record -a --split --duration=250.

edit flag offensive delete link more

answered 2018-05-01 17:09:55 -0500

lucasw gravatar image

Here is a bash script to split a bag into two using a parameter from 0.0 to 1.0- 0.5 would produce two half length bags:

# provide input bag, output bags prefix, and time fraction
echo $1, $2, $3
t0=`rosbag info -y -k start $1`
t1=`rosbag info -y -k end $1`
tfr=`echo "$t0 + ($t1 - $t0) * $3" | bc -l`
echo $t0, $t1, $tfr
rosbag filter $1 $2_a.bag "t.secs <= $tfr"
rosbag filter $1 $2_b.bag "t.secs > $tfr"
edit flag offensive delete link more


Very helpful! Thx

yusanshi gravatar image yusanshi  ( 2019-12-22 22:01:11 -0500 )edit

Question Tools



Asked: 2013-11-10 21:36:33 -0500

Seen: 63,869 times

Last updated: Nov 07 '21