librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

cutting a .bag file

Open Yonatan-Sade opened this issue 8 years ago • 22 comments

Required Info
Camera Model D400
Operating System & Version Win (8.1/10)
Platform PC
Language C

Issue Description

Sorry for the shortness - Is there any option to cut a .bag file? I have an enormous .bag file from my camera (25 Giga) And I only to save only 10 second from it to another .bag file. How can I do that? I looked over the rs-record-playback example, but it seems that it will work only for streaming from device/sensor but NOT from file. Any idea?

Thank you very much Yonatan Sade

Yonatan-Sade avatar Jun 11 '18 13:06 Yonatan-Sade

Are you talking about ROS bag files? In that case, you can use rosbag play with --start and --duration options to replay only a part of the recorded messages.

GuidoBartoli avatar Jun 11 '18 14:06 GuidoBartoli

Hello @Yonatan-Sade , in the latest release 2.12.0 we added a tool that splits rosbag into separate (single) frames : rs-convert. Though this is not precisely the functionality that you're asking for, you still may find it useful.

ev-mp avatar Jun 11 '18 15:06 ev-mp

Let's say I convert this .bag file into million separated frames. Is there any way to collapse some specific frames into another (small) .bag file?

Yonatan-Sade avatar Jun 14 '18 20:06 Yonatan-Sade

Not sure if it can be used on Windows easily, but I would try doing this with rosbag filter, see more info at http://wiki.ros.org/rosbag/Commandline#filter.

mbuijs avatar Jun 15 '18 08:06 mbuijs

[Realsense Customer Engineering Team Comment] Hi @Yonatan-Sade,

Did you try the suggestiosn from @GuidoBartoli and @jbvo to see if it's helpful?

  1. The @GuidoBartoli approach is not addressing my need since I don't want to play the file. I want to process a small one.
  2. The @jbvo approach should be checked.. (do I need some environment to run rosbag filter?)

Yonatan-Sade avatar Jun 25 '18 11:06 Yonatan-Sade

rosbag filter doesn't require an environment, on the wiki that I linked to there's a description on how it's supposed to be used, e.g. which variables are available in the python statement that you provide to it.

Examples:

rosbag filter my.bag only-tf.bag "topic == '/tf'"
rosbag filter my.bag sliced.bag "t.secs > 100 and t.secs < 110"

mbuijs avatar Jun 26 '18 08:06 mbuijs

Does it work in windows?

Yonatan-Sade avatar Jun 28 '18 20:06 Yonatan-Sade

I don't know, I'm only working on Ubuntu. I think theoretically is should be possible because rosbag filter is implemented in python completely, but in not sure if someone is actually distributing it.

mbuijs avatar Jun 28 '18 22:06 mbuijs

[Realsense Customer Engineering Team Comment] Hi @Yonatan-Sade,

Does it work for you per @jbvo's feedback?

Did not try (I have only windows) Meanwhile, we moved to ZED..

Yonatan-Sade avatar Jul 09 '18 16:07 Yonatan-Sade

Hi I tried to trim the rosbag using: https://github.com/IntelRealSense/librealsense/issues/1868#issuecomment-400223270 But when I try to replay it using the realsense-viewer I get the following error: "Failed to load file /media/.../1_trim.bag. Reason: Failed to create ros reader: Invalid format, file does not contain topic "/file_version" nor "FILE_VERSION"

jfcisneros avatar Dec 03 '18 18:12 jfcisneros

@jfcisneros: I got this working as described in #1868 (comment) by digging through the .bag files that are generated by RealSense Viewer. It looks like RealSense stores important metadata in some of the messages. These messages all have a timestamp with secs = 0 and nsecs = 1.

For example, I was able to generate a trimmed .bag file that is usable by RealSense Viewer using this command:

rosbag filter my.bag sliced.bag "(t.secs==0 and t.nsecs==1) or (t.secs>=10 and t.secs<=20)"

You can see all these topics by using the -p option with rosbag. i.e.:

rosbag filter -p '"%4d.%09d: %s" % (t.secs, t.nsecs, topic)'  my.bag test.bag "(t.secs==0 and t.nsecs == 1)"

... resulting in something like the following output. Note at the top, we see that all-important /file_version topic:

