Last active
October 14, 2025 07:45
-
-
Save munzz11/1131f18b4134094a70db4e451040e08f to your computer and use it in GitHub Desktop.
Simple ROS .bag to .mp4 conversion
This file contains hidden or 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
| import rosbag | |
| import cv2 | |
| from cv_bridge import CvBridge | |
| import argparse | |
| import glob | |
| def create_video(bag_file, image_topic, output_video): | |
| bridge = CvBridge() | |
| bag = rosbag.Bag(bag_file, 'r') | |
| # Define the codec and video writer | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| frame_rate = 10 # You can adjust this as needed | |
| frame_size = None # Will be determined from the first frame | |
| video_writer = None | |
| for topic, msg, t in bag.read_messages(topics=[image_topic]): | |
| try: | |
| frame = bridge.imgmsg_to_cv2(msg, "bgr8") | |
| except Exception as e: | |
| print(f"Error converting message: {e}") | |
| continue | |
| if frame_size is None: | |
| frame_size = (frame.shape[1], frame.shape[0]) | |
| video_writer = cv2.VideoWriter(output_video, fourcc, frame_rate, frame_size) | |
| video_writer.write(frame) | |
| bag.close() | |
| if video_writer is not None: | |
| video_writer.release() | |
| print(f"Video saved as {output_video}") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Convert ROS1 bag image topic to MP4 video") | |
| parser.add_argument("bag_files", nargs="+", help="Paths to the ROS1 bag files (supports wildcards like *.bag)") | |
| parser.add_argument("image_topic", help="Image topic to extract from the bag file") | |
| parser.add_argument("output_directory", help="Directory to save the output MP4 videos") | |
| args = parser.parse_args() | |
| for bag_file in args.bag_files: | |
| bag_paths = glob.glob(bag_file) | |
| for path in bag_paths: | |
| output_video = f"{args.output_directory}/{path.split('/')[-1].replace('.bag', '.mp4')}" | |
| create_video(path, args.image_topic, output_video) |
Author
Thank you so much for sharing your code!
In my case, I had to convert bags containing both images and audio into an mp4 file. Starting from your implementation made the task so much easier!
I hope it is not too forward, but I have extended your work to support audio as well. I am leaving a link to my implementation here in case others passing by might need this additional functionality.
That said, for anyone looking for a straightforward bag-to-mp4 conversion with images only, I highly recommend sticking with @munzz11 original implementation which is way cleaner.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
usage: bag2video.py [-h] bag_files [bag_files ...] image_topic output_directoryExample:
If you want to convert a batch of .bag files from in the directory they exist in, simply run:
python3 bag2video.py *.bag /image_raw output