AVIRecordingStrategy.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package visionx::imrec
17  * @author Christian R. G. Dreher <christian.dreher@student.kit.edu>
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
25 
26 
27 // OpenCV 2
28 #include <opencv2/core/core.hpp>
29 #include <opencv2/highgui/highgui.hpp> // cv::VideoWriter
30 
31 // Simox
32 #include <SimoxUtility/algorithm.h>
33 
35 {
36  // pass
37 }
38 
40  const std::filesystem::path& file_path,
41  const std::string& filename,
42  const double fps) :
43  visionx::imrec::AbstractRecordingStrategy(file_path), m_fps{fps}
44 {
45  std::string filename_ext = filename;
46  if (not simox::alg::ends_with(filename_ext, ".avi"))
47  {
48  filename_ext += ".avi";
49  }
50 
51  m_file_path /= filename_ext;
52 }
53 
55 {
56  // pass
57 }
58 
59 void
61 {
63  m_avi_video_writer = std::make_unique<cv::VideoWriter>();
64 }
65 
66 void
68  const std::chrono::microseconds timestamp)
69 {
70  writeMetadataFrame(frame, timestamp);
71 
72  if (not m_avi_video_writer->isOpened())
73  {
74  const std::string filePath = getFilePath().string();
75  const int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
76  m_avi_video_writer->open(filePath, fourcc, m_fps, frame.size());
77 
78  if (not m_avi_video_writer->isOpened())
79  {
80  ARMARX_ERROR << deactivateSpam() << "Could not open the output video file '" << filePath
81  << "' for writing. FRAME DROPPED!";
82  return;
83  }
84  }
85 
86  m_avi_video_writer->write(frame);
87 }
88 
89 void
91 {
93  m_avi_video_writer->release();
94 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::imrec::AbstractRecordingStrategy
Abstract interface of a recording strategy.
Definition: AbstractRecordingStrategy.h:52
AVIRecordingStrategy.h
visionx::imrec::AbstractRecordingStrategy::stopRecording
virtual void stopRecording()
Stops the recording.
Definition: AbstractRecordingStrategy.cpp:111
visionx::imrec::strats::AVIRecordingStrategy::AVIRecordingStrategy
AVIRecordingStrategy()
Definition: AVIRecordingStrategy.cpp:34
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
visionx::imrec::strats::AVIRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: AVIRecordingStrategy.cpp:60
filename
std::string filename
Definition: VisualizationRobot.cpp:86
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
visionx::imrec::strats::AVIRecordingStrategy::~AVIRecordingStrategy
~AVIRecordingStrategy() override
Definition: AVIRecordingStrategy.cpp:54
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:53
visionx::imrec::AbstractRecordingStrategy::startRecording
virtual void startRecording()
Starts the recording manually if constructed empty.
Definition: AbstractRecordingStrategy.cpp:73
visionx::imrec::strats::AVIRecordingStrategy::stopRecording
void stopRecording() override
Stops the recording.
Definition: AVIRecordingStrategy.cpp:90
visionx::imrec::strats::AVIRecordingStrategy::recordFrame
void recordFrame(const cv::Mat &frame, std::chrono::microseconds timestamp) override
Adds the given frame to the recording.
Definition: AVIRecordingStrategy.cpp:67