Tip
A utility script to batch convert ROS1/2 bag files to MCAP format.
This repository contains a bash script that automatically finds and converts all .bag
files in a given directory to MCAP format. The MCAP format offers better compression and faster access compared to traditional ROS bag files.
- Batch Processing: Automatically finds and processes all
.bag
files in a directory and subdirectories - Preserves Directory Structure: Maintains the relative path structure in the output
- Clean Output: Removes temporary files after conversion
- ROS2 (tested with ROS2 Humble)
ros2 bag
command-line tools- MCAP storage plugin for ROS2
Install the required ROS2 MCAP storage plugin:
sudo apt install ros-${ROS_DISTRO}-rosbag2-storage-mcap
./script/ros2mcap.sh <directory>
<directory>
: Path to the directory containing.bag
files
# Convert all bag files in the current directory
./script/ros2mcap.sh .
# Convert all bag files in a specific directory
./script/ros2mcap.sh /path/to/ros/bags
# Convert bag files with absolute path
./script/ros2mcap.sh /home/user/ros_data
The script creates a mcap
subdirectory in the input directory with the converted files:
input_directory/
├── data/
│ └── session1.bag
├── logs/
│ └── recording.bag
└── mcap/ # Created by script
├── data/
│ └── session1/ # MCAP format
└── logs/
└── recording/ # MCAP format
- Discovery: Recursively finds all
.bag
files in the specified directory - Conversion: For each bag file:
- Creates a YAML configuration file specifying input and output formats
- Uses
ros2 bag convert
to transform the file to MCAP format - Preserves the relative directory structure
- Cleanup: Removes temporary configuration files
The script uses the following MCAP settings:
- Storage ID:
mcap
You can modify these settings in the script by editing the convert.yaml
template.
The script includes basic error handling:
- Validates input directory parameter
- Creates necessary output directories
- Provides progress feedback for each file processed
If you encounter this error, ensure the MCAP storage plugin is installed:
sudo apt install ros-${ROS_DISTRO}-rosbag2-storage-mcap
Make sure the script is executable:
chmod +x script/ros2mcap.sh
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is open source under the Apache License 2.0.