Created
September 5, 2017 14:17
-
-
Save zxf8665905/7f77d160eb442b1cf01066e53c9210a3 to your computer and use it in GitHub Desktop.
split large ros bag to small bag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import rosbag | |
import os | |
from shutil import copyfile | |
import glob | |
bags_path = glob.glob('/home/aaa/dataset/*.bag') | |
for bag_file in bags_path: | |
bag = rosbag.Bag(bag_file) | |
bag_prefix = bag_file.split('.bag')[0]+'_' | |
time_step = 50 | |
print "Reading bag. Please wait..." | |
it = bag.read_messages() | |
last_ts = bag.get_start_time() | |
index = 1 | |
print "Reading done!" | |
tag = str(index).zfill(3) | |
bag_dir = bag_prefix+tag | |
wbag = rosbag.Bag(bag_dir+'.bag', 'w') | |
for msg in it: | |
ts = msg.timestamp | |
ts = ts.to_sec() | |
if (ts-last_ts) > time_step: | |
wbag.close() | |
print 'Bag # '+str(index)+' written.' | |
index += 1 | |
tag = str(index).zfill(3) | |
bag_dir = bag_prefix+tag | |
print('write: {}'.format(bag_dir+'.bag')) | |
wbag = rosbag.Bag(bag_dir+'.bag', 'w') | |
last_ts = ts | |
wbag.write(msg.topic, msg.message, msg.timestamp) | |
wbag.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment