Skip to content

Commit 0f6fca3

Browse files
committed
added compress_bag and fixed some bugs
1 parent 244fd25 commit 0f6fca3

File tree

6 files changed

+354
-29
lines changed

6 files changed

+354
-29
lines changed

CMakeLists.txt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ find_package(rosbag2_cpp REQUIRED)
2828
find_package(rosbag2_storage REQUIRED)
2929
find_package(sensor_msgs REQUIRED)
3030

31-
foreach(pkg ${ament_dependencies})
32-
find_package(${pkg} REQUIRED)
33-
endforeach()
31+
if(${rosbag2_cpp_VERSION_MAJOR} GREATER 0 OR ${rosbag2_cpp_VERSION_MINOR} GREATER 9)
32+
add_definitions(-DUSE_NEW_ROSBAG_WRITE_INTERFACE)
33+
endif()
3434

3535
if(${cv_bridge_VERSION} VERSION_GREATER "3.3.0")
3636
add_definitions(-DUSE_CV_BRIDGE_HPP)
@@ -73,9 +73,25 @@ target_link_libraries(bag_to_frames
7373
target_compile_features(bag_to_frames PRIVATE cxx_std_17)
7474
target_include_directories(bag_to_frames PRIVATE include)
7575

76+
#
77+
# -------- compress_bag
78+
#
79+
add_executable(compress_bag src/compress_bag.cpp)
80+
target_link_libraries(compress_bag
81+
ffmpeg_encoder_decoder::ffmpeg_encoder_decoder
82+
${ffmpeg_image_transport_msgs_TARGETS}
83+
rclcpp::rclcpp
84+
rosbag2_cpp::rosbag2_cpp
85+
rosbag2_storage::rosbag2_storage
86+
${sensor_msgs_TARGETS})
87+
target_compile_features(compress_bag PRIVATE cxx_std_17)
88+
target_include_directories(compress_bag PRIVATE include)
89+
90+
7691
install(TARGETS
7792
bag_to_file
7893
bag_to_frames
94+
compress_bag
7995
DESTINATION lib/${PROJECT_NAME}/)
8096

8197
if(BUILD_TESTING)

include/ffmpeg_image_transport_tools/bag_processor.hpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <rclcpp/serialized_message.hpp>
2626
#include <rosbag2_cpp/reader.hpp>
2727
#include <rosbag2_cpp/readers/sequential_reader.hpp>
28+
#include <set>
29+
#include <vector>
2830

2931
using rclcpp::Time;
3032
using bag_time_t = rcutils_time_point_value_t;
@@ -36,24 +38,26 @@ class BagProcessor
3638
{
3739
public:
3840
BagProcessor(
39-
const std::string & bag_name, const std::string & topic, const std::string & topic_type,
40-
bag_time_t start_time, bag_time_t end_time)
41-
: topic_(topic), start_time_(start_time), end_time_(end_time)
41+
const std::string & bag_name, const std::vector<std::string> & topics,
42+
const std::string & topic_type, bag_time_t start_time, bag_time_t end_time)
43+
: start_time_(start_time), end_time_(end_time)
4244
{
4345
std::cout << "opening bag: " << bag_name << std::endl;
44-
std::cout << "topic: " << topic << std::endl;
4546
reader_.open(bag_name);
4647
const auto meta = reader_.get_all_topics_and_types();
47-
const auto it = std::find_if(
48-
meta.begin(), meta.end(), [topic, topic_type](const rosbag2_storage::TopicMetadata & m) {
49-
return (m.name == topic && m.type == topic_type);
50-
});
51-
if (it == meta.end()) {
52-
std::cerr << "topic " << topic << " with type " << topic_type << " not found!" << std::endl;
53-
throw(std::runtime_error("topic not found!"));
48+
for (const auto & topic : topics) {
49+
topics_.insert(topic);
50+
const auto it = std::find_if(
51+
meta.begin(), meta.end(), [topic, topic_type](const rosbag2_storage::TopicMetadata & m) {
52+
return (m.name == topic && m.type == topic_type);
53+
});
54+
if (it == meta.end()) {
55+
std::cerr << "topic " << topic << " with type " << topic_type << " not found!" << std::endl;
56+
throw(std::runtime_error("topic not found!"));
57+
}
58+
std::cout << "found topic: " << it->name << " with type: " << it->type << std::endl;
59+
filter_.topics.push_back(topic);
5460
}
55-
56-
filter_.topics.push_back(topic);
5761
reader_.set_filter(filter_);
5862
}
5963

@@ -68,7 +72,7 @@ class BagProcessor
6872

6973
while (reader_.has_next()) {
7074
auto msg = reader_.read_next();
71-
if (!msg || topic_ != msg->topic_name) {
75+
if (!msg || (topics_.find(msg->topic_name) == topics_.end())) {
7276
continue;
7377
}
7478
rclcpp::SerializedMessage serialized_msg(*msg->serialized_data);
@@ -82,7 +86,7 @@ class BagProcessor
8286
if (t > end_time_) {
8387
break;
8488
}
85-
mp->process(t, m);
89+
mp->process(t, msg->topic_name, m);
8690
message_number++;
8791
}
8892
const auto stop = std::chrono::high_resolution_clock::now();
@@ -95,7 +99,7 @@ class BagProcessor
9599
private:
96100
rosbag2_cpp::Reader reader_;
97101
rosbag2_storage::StorageFilter filter_;
98-
std::string topic_;
102+
std::set<std::string> topics_;
99103
bag_time_t start_time_;
100104
bag_time_t end_time_;
101105
rclcpp::Serialization<T> serialization_;

include/ffmpeg_image_transport_tools/message_processor.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class MessageProcessor
2323
{
2424
public:
2525
virtual ~MessageProcessor() {}
26-
virtual void process(uint64_t t, const typename T::ConstSharedPtr & msg) = 0;
26+
virtual void process(
27+
uint64_t t, const std::string & topic, const typename T::ConstSharedPtr & msg) = 0;
2728
};
2829
} // namespace ffmpeg_image_transport_tools
2930

src/bag_to_file.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class FileWriter : public ffmpeg_image_transport_tools::MessageProcessor<FFMPEGP
5858
ts_file_.open(ts_file);
5959
}
6060

61-
void process(uint64_t t, const FFMPEGPacket::ConstSharedPtr & m) final
61+
void process(uint64_t t, const std::string &, const FFMPEGPacket::ConstSharedPtr & m) final
6262
{
6363
const auto t_header = Time(m->header.stamp).nanoseconds();
6464
raw_file_.write(reinterpret_cast<const char *>(m->data.data()), m->data.size());
@@ -98,7 +98,7 @@ int main(int argc, char ** argv)
9898
}
9999
break;
100100
case 'o':
101-
out_file = atof(optarg);
101+
out_file = optarg;
102102
break;
103103
case 'r':
104104
rate = atof(optarg);
@@ -144,9 +144,9 @@ int main(int argc, char ** argv)
144144

145145
const std::string raw_file = out_file + ".h264";
146146
const std::string topic_type = "ffmpeg_image_transport_msgs/msg/FFMPEGPacket";
147+
std::vector<std::string> topics{topic};
147148
ffmpeg_image_transport_tools::BagProcessor<FFMPEGPacket> bproc(
148-
bag, topic, topic_type, start_time, end_time);
149+
bag, topics, topic_type, start_time, end_time);
149150
FileWriter fw(raw_file, time_stamp_file);
150151
bproc.process(&fw);
151-
convertToMP4(raw_file, out_file, rate);
152152
}

