Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <cstdlib> |
| 4 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 5 | #include "absl/flags/flag.h" |
| 6 | #include "absl/log/check.h" |
| 7 | #include "absl/log/log.h" |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 8 | |
| 9 | #include "aos/events/logging/log_reader.h" |
| 10 | #include "aos/events/simulated_event_loop.h" |
| 11 | #include "aos/init.h" |
| 12 | #include "aos/scoped/scoped_fd.h" |
| 13 | #include "frc971/vision/vision_generated.h" |
| 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 15 | ABSL_FLAG(std::string, channel, "/camera", "Channel name for the image."); |
| 16 | ABSL_FLAG(int32_t, width, 1280, "Width of the image"); |
| 17 | ABSL_FLAG(int32_t, height, 720, "Height of the image"); |
| 18 | ABSL_FLAG(std::string, ffmpeg_binary, "external/ffmpeg/ffmpeg", |
| 19 | "The path to the ffmpeg binary"); |
| 20 | ABSL_FLAG(std::string, output_path, "video_ripper_output.mp4", |
| 21 | "The path to output the mp4 video"); |
| 22 | ABSL_FLAG(bool, flip, true, "If true, rotate the video 180 deg."); |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 23 | |
| 24 | // Replays a log and dumps the contents of /camera frc971.vision.CameraImage |
| 25 | // directly to stdout. |
| 26 | int main(int argc, char *argv[]) { |
| 27 | aos::InitGoogle(&argc, &argv); |
| 28 | const std::vector<aos::logger::LogFile> logfiles = |
| 29 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv)); |
| 30 | CHECK(!logfiles.empty()); |
| 31 | |
| 32 | // Start ffmpeg |
| 33 | std::stringstream command; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 34 | command << absl::GetFlag(FLAGS_ffmpeg_binary); |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 35 | command << " -framerate 30 -f rawvideo -pix_fmt yuyv422"; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 36 | command << " -s " << absl::GetFlag(FLAGS_width) << "x" |
| 37 | << absl::GetFlag(FLAGS_height); |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 38 | command << " -i pipe:"; |
| 39 | command << " -c:v libx264 -f mp4"; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 40 | if (absl::GetFlag(FLAGS_flip)) { |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 41 | command << " -vf rotate=PI"; |
| 42 | } |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 43 | command << " \"" << absl::GetFlag(FLAGS_output_path) << "\""; |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 44 | |
| 45 | FILE *stream = popen(command.str().c_str(), "w"); |
| 46 | |
| 47 | const std::string replay_node = logfiles.at(0).logger_node; |
| 48 | LOG(INFO) << "Replaying as \"" << replay_node << "\""; |
| 49 | |
| 50 | aos::logger::LogReader reader(logfiles, nullptr); |
| 51 | aos::SimulatedEventLoopFactory factory(reader.configuration()); |
| 52 | reader.RegisterWithoutStarting(&factory); |
| 53 | |
| 54 | const aos::Node *node = |
| 55 | (replay_node.empty() || |
| 56 | !aos::configuration::MultiNode(reader.configuration())) |
| 57 | ? nullptr |
| 58 | : aos::configuration::GetNode(reader.configuration(), replay_node); |
| 59 | |
| 60 | std::unique_ptr<aos::EventLoop> event_loop; |
| 61 | factory.GetNodeEventLoopFactory(node)->OnStartup([&stream, &event_loop, |
| 62 | &reader, node]() { |
| 63 | event_loop = |
| 64 | reader.event_loop_factory()->MakeEventLoop("video_ripper", node); |
| 65 | event_loop->MakeWatcher( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 66 | absl::GetFlag(FLAGS_channel), |
| 67 | [&stream](const frc971::vision::CameraImage &image) { |
| 68 | CHECK_EQ(absl::GetFlag(FLAGS_width), image.cols()) |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 69 | << "Image width needs to match the images in the logfile"; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 70 | CHECK_EQ(absl::GetFlag(FLAGS_height), image.rows()) |
Ravago Jones | 3b4fd91 | 2023-03-18 13:35:17 -0700 | [diff] [blame] | 71 | << "Image width needs to match the images in the logfile"; |
| 72 | |
| 73 | const size_t bytes_written = |
| 74 | write(fileno(stream), image.data()->data(), image.data()->size()); |
| 75 | PCHECK(bytes_written == image.data()->size()); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | reader.event_loop_factory()->Run(); |
| 80 | reader.Deregister(); |
| 81 | |
| 82 | pclose(stream); |
| 83 | } |