MATCH    0.000000001: /file_version
MATCH    0.000000001: /device_0/sensor_0/Depth_0/info
MATCH    0.000000001: /device_0/sensor_0/Depth_0/info/camera_info
MATCH    0.000000001: /device_0/sensor_0/Infrared_1/info
MATCH    0.000000001: /device_0/sensor_0/Infrared_1/info/camera_info
MATCH    0.000000001: /device_0/sensor_0/Infrared_2/info
MATCH    0.000000001: /device_0/sensor_0/Infrared_2/info/camera_info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/info
MATCH    0.000000001: /device_0/sensor_0/info
MATCH    0.000000001: /device_0/sensor_0/option/Exposure/value
MATCH    0.000000001: /device_0/sensor_0/option/Exposure/description
MATCH    0.000000001: /device_0/sensor_0/option/Gain/value
MATCH    0.000000001: /device_0/sensor_0/option/Gain/description
MATCH    0.000000001: /device_0/sensor_0/option/Enable Auto Exposure/value
MATCH    0.000000001: /device_0/sensor_0/option/Enable Auto Exposure/description
MATCH    0.000000001: /device_0/sensor_0/option/Visual Preset/value
MATCH    0.000000001: /device_0/sensor_0/option/Visual Preset/description
MATCH    0.000000001: /device_0/sensor_0/option/Laser Power/value
MATCH    0.000000001: /device_0/sensor_0/option/Laser Power/description
MATCH    0.000000001: /device_0/sensor_0/option/Emitter Enabled/value
MATCH    0.000000001: /device_0/sensor_0/option/Emitter Enabled/description
MATCH    0.000000001: /device_0/sensor_0/option/Frames Queue Size/value
MATCH    0.000000001: /device_0/sensor_0/option/Frames Queue Size/description
MATCH    0.000000001: /device_0/sensor_0/option/Asic Temperature/value
MATCH    0.000000001: /device_0/sensor_0/option/Asic Temperature/description
MATCH    0.000000001: /device_0/sensor_0/option/Error Polling Enabled/value
MATCH    0.000000001: /device_0/sensor_0/option/Error Polling Enabled/description
MATCH    0.000000001: /device_0/sensor_0/option/Projector Temperature/value
MATCH    0.000000001: /device_0/sensor_0/option/Projector Temperature/description
MATCH    0.000000001: /device_0/sensor_0/option/Output Trigger Enabled/value
MATCH    0.000000001: /device_0/sensor_0/option/Output Trigger Enabled/description
MATCH    0.000000001: /device_0/sensor_0/option/Depth Units/value
MATCH    0.000000001: /device_0/sensor_0/option/Depth Units/description
MATCH    0.000000001: /device_0/sensor_0/option/Stereo Baseline/value
MATCH    0.000000001: /device_0/sensor_0/option/Stereo Baseline/description
MATCH    0.000000001: /device_0/sensor_0/option/Inter Cam Sync Mode/value
MATCH    0.000000001: /device_0/sensor_0/option/Inter Cam Sync Mode/description
MATCH    0.000000001: /device_0/sensor_0/option/Emitter On Off/value
MATCH    0.000000001: /device_0/sensor_0/option/Emitter On Off/description
MATCH    0.000000001: /device_0/sensor_0/option/Global Time Enabled/value
MATCH    0.000000001: /device_0/sensor_0/option/Global Time Enabled/description
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_0/post_processing
MATCH    0.000000001: /device_0/sensor_1/info
MATCH    0.000000001: /device_0/sensor_1/option/Backlight Compensation/value
MATCH    0.000000001: /device_0/sensor_1/option/Backlight Compensation/description
MATCH    0.000000001: /device_0/sensor_1/option/Brightness/value
MATCH    0.000000001: /device_0/sensor_1/option/Brightness/description
MATCH    0.000000001: /device_0/sensor_1/option/Contrast/value
MATCH    0.000000001: /device_0/sensor_1/option/Contrast/description
MATCH    0.000000001: /device_0/sensor_1/option/Exposure/value
MATCH    0.000000001: /device_0/sensor_1/option/Exposure/description
MATCH    0.000000001: /device_0/sensor_1/option/Gain/value
MATCH    0.000000001: /device_0/sensor_1/option/Gain/description
MATCH    0.000000001: /device_0/sensor_1/option/Gamma/value
MATCH    0.000000001: /device_0/sensor_1/option/Gamma/description
MATCH    0.000000001: /device_0/sensor_1/option/Hue/value
MATCH    0.000000001: /device_0/sensor_1/option/Hue/description
MATCH    0.000000001: /device_0/sensor_1/option/Saturation/value
MATCH    0.000000001: /device_0/sensor_1/option/Saturation/description
MATCH    0.000000001: /device_0/sensor_1/option/Sharpness/value
MATCH    0.000000001: /device_0/sensor_1/option/Sharpness/description
MATCH    0.000000001: /device_0/sensor_1/option/White Balance/value
MATCH    0.000000001: /device_0/sensor_1/option/White Balance/description
MATCH    0.000000001: /device_0/sensor_1/option/Enable Auto Exposure/value
MATCH    0.000000001: /device_0/sensor_1/option/Enable Auto Exposure/description
MATCH    0.000000001: /device_0/sensor_1/option/Enable Auto White Balance/value
MATCH    0.000000001: /device_0/sensor_1/option/Enable Auto White Balance/description
MATCH    0.000000001: /device_0/sensor_1/option/Frames Queue Size/value
MATCH    0.000000001: /device_0/sensor_1/option/Frames Queue Size/description
MATCH    0.000000001: /device_0/sensor_1/option/Power Line Frequency/value
MATCH    0.000000001: /device_0/sensor_1/option/Power Line Frequency/description
MATCH    0.000000001: /device_0/sensor_1/option/Auto Exposure Priority/value
MATCH    0.000000001: /device_0/sensor_1/option/Auto Exposure Priority/description
MATCH    0.000000001: /device_0/sensor_1/option/Global Time Enabled/value
MATCH    0.000000001: /device_0/sensor_1/option/Global Time Enabled/description
MATCH    0.000000001: /device_0/sensor_1/post_processing
MATCH    0.000000001: /device_0/sensor_2/info
MATCH    0.000000001: /device_0/sensor_2/option/Frames Queue Size/value
MATCH    0.000000001: /device_0/sensor_2/option/Frames Queue Size/description
MATCH    0.000000001: /device_0/sensor_2/option/Global Time Enabled/value
MATCH    0.000000001: /device_0/sensor_2/option/Global Time Enabled/description
MATCH    0.000000001: /device_0/sensor_0/Infrared_1/tf/0
MATCH    0.000000001: /device_0/sensor_0/Infrared_2/tf/0
MATCH    0.000000001: /device_0/sensor_0/Depth_0/tf/0
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/data
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
NO MATCH    0.000000600: /device_0/sensor_0/Infrared_1/image/metadata
[etc...]

