(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

How to export image and video data from a bag file

Description: This tutorial explains how to export image messages from a bag file into a series of jpeg images and then goes on to show how to encode them into an OGG Theora video.

Keywords: data, rosbag, record, play, info, bag, export, video

Tutorial Level: BEGINNER

Tutorial setup

This tutorial requires that you have previously recorded a bag file which contains image data you would like to export as jpeg images or video. Additionally, this tutorial requires that the image_view package has been built and a few video utilities are installed:

   1 roscd image_view
   2 rosmake image_view --rosdep-install
   3 sudo aptitude install mjpegtools

For newer versions :

   1 roscd image_view
   2 rosmake image_view
   3 sudo apt-get install mjpegtools

This will install the necessary tools to complete the tutorial. The rest of this tutorial will assume that you have a .bag file previously created that is named test.bag and that this bag file is stored in the image_view package directory.

Exporting jpegs from bag file

To export jpeg images from a bag file first you will need to create a launch file which will dump the data. This example uses /camera/image_raw as the topic for the desired image data. This can be replaced as needed.

   1 <launch>
   2   <node pkg="rosbag" type="play" name="rosbag" required="true" args="$(find image_view)/test.bag"/>
   3   <node name="extract" pkg="image_view" type="extract_images" respawn="false" required="true" output="screen" cwd="ROS_HOME">
   4     <remap from="image" to="/camera/image_raw"/>
   5   </node>
   6 </launch>

The launch file can be started by running

   1 roslaunch export.launch

This will dump the images name frame%04d.jpg into the folder ".ros" in your home directory.

The images files can be easily to moved to where ever is convenient.

   1 cd ~
   2 mkdir test
   3 mv ~/.ros/frame*.jpg test/

If your bag file contains compressed images for example on /camera/image_raw/compressed it will be necessary for you to decompress the images before exporting.

   1 rosrun image_transport republish compressed in:=camera/image_raw raw out:=camera_out/image

Now your decompressed images will be the topic /camera_out/image, so you should change line 4 in the launch file to:

   1 <remap from="image" to="/camera_out/image"/>

Converting jpegs into an OGG Theora Video

These instructions are based on the information found here, and have been tested and shown to work.

If your camera was running at 15 frames per second then you would execute the following in a shell for reasonable results.

   1 cd ~/test
   2 jpeg2yuv -I p -f 15 -j frame%04d.jpg -b 1 > tmp.yuv
   3 ffmpeg2theora --optimize --videoquality 10 --videobitrate 16778 -o output.ogv tmp.yuv

or generate MPEG video directly from jpegs.

   1 cd ~/test
   2 mencoder "mf://*.jpg" -mf type=jpg:fps=15 -o output.mpg -speed 1 -ofps 30 -ovc lavc -lavcopts vcodec=mpeg2video:vbitrate=2500 -oac copy -of mpeg

Also, to generate an mp4 video directly from jpg on the command line, you can use -

   1 cd ~/test
   2 ffmpeg -framerate 25 -i frame%04d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4

Converting images in rosbag into an Video using jsk_rosbag_tools

jsk_rosbag_tools is a package with tools such as creating videos from rosbag and compressing rosbag images.

   1 mkdir -p ~/catkin_ws/src
   2 cd  ~/catkin_ws
   3 source /opt/ros/${ROS_DISTRO}/setup.bash
   4 catkin init
   5 cd  ~/catkin_ws/src
   6 git clone https://github.com/jsk-ros-pkg/jsk_common
   7 cd jsk_common/jsk_rosbag_tools
   8 rosdep install --from-paths -i -y -r .
   9 catkin bt
  10 source  ~/catkin_ws/devel/setup.bash

By executing bag_to_video.py, you can create a video that combines audio and video.

   1 rosrun jsk_rosbag_tools bag_to_video.py $(rospack find jsk_rosbag_tools)/samples/data/20220530173950_go_to_kitchen_rosbag.bag \
   2 --samplerate 16000 --channels 1 --audio-topic /audio \
   3 --image-topic /head_camera/rgb/throttled/image_rect_color/compressed \
   4 -o /tmp/20220530173950_go_to_kitchen_rosbag.mp4

Wiki: rosbag/Tutorials/Exporting image and video data (last edited 2022-07-03 11:47:14 by iory)