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 
34 
36 {
37  // pass
38 }
39 
40 
41 visionx::imrec::strats::AVIRecordingStrategy::AVIRecordingStrategy(const std::filesystem::path& file_path, const std::string& filename, const double fps) :
42  visionx::imrec::AbstractRecordingStrategy(file_path),
43  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 
54 
56 {
57  // pass
58 }
59 
60 
61 void
63 {
65  m_avi_video_writer = std::make_unique<cv::VideoWriter>();
66 }
67 
68 
69 void
70 visionx::imrec::strats::AVIRecordingStrategy::recordFrame(const cv::Mat& frame, const std::chrono::microseconds timestamp)
71 {
72  writeMetadataFrame(frame, timestamp);
73 
74  if (not m_avi_video_writer->isOpened())
75  {
76  const std::string filePath = getFilePath().string();
77  const int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
78  m_avi_video_writer->open(filePath, fourcc, m_fps, frame.size());
79 
80  if (not m_avi_video_writer->isOpened())
81  {
82  ARMARX_ERROR << deactivateSpam() << "Could not open the output video file '" << filePath << "' for writing. FRAME DROPPED!";
83  return;
84  }
85  }
86 
87  m_avi_video_writer->write(frame);
88 }
89 
90 
91 void
93 {
95  m_avi_video_writer->release();
96 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::imrec::AbstractRecordingStrategy
Abstract interface of a recording strategy.
Definition: AbstractRecordingStrategy.h:54
AVIRecordingStrategy.h
visionx::imrec::AbstractRecordingStrategy::stopRecording
virtual void stopRecording()
Stops the recording.
Definition: AbstractRecordingStrategy.cpp:116
visionx::imrec::strats::AVIRecordingStrategy::AVIRecordingStrategy
AVIRecordingStrategy()
Definition: AVIRecordingStrategy.cpp:35
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
visionx::imrec::strats::AVIRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: AVIRecordingStrategy.cpp:62
filename
std::string filename
Definition: VisualizationRobot.cpp:83
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::imrec::strats::AVIRecordingStrategy::~AVIRecordingStrategy
~AVIRecordingStrategy() override
Definition: AVIRecordingStrategy.cpp:55
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
visionx::imrec::AbstractRecordingStrategy::startRecording
virtual void startRecording()
Starts the recording manually if constructed empty.
Definition: AbstractRecordingStrategy.cpp:78
visionx::imrec::strats::AVIRecordingStrategy::stopRecording
void stopRecording() override
Stops the recording.
Definition: AVIRecordingStrategy.cpp:92
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:70