DSDev-NickHogle avatar Jan 09 '20 04:01 DSDev-NickHogle

@DSDev-NickHogle I did exactly as you said but I still can't read it using the intelrealsense SDK (cf images below). But note that I can open it using rqt_bag

Capture d’écran du 2022-05-04 18-58-03

Capture d’écran du 2022-05-04 17-27-47

julienguegan avatar May 04 '22 15:05 julienguegan

@DSDev-NickHogle I did exactly as you said but I still can't read it using the intelrealsense SDK (cf images below). But note that I can open it using rqt_bag

@julienguegan : Hmm.. Do you think the HDR Merge filter error message has something to do with it? Looking at the code, that warning is generated if there is metadata missing... Perhaps you'd need to also include those .../metadata frames too?

Maybe something like this would work? (Note, I have not tested this):

rosbag filter my.bag sliced.bag "(t.secs==0 and t.nsecs==1) or (t.secs>=10 and t.secs<=20) or topic.endswith('/metadata')"

DSDev-NickHogle avatar May 13 '22 17:05 DSDev-NickHogle

Hi all! Sorry to bother, are there any updates? Can anyone explain how to cut bag file with ability to open it in the IntelRealSense Viewer? I've tried the command below but I got the same error "Failed to read next frame ..." as comment rosbag filter my.bag sliced.bag "(t.secs==0 and t.nsecs==1) or (t.secs>=10 and t.secs<=20)"

Adding or topic.endswith('/metadata') just copies the initial file

temav avatar Oct 18 '22 15:10 temav

@DSDev-NickHogle I did exactly as you said but I still can't read it using the intelrealsense SDK (cf images below). But note that I can open it using rqt_bag

Capture d’écran du 2022-05-04 18-58-03

Capture d’écran du 2022-05-04 17-27-47

I have the same problem.

Did you find any solution?

antoniogrimalt avatar Jun 05 '24 19:06 antoniogrimalt

Hi @antoniogrimalt The RealSense SDK uses a bag format that is based on the ROS rosbag standard but has some differences - as described at https://github.com/IntelRealSense/librealsense/issues/3020#issuecomment-452321766 - and so a rosbag recorded in ROS with rosbag record may not be able to be played back in the RealSense Viewer.

MartyG-RealSense avatar Jun 06 '24 10:06 MartyG-RealSense

Hello, I also needed this.

I've found this thread and the findings here helped me come up with a script that actually works on the Realsense Viewer v2.55.1, it works with recordings made by the RealSense D455 with firmware version 5.16.0.1 (the only camera I have) either by the Python API or the RealSense app.

rs_trimmed_example

The way I made this work is by using the Python ROS modules ( rospag, rospy), I figured out that plainly reading the bag and saving it ( read_messages and write ) even including the header topics would result in an incompatible RS bag probably because some encoding error. Thankfully there exists a raw flag, setting it seems to do the trick.

I've written a small working Python script here that trims the RealSense bag from an RGB frame number to another frame, or either the start of the recording to a given frame or a given frame to the end of the recording.

It plays on the RS app although the controls bug out a bit: The timeline doesn't go all the way to the real start or end of the recording, but if you let it play it, it plays from the exact frames. Maybe there's another ROS topic that saves the length of the original file and it bugs out, I haven't found it. In the Python API it seems to play perfectly.

Hope this is useful to someone in the future.

Also, It'd be great if the RealSense team would consider adding this functionality (trimming a RS bag) to the API, since it's quite convenient in many scenarios.

RhinoBlindado avatar Jul 30 '24 10:07 RhinoBlindado

Thanks so much @RhinoBlindado for sharing your experience and your Python script at https://gist.github.com/RhinoBlindado/4550691ded75570d6d0ee82d8f90229d

MartyG-RealSense avatar Jul 30 '24 10:07 MartyG-RealSense