-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.cpp
73 lines (62 loc) · 1.77 KB
/
video.cpp
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
#include <unistd.h>
#include <vector>
#include <iostream>
#include <limits>
#include <csignal>
#include <opencv2/opencv.hpp>
#include "../lib/canvas.hpp"
constexpr off_t WIDTH = 100;
constexpr off_t HEIGHT = 10;
constexpr size_t MAGNIFICATION = 18;
bool DONE;
static void FINISH(int ignore) {
DONE = true;
}
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "Usage: video <video-file>" << std::endl;
exit(1);
}
cv::VideoCapture capture(argv[1]);
double fps = capture.get(CV_CAP_PROP_FPS);
if (!capture.isOpened()) {
std::cerr << "Error opening video stream or file" << std::endl;
return -1;
}
DONE = false;
signal(SIGINT, FINISH);
Canvas* canvas = new Canvas(WIDTH, HEIGHT, MAGNIFICATION, false);
SDL_Event event;
bool first = true;
cv::Mat frame;
cv::Mat frameRGBA;
cv::Mat scaled(HEIGHT, WIDTH, CV_8UC4);
cv::rectangle(scaled, cv::Point(0, 0), cv::Point(WIDTH,HEIGHT), {0,0,0}, CV_FILLED);
while (!DONE) {
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit(0);
}
}
capture >> frame;
cv::cvtColor(frame, frameRGBA, CV_RGB2RGBA);
if(frame.empty()) {
capture.set(CV_CAP_PROP_POS_AVI_RATIO, 0);
continue;
}
double scaledW = 0;
double scaledH = 0;
double ratioW = frameRGBA.cols / (double)WIDTH;
double ratioH = frameRGBA.rows / (double)HEIGHT;
double ratio = std::max(ratioW, ratioH);
scaledW = frameRGBA.cols / ratio;
scaledH = frameRGBA.rows / ratio;
cv::resize(frameRGBA, scaled(cv::Rect(0,0,scaledW, scaledH)), {scaledW, scaledH}, 0, 0, cv::INTER_CUBIC);
cv::Mat tile = scaled(cv::Rect(0,0, scaledW, scaledH));
for(size_t i = 0; i < floor(WIDTH / scaledW); ++i)
tile.copyTo(scaled(cv::Rect(scaledW * i,0,scaledW, scaledH)));
canvas->draw(scaled);
usleep(1000000 / fps);
}
return 0;
}