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

Some simple operations can be done from the command line. For example, if you just want to remove the camera info messages, you can do:

rosbag filter orig.bag filtered.bag "m._type != 'sensor_msgs/CameraInfo'"

If you need to mutate messages in a bag file or some other more complicated operation, the easiest way is to write a Python script using the rosbag code API. Below is a script I used at some point to replace the value of header.frame_id for all CameraInfo and Image messages in one or more bags:

#!/usr/bin/env python

PKG = 'sensor_msgs'
import roslib; roslib.load_manifest(PKG)
import rospy
import rosrecord
import fileinput
import os

def fix(inbags):
  for b in inbags:
    print "Trying to migrating file: %s"%b
    outbag = b+'.tmp'
    rebag = rosrecord.Rebagger(outbag)
    try:
      for (topic, msg, t) in rosrecord.logplayer(b, raw=False):
        if msg._type == 'sensor_msgs/CameraInfo' or msg._type == 'sensor_msgs/Image':
          msg.header.frame_id = 'l_forearm_cam_optical_frame'
          rebag.add(topic, msg, t, raw=False)
        else:
          rebag.add(topic, msg, t, raw=False)
      rebag.close()
    except rosrecord.ROSRecordException, e:
      print " Migration failed: %s"%(e.message)
      os.remove(outbag)
      continue

    oldnamebase = b+'.old'
    oldname = oldnamebase
    i = 1
    while os.path.isfile(oldname):
      i=i+1
      oldname = oldnamebase + str(i)
    os.rename(b, oldname)
    os.rename(outbag, b)
    print " Migration successful.  Original stored as: %s"%oldname

if __name__ == '__main__':
  import sys
  if len(sys.argv) >= 2:
    fix(sys.argv[1:])
  else:
    print "usage: %s bag1 [bag2 bag3 ...]" % sys.argv[0]