【问题标题】:Cannot rename rosbag file after recording录制后无法重命名rosbag文件
【发布时间】:2019-12-15 09:45:58
【问题描述】:

我想录制一个具有特定名称的 rosbag 文件;但是,我是录制到一半才知道的,所以这里不能设置文件名:

cfg.enable_record_to_file("some/path/filename.bag");

我尝试重命名新文件,如下所示,但没有取得令人满意的成功。我还使用了 std::experimental::filesystem::rename,结果相同。起作用的是录制第二个视频(即时),然后才重命名第一个。这表明(两个)重命名功能都有效,但我无法更改文件名,因为文件似乎在我当前的程序中仍处于打开状态。

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;
}

我想知道如何从管道中“卸载”生成的视频(pipe->stop() 不起作用),所以我可以即时重命名生成的 rosbag 文件。

【问题讨论】:

  • 这是在 Windows 上吗?如果是这样,我假设您的程序或其他程序打开了该文件。试试这个:在rs2::config cfg; 之前添加一个{ 并在pipe-&gt;stop(); 之后添加一个} 以查看当所有rs2 变量超出范围时它是否已关闭。
  • @TedLyngmo 是的,这是 Windows,但我看不出任何其他操作系统会有所不同的原因。无论如何,我按照建议添加了大括号,这似乎可以解决问题。谢谢!
  • 不客气!不同之处在于,在 Posix 系统中,您可以在文件打开时对其进行重命名。

标签: c++ record realsense


【解决方案1】:

正如@TedLyngmo 所建议的,添加大括号可以让我即时更改文件名。代码如下:


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;
}

编辑

我进一步研究了rs-record-playback,下面的代码修复了重命名问题。


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>();
}

【讨论】:

  • 太好了,现在您只需要弄清楚是哪个对象使文件保持打开状态,看看是否有办法让它在不限定该对象范围的情况下关闭文件。
猜你喜欢
  • 2010-09-14
  • 2013-08-31
  • 2012-04-23
  • 1970-01-01
  • 2013-08-20
  • 2012-07-08
  • 2020-04-05
  • 2021-04-21
  • 2015-10-08
相关资源
最近更新 更多