Reputation: 59
I would like to record a rosbag file with a specific name; however, I only know it after the recording is halfway done, so I cannot set the filename here:
cfg.enable_record_to_file("some/path/filename.bag");
I have tried to rename the new file as shown below without satisfactory success. I have also used std::experimental::filesystem::rename, with the same outcome. What did work was to record a second video (on the fly) and only then rename the first. This indicates that the (both) rename functions work, but I cannot change the filename it as it seems the file is still open in my current program.
int main(int argc, char* argv[])
{
rs2::config cfg;
rs2::device device;
auto pipe = std::make_shared<rs2::pipeline>();
cfg.disable_all_streams();
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
cfg.enable_record_to_file("tmp.bag");
pipe->start(cfg);
device = pipe->get_active_profile().get_device();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
device.as<rs2::recorder>().pause();
pipe->stop();
int res = std::rename("tmp.bag", "test.bag");
if (res == 0)
std::cout << "File successfully renamed" << std::endl;
else
std::cout << "Error renaming file" << std::endl;
return 0;
}
I wonder how to 'unload' the produced video from the pipeline (pipe->stop() did not work), so I can rename the generated rosbag files on the fly.
Upvotes: 1
Views: 663
Reputation: 59
As suggested by @TedLyngmo, adding curly brackets allowed me to change the filename on the fly. Code as follows:
int main(int argc, char* argv[])
{
{
rs2::config cfg;
rs2::device device;
auto pipe = std::make_shared<rs2::pipeline>();
cfg.disable_all_streams();
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
cfg.enable_record_to_file("tmp.bag");
pipe->start(cfg);
device = pipe->get_active_profile().get_device();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
device.as<rs2::recorder>().pause();
pipe->stop();
}
int res = std::rename("tmp.bag", "test.bag");
if (res == 0)
std::cout << "File successfully renamed" << std::endl;
else
std::cout << "Error renaming file" << std::endl;
return 0;
}
EDIT
I studied rs-record-playback a bit more and the code below fixes the renaming issue.
int main(int argc, char* argv[])
{
rs2::device device;
auto pipe = std::make_shared<rs2::pipeline>();
pipe->start();
device = pipe->get_active_profile().get_device();
if (!device.as<rs2::recorder>())
{
pipe->stop(); // Stop the pipeline with the default configuration
pipe = std::make_shared<rs2::pipeline>();
rs2::config cfg;
cfg.disable_all_streams();
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
cfg.enable_record_to_file("tmp.bag");
pipe->start(cfg);
device = pipe->get_active_profile().get_device();
}
// record for 1 sec, if conditions can be added to check if recorder is initialised
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// if condition can be added to check if recording has been completed
pipe->stop();
pipe = std::make_shared<rs2::pipeline>(); // reset the shared pointer
pipe->start();
device = pipe->get_active_profile().get_device();
int res = std::rename("tmp.bag", "testing.bag");
if (res == 0)
std::cout << "File successfully renamed" << std::endl;
else
std::cout << "Error renaming file" << std::endl;
pipe->stop();
pipe = std::make_shared<rs2::pipeline>();
}
Upvotes: 1