-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvideostreamer.cpp
More file actions
202 lines (195 loc) · 5.84 KB
/
Copy pathvideostreamer.cpp
File metadata and controls
202 lines (195 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "videostreamer.h"
#include <QDebug>
#include <QStandardPaths>
#include <QDateTime>
#include <QDir>
#include <opencv2/opencv.hpp>
//static bool recording_status = false,recording_status2 = false;
QTimer sub_timer;
QString subtitle_path = "";
static int sub_i=1;
static int sub_i2=2;
bool recording_status = false;
int frame_width=0;
int frame_height=0;
std::string outputPathStdString="";
QString record_type="";
QString path ="";
VideoStreamer::VideoStreamer()
{
connect(&tUpdate,&QTimer::timeout,this,&VideoStreamer::streamVideo);
}
VideoStreamer::~VideoStreamer()
{
cap.release();
tUpdate.stop();
threadStreamer->requestInterruption();
}
void VideoStreamer::streamVideo()
{
if(frame.data)
{
QImage img = QImage(frame.data,frame.cols,frame.rows,QImage::Format_RGB888).rgbSwapped();
emit newImage(img);
}
}
void VideoStreamer::catchFrame(cv::Mat emittedFrame)
{
frame = emittedFrame;
if (recording_status && video.isOpened())
{
if (frame.cols != frame_width || frame.rows != frame_height)
{
cv::resize(frame, frame, cv::Size(frame_width, frame_height));
}
video.write(frame);
}
}
void VideoStreamer::openVideoCamera(QString path)
{
closeCamera();
//if(cap.isOpened())
// cap.release();
if(path.length() == 1)
cap.open(path.toInt());
else
cap.open(path.toStdString(),cv::CAP_FFMPEG);
VideoStreamer* worker = new VideoStreamer();
worker->moveToThread(threadStreamer);
QObject::connect(threadStreamer,SIGNAL(started()),worker,SLOT(streamerThreadSlot()));
QObject::connect(worker,&VideoStreamer::emitThreadImage,this,&VideoStreamer::catchFrame);
threadStreamer->start();
fps = cap.get(cv::CAP_PROP_FPS);
if (fps <= 0 || fps > 120)
fps = 30;
tUpdate.start(1000/fps);
}
void VideoStreamer::closeCamera()
{
if (tUpdate.isActive())
tUpdate.stop();
if(cap.isOpened())
cap.release();
qDebug() << "Camera closed successfully";
}
void VideoStreamer::streamerThreadSlot()
{
cv::Mat tempFrame;
while (1) {
cap>>tempFrame;
if(tempFrame.data)
emit emitThreadImage(tempFrame);
if(QThread::currentThread()->isInterruptionRequested())
{
cap.release();
return;
}
}
}
void VideoStreamer::changeCamera()
{
//qDebug()<<"camera changed";
/*if(cap.isOpened())
{
cap.release();
qDebug()<<"camera changed to rtsp";
}*/
openVideoCamera(
"rtsp://admin:Vikra@123@192.168.56.50:554/video/live?channel=1&subtype=0"
);
}
void VideoStreamer::start_recording()
{
if(subtitleFile.isOpen())
{
sub_timer.stop();
out.flush();
subtitleFile.close();
sub_i=1;
sub_i2=2;
}
recording_status = true;
if(frame_width == 0)
{
frame_width= cap.get(cv::CAP_PROP_FRAME_WIDTH);
frame_height= cap.get(cv::CAP_PROP_FRAME_HEIGHT);
}
// Get the target directory path for saving files.
QString targetDirectory = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
// Modify the target directory to add a subdirectory for your files.
QString logFileDir = targetDirectory + "/Recording";
// Create the directory if it doesn't exist.
QDir().mkpath(logFileDir);
if(path != "")
logFileDir = path;
QDateTime currentDateTime = QDateTime::currentDateTime();
formattedTime = currentDateTime.toString("dd.MM.yyyy-hh.mm.ss");
QString outputPath;
// Construct the file paths relative to the target directory.
if(record_type == "")
outputPath = logFileDir + "/Recording-" + formattedTime + ".mp4";
else
outputPath = logFileDir + "/Recording-" + formattedTime + "." + record_type;
QString outputPath2 = logFileDir + "/Recording-" + formattedTime + ".ass";
subtitleFile.setFileName(outputPath2);
outputPathStdString= outputPath.toStdString();
if (subtitleFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
out.setDevice(&subtitleFile);
// Write the ASS file header and styles
qDebug() << "[Script Info]\n";
qDebug()<< "Title: Example ASS File\n";
qDebug() << "Original Script: OpenAI\n";
qDebug() << "ScriptType: v4.00+\n";
qDebug() << "Collisions: Normal\n";
qDebug() << "PlayDepth: 0\n\n";
qDebug() << "[V4+ Styles]\n";
qDebug() << "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n";
qDebug() << "Style: Default,Arial,20,&H00FFFFFF,&H00000000,&H00000000,-1,0,1,1.0,0.0,2,10,10,10,1\n\n";
qDebug() << "[Events]\n";
qDebug() << "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n";
}
video.open(outputPathStdString,
// cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
cv::VideoWriter::fourcc('M', 'P', '4', 'V'),
fps,
cv::Size(frame_width,frame_height),
true);
//recording_status=true;
if (!video.isOpened()) {
qDebug() << "Error: Could not open the video writer.";
recording_status = false;
}
else
{
recording_status = true;
}
sub_timer.start(1000);
}
void VideoStreamer::stop_recording()
{
recording_status = false;
//QThread::msleep(2000);
if (video.isOpened()) {
video.release();
//export_video();
}
else
{
}
//sub_i=1;
//sub_i2=2;
}
void VideoStreamer::pauseStreaming()
{
if (tUpdate.isActive()) {
tUpdate.stop();
qDebug() << "Video streaming paused";
}
}
void VideoStreamer::resumeStreaming()
{
if (!tUpdate.isActive() && cap.isOpened()) {
tUpdate.start(1000 / fps);
qDebug() << "Video streaming resumed";
}
}