首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >录制后无法重命名rosbag文件

录制后无法重命名rosbag文件
EN

Stack Overflow用户
提问于 2019-08-08 01:44:38
回答 1查看 201关注 0票数 1

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

代码语言:javascript
复制
cfg.enable_record_to_file("some/path/filename.bag");

我尝试重命名新文件,如下所示,但没有成功。我还使用了std::experimental::filesystem::rename,结果是一样的。真正起作用的是录制第二个视频(在运行中),然后才重新命名第一个视频。这表明(两个)重命名函数都有效,但我无法更改文件名,因为该文件似乎在我当前的程序中仍处于打开状态。

代码语言:javascript
复制
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;
}

我想知道如何从管道中‘卸载’生成的视频(管道->stop()不起作用),这样我就可以动态地重命名生成的rosbag文件。

EN

回答 1

Stack Overflow用户

发布于 2019-08-08 16:58:45

正如@TedLyngmo所建议的,添加大括号允许我动态更改文件名。代码如下:

代码语言:javascript
复制
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,下面的代码解决了重命名问题。

代码语言:javascript
复制
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>();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57399755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档