Skip to content

Instantly share code, notes, and snippets.

@srathi-monarch
Last active July 8, 2024 04:53
Show Gist options
  • Save srathi-monarch/d502cd6e7afc423b7bf2ec4327a234d3 to your computer and use it in GitHub Desktop.
Save srathi-monarch/d502cd6e7afc423b7bf2ec4327a234d3 to your computer and use it in GitHub Desktop.
Joins rosbags that are in chronological order into a single bagfile.
#! /usr/bin/python3
import sys
from pathlib import Path
import rosbag
from tqdm import tqdm
def main():
# Read the name of bagfiles from the command line args
# Merge the bags and create a new bagfile output
if len(sys.argv) < 3:
print("Usage: join_bags.py bag1 bag2 [bagn...] <output>")
sys.exit(1)
bagfiles = sys.argv[1:-1]
output = sys.argv[-1]
for file in bagfiles:
if not Path(file).is_file():
print(f"File {file} not found")
sys.exit(1)
if Path(output).is_file():
print(f"File {output} already exists")
sys.exit(1)
print("Starting Merge")
with rosbag.Bag(output, 'w') as outbag:
for bagfile in tqdm(bagfiles, unit="bags"):
with rosbag.Bag(bagfile, 'r') as inbag:
for topic, msg, t in tqdm(inbag.read_messages(), total=inbag.get_message_count(), unit="msgs"):
outbag.write(topic, msg, t)
print("Done")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment