|
2 | 2 |
|
3 | 3 | #include "base.hpp"
|
4 | 4 |
|
5 |
| -namespace decode_ffmpeg { |
6 |
| - |
7 |
| -const int BUFFER_SIZE = 1 << 8; |
8 |
| -const int STDOUT_SIZE = 1 << 20; |
9 |
| - |
10 |
| -inline int exec_r(const char *cmd, char *result) { |
11 |
| - FILE *p = r_popen(cmd); |
12 |
| - |
13 |
| - if (!p) return 1; |
14 |
| - int code = 0; |
15 |
| - |
16 |
| - int t = 0; |
17 |
| - while (!feof(p)) { |
18 |
| - fread(result + t, 1, BUFFER_SIZE, p); |
19 |
| - t += BUFFER_SIZE; |
20 |
| - if (t >= STDOUT_SIZE) { |
21 |
| - code = 2; |
22 |
| - break; |
| 5 | +class Decoder_FFmpeg : public Decoder { |
| 6 | +private: |
| 7 | + const int BUFFER_SIZE = 1 << 8; |
| 8 | + const int STDOUT_SIZE = 1 << 20; |
| 9 | + |
| 10 | + inline int exec_r(const char *cmd, char *result) { |
| 11 | + FILE *p = r_popen(cmd); |
| 12 | + |
| 13 | + if (!p) return 1; |
| 14 | + int code = 0; |
| 15 | + |
| 16 | + int t = 0; |
| 17 | + while (!feof(p)) { |
| 18 | + fread(result + t, 1, BUFFER_SIZE, p); |
| 19 | + t += BUFFER_SIZE; |
| 20 | + if (t >= STDOUT_SIZE) { |
| 21 | + code = 2; |
| 22 | + break; |
| 23 | + } |
23 | 24 | }
|
24 |
| - } |
25 |
| - pipe_pclose(p); |
26 |
| - |
27 |
| - return code; |
28 |
| -} |
| 25 | + pipe_pclose(p); |
29 | 26 |
|
30 |
| -std::string video; |
31 |
| -FILE *fp; |
32 |
| -int x, y, xy; |
33 |
| -} // namespace decode_ffmpeg |
| 27 | + return code; |
| 28 | + } |
34 | 29 |
|
35 |
| -inline VideoProperties *analysis(std::string _video, int _x, int _y) { |
36 |
| - using namespace decode_ffmpeg; |
37 |
| - video = _video; |
38 |
| - x = _x; |
39 |
| - y = _y; |
40 |
| - xy = x * y; |
| 30 | + FILE *fp; |
41 | 31 |
|
42 |
| - std::string cmd = (std::string) "ffprobe -v quiet -show_streams -select_streams v \"" + video + "\""; |
43 |
| - // printf("%s\n", cmd.c_str()); |
| 32 | +public: |
| 33 | + Decoder_FFmpeg(std::string _video) : Decoder(_video){}; |
44 | 34 |
|
45 |
| - VideoProperties *vp = new VideoProperties(); |
| 35 | + inline VideoProperties *analysis() { |
| 36 | + std::string cmd = (std::string) "ffprobe -v quiet -show_streams -select_streams v \"" + video + "\""; |
| 37 | + // printf("%s\n", cmd.c_str()); |
46 | 38 |
|
47 |
| - double rate_l, rate_r; // fps = rate_l / rate_r; |
48 |
| - char result_c[STDOUT_SIZE]; |
49 |
| - if (exec_r(cmd.c_str(), result_c)) { |
50 |
| - throws("Failed to analysis video."); |
51 |
| - return nullptr; |
52 |
| - } |
53 |
| - std::string result_s(result_c), _k; |
54 |
| - std::size_t p, p2; |
55 |
| - |
56 |
| - _k = "\nwidth="; |
57 |
| - p = result_s.find(_k); |
58 |
| - if (p == std::string::npos) { |
59 |
| - throws("This video has no width."); |
60 |
| - return nullptr; |
61 |
| - } |
62 |
| - p += _k.length(); |
63 |
| - p2 = result_s.find("\n", p); |
64 |
| - sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->width); |
65 |
| - |
66 |
| - _k = "\nheight="; |
67 |
| - p = result_s.find(_k); |
68 |
| - if (p == std::string::npos) { |
69 |
| - throws("This video has no height."); |
70 |
| - return nullptr; |
71 |
| - } |
72 |
| - p += _k.length(); |
73 |
| - p2 = result_s.find("\n", p); |
74 |
| - sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->height); |
| 39 | + VideoProperties *vp = new VideoProperties(); |
75 | 40 |
|
76 |
| - _k = "\nnb_frames="; |
77 |
| - p = result_s.find(_k); |
78 |
| - if (p == std::string::npos) { |
| 41 | + double rate_l, rate_r; // fps = rate_l / rate_r; |
| 42 | + char result_c[STDOUT_SIZE]; |
| 43 | + if (exec_r(cmd.c_str(), result_c)) { |
| 44 | + throws("Failed to analysis video."); |
| 45 | + return nullptr; |
| 46 | + } |
| 47 | + std::string result_s(result_c), _k; |
| 48 | + std::size_t p, p2; |
| 49 | + |
| 50 | + _k = "\nwidth="; |
| 51 | + p = result_s.find(_k); |
| 52 | + if (p == std::string::npos) { |
| 53 | + throws("This video has no width."); |
| 54 | + return nullptr; |
| 55 | + } |
79 | 56 | p += _k.length();
|
80 | 57 | p2 = result_s.find("\n", p);
|
81 |
| - sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->nb_frames); |
82 |
| - } |
| 58 | + sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->width); |
83 | 59 |
|
84 |
| - double _l, _r; |
85 |
| - _k = "\nr_frame_rate="; |
86 |
| - p = result_s.find(_k); |
87 |
| - if (p == std::string::npos) { |
88 |
| - throws("This video has no frame rate."); |
89 |
| - return nullptr; |
90 |
| - } |
91 |
| - p += _k.length(); |
92 |
| - p2 = result_s.find("/", p); |
93 |
| - sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_l); |
94 |
| - p = p2 + 1; |
95 |
| - p2 = result_s.find("\n", p); |
96 |
| - sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_r); |
97 |
| - if (_r < 1) { |
98 |
| - throws("This video has no frame rate."); |
99 |
| - return nullptr; |
100 |
| - } |
101 |
| - vp->rate = _l / _r; |
| 60 | + _k = "\nheight="; |
| 61 | + p = result_s.find(_k); |
| 62 | + if (p == std::string::npos) { |
| 63 | + throws("This video has no height."); |
| 64 | + return nullptr; |
| 65 | + } |
| 66 | + p += _k.length(); |
| 67 | + p2 = result_s.find("\n", p); |
| 68 | + sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->height); |
| 69 | + |
| 70 | + _k = "\nnb_frames="; |
| 71 | + p = result_s.find(_k); |
| 72 | + if (p == std::string::npos) { |
| 73 | + p += _k.length(); |
| 74 | + p2 = result_s.find("\n", p); |
| 75 | + sscanf(result_s.substr(p, p2 - p).c_str(), "%d", &vp->nb_frames); |
| 76 | + } |
102 | 77 |
|
103 |
| - _k = "\nduration="; |
104 |
| - p = result_s.find(_k); |
105 |
| - if (p != std::string::npos) { |
| 78 | + double _l, _r; |
| 79 | + _k = "\nr_frame_rate="; |
| 80 | + p = result_s.find(_k); |
| 81 | + if (p == std::string::npos) { |
| 82 | + throws("This video has no frame rate."); |
| 83 | + return nullptr; |
| 84 | + } |
106 | 85 | p += _k.length();
|
| 86 | + p2 = result_s.find("/", p); |
| 87 | + sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_l); |
| 88 | + p = p2 + 1; |
107 | 89 | p2 = result_s.find("\n", p);
|
108 |
| - sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &vp->duration); |
109 |
| - } |
| 90 | + sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &_r); |
| 91 | + if (_r < 1) { |
| 92 | + throws("This video has no frame rate."); |
| 93 | + return nullptr; |
| 94 | + } |
| 95 | + vp->rate = _l / _r; |
| 96 | + |
| 97 | + _k = "\nduration="; |
| 98 | + p = result_s.find(_k); |
| 99 | + if (p != std::string::npos) { |
| 100 | + p += _k.length(); |
| 101 | + p2 = result_s.find("\n", p); |
| 102 | + sscanf(result_s.substr(p, p2 - p).c_str(), "%lf", &vp->duration); |
| 103 | + } |
110 | 104 |
|
111 |
| - return vp; |
112 |
| -} |
| 105 | + return vp; |
| 106 | + } |
113 | 107 |
|
114 |
| -inline int ready_to_read() { |
115 |
| - using namespace decode_ffmpeg; |
| 108 | + inline int ready_to_read(int _x, int _y) { |
| 109 | + x = _x; |
| 110 | + y = _y; |
| 111 | + xy = x * y; |
| 112 | + std::string cmd = (std::string) "ffmpeg -v quiet -i \"" + video + "\" -vf scale=" + std::to_string(x) + ":" + std::to_string(y) + " -c:v rawvideo -pix_fmt gray -f rawvideo -"; |
| 113 | + // printf("%s\n", cmd.c_str()); |
| 114 | + |
| 115 | + fp = rb_popen(cmd); |
| 116 | + if (!fp) { |
| 117 | + throws("Failed to build pipe."); |
| 118 | + return 1; |
| 119 | + } |
| 120 | + return 0; |
| 121 | + } |
116 | 122 |
|
117 |
| - std::string cmd = (std::string) "ffmpeg -v quiet -i \"" + video + "\" -vf scale=" + std::to_string(x) + ":" + std::to_string(y) + " -c:v rawvideo -pix_fmt gray -f rawvideo -"; |
118 |
| - // printf("%s\n", cmd.c_str()); |
| 123 | + inline int read_a_frame(B *f) { |
| 124 | + return xy ^ fread(f, 1, xy, fp); |
| 125 | + } |
119 | 126 |
|
120 |
| - fp = rb_popen(cmd); |
121 |
| - if (!fp) { |
122 |
| - throws("Failed to build pipe."); |
123 |
| - return 1; |
| 127 | + inline void cls() { |
| 128 | + pipe_pclose(fp); |
124 | 129 | }
|
125 |
| - return 0; |
126 |
| -} |
127 |
| - |
128 |
| -inline int read_a_frame(B *f) { |
129 |
| - using namespace decode_ffmpeg; |
130 |
| - return xy ^ fread(f, 1, xy, fp); |
131 |
| -} |
132 |
| - |
133 |
| -inline void cls() { |
134 |
| - using namespace decode_ffmpeg; |
135 |
| - pipe_pclose(fp); |
136 |
| -} |
| 130 | +}; |
0 commit comments