src/bag_to_frames.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ class FrameWriter : public ffmpeg_image_transport_tools::MessageProcessor<FFMPEG
6161
ts_file_.open(Path(base_dir_) / Path(ts_file));
6262
}
6363

64-
void process(uint64_t t, const FFMPEGPacket::ConstSharedPtr & m) final
64+
void process(uint64_t t, const std::string &, const FFMPEGPacket::ConstSharedPtr & m) final
6565
{
6666
if (!decoder_.isInitialized()) {
6767
std::string dtype = decoder_type_;
6868
if (dtype.empty()) {
6969
const auto & decoderMap = Decoder::getDefaultEncoderToDecoderMap();
7070
auto decTypeIt = decoderMap.find(m->encoding);
7171
if (decTypeIt == decoderMap.end()) {
72-
std::cerr << "unknown encoding: " << m->encoding << std::endl;
72+
std::cerr << "unknown encoder: " << m->encoding << std::endl;
73+
std::cerr << "must specify decoder!" << std::endl;
7374
throw(std::runtime_error("unknown encoding: " + m->encoding));
7475
}
7576
dtype = decTypeIt->second;
@@ -152,7 +153,7 @@ int main(int argc, char ** argv)
152153
}
153154
break;
154155
case 'o':
155-
out_dir = atof(optarg);
156+
out_dir = optarg;
156157
break;
157158
case 's':
158159
start_time = static_cast<bag_time_t>(atof(optarg) * 1e9);
@@ -187,10 +188,10 @@ int main(int argc, char ** argv)
187188
usage();
188189
return (-1);
189190
}
190-
191+
const std::vector<std::string> topics{topic};
191192
const std::string topic_type = "ffmpeg_image_transport_msgs/msg/FFMPEGPacket";
192193
ffmpeg_image_transport_tools::BagProcessor<FFMPEGPacket> bproc(
193-
bag, topic, topic_type, start_time, end_time);
194+
bag, topics, topic_type, start_time, end_time);
194195
FrameWriter fw(decoder, out_dir, time_stamp_file);
195196
bproc.process(&fw);
196197
}

0 commit comments

Comments
 (0)