forked from Sarthak2143/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
289 lines (249 loc) · 8 KB
/
example.cpp
File metadata and controls
289 lines (249 loc) · 8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "sakura.hpp"
#include <cpr/cpr.h>
#include <getopt.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <sys/ioctl.h>
#include <unistd.h>
#include <utility>
std::pair<int, int> getTerminalPixelSize() {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 && w.ws_xpixel > 0 &&
w.ws_ypixel > 0) {
return {w.ws_xpixel, w.ws_ypixel};
}
// Fallback: return a reasonable default if pixel size is not available
return {1920, 1080};
}
std::pair<int, int> getTerminalCharSize() {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
return {w.ws_col, w.ws_row};
}
// Fallback
return {80, 24};
}
std::pair<int, int> calculateBestFitSize(int contentWidth, int contentHeight,
int termWidth, int termHeight) {
double contentAspect = static_cast<double>(contentWidth) / contentHeight;
double termAspect = static_cast<double>(termWidth) / termHeight;
int outw, outh;
if (contentAspect > termAspect) {
// Content is wider - fit to terminal width
outw = termWidth;
outh = static_cast<int>(termWidth / contentAspect);
} else {
// Content is taller - fit to terminal height
outh = termHeight;
outw = static_cast<int>(termHeight * contentAspect);
}
return {outw, outh};
}
bool process_image(std::string url) {
Sakura sakura;
bool stat = false;
auto [termPixW, termPixH] = getTerminalPixelSize();
auto response = cpr::Get(cpr::Url{url});
if (response.status_code != 200) {
std::cerr << "Failed to download image. Status: " << response.status_code
<< std::endl;
return 1;
}
std::vector<uchar> imgData(response.text.begin(), response.text.end());
cv::Mat img = cv::imdecode(imgData, cv::IMREAD_COLOR);
if (img.empty()) {
std::cerr << "Failed to decode image" << std::endl;
return 1;
}
// calculate proper dimensions for this image
auto [outw, outh] =
calculateBestFitSize(img.cols, img.rows, termPixW, termPixH);
Sakura::RenderOptions options;
options.mode = Sakura::SIXEL;
options.dither = Sakura::FLOYD_STEINBERG;
options.terminalAspectRatio = 1.0;
options.width = outw;
options.height = outh;
return sakura.renderFromMat(img, options);
}
bool process_gif(std::string url) {
Sakura sakura;
bool stat = false;
auto [termPixW, termPixH] = getTerminalPixelSize();
// For GIF, we'll let the sakura library handle sizing internally
Sakura::RenderOptions options;
options.mode = Sakura::SIXEL;
options.dither = Sakura::FLOYD_STEINBERG;
options.terminalAspectRatio = 1.0;
options.width = termPixW;
options.height = termPixH;
return sakura.renderGifFromUrl(url, options);
}
bool process_video(std::string url) {
Sakura sakura;
bool stat = false;
auto [termPixW, termPixH] = getTerminalPixelSize();
// For video, we'll let the sakura library handle sizing internally
Sakura::RenderOptions options;
options.mode = Sakura::SIXEL;
options.dither = Sakura::FLOYD_STEINBERG;
options.terminalAspectRatio = 1.0;
options.width = termPixW;
options.height = termPixH;
return sakura.renderVideoFromUrl(url, options);
}
bool process_local_video(std::string path) {
Sakura sakura;
bool stat = false;
auto [termCols, termRows] = getTerminalCharSize(); // Use character dimensions
// Ultra-fast settings for maximum performance
Sakura::RenderOptions options;
options.mode = Sakura::ULTRA_FAST;
options.dither = Sakura::NONE;
options.terminalAspectRatio = 1.0;
options.width = termCols;
options.height = termRows;
options.queueSize = 1;
options.prebufferFrames = 1;
options.staticPalette = true;
options.fastResize = true;
options.targetFps = 0.0; // Follow source FPS
options.adaptivePalette = false;
options.adaptiveScale = false;
options.hwAccelPipe = false;
options.tileUpdates = false;
options.fit = Sakura::FitMode::COVER; // Fill terminal
options.sixelQuality = Sakura::SixelQuality::HIGH;
stat = sakura.renderVideoFromFile(path, options);
return stat;
}
int main(int argc, char **argv) {
// Parse command line arguments
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"image", required_argument, 0, 'i'},
{"gif", required_argument, 0, 'g'},
{"video", required_argument, 0, 'v'},
{"local-video", required_argument, 0, 'l'},
{0, 0, 0, 0}};
std::string video_path, image_path;
bool show_help = false;
int opt;
int option_index = 0;
bool stat = false;
if (argc > 1) {
while ((opt = getopt_long(argc, argv, "hv:i:g:l:", long_options,
&option_index)) != -1) {
switch (opt) {
case 'h':
std::cout << "Usage: sakura [options]\n"
<< "Options:\n"
<< " -h, --help Show help message\n"
<< " -i, --image <path> Process image file\n"
<< " -g, --gif <path> Process GIF file\n"
<< " -v, --video <path> Process video file\n"
<< " -l, --local-video <path> Process local video file\n";
return 0;
case 'i':
stat = process_image(optarg);
break;
case 'g':
stat = process_gif(optarg);
break;
case 'v':
stat = process_video(optarg);
break;
case 'l':
stat = process_local_video(optarg);
break;
case '?':
// getopt_long automatically prints error message
return 1;
default:
std::cerr << "Unknown option: " << opt << std::endl;
return 1;
}
}
if (!stat) {
std::cerr << "Failed to render content\n";
}
return 0;
}
// Handle any remaining non-option arguments
for (int i = optind; i < argc; i++) {
std::cout << "Non-option argument: " << argv[i] << std::endl;
}
// Continue to existing interactive mode
Sakura sakura;
std::string url, gifUrl, videoUrl, localVideoPath;
// Get terminal pixel size once
auto [termPixW, termPixH] = getTerminalPixelSize();
std::cout << "Sakura Video Player with SIXEL\n";
std::cout << "1. Image\n2. GIF\n3. Video (URL)\n4. Video (File)\n";
std::cout << "Choose option (1-4): ";
int choice;
std::cin >> choice;
switch (choice) {
case 1: {
std::cout << "Enter image URL: ";
std::cin >> url;
std::cout << "Rendering image...\n";
stat = process_image(url);
break;
}
case 2: {
std::cout << "Enter GIF URL: ";
std::cin >> gifUrl;
std::cout << "Rendering GIF...\n";
stat = process_gif(gifUrl);
break;
}
case 3: {
std::cout << "Enter video URL: ";
std::cin >> videoUrl;
std::cout << "Rendering video from URL (with audio)...\n";
stat = process_video(videoUrl);
break;
}
case 4: {
std::cout << "Enter video file path: ";
std::cin >> localVideoPath;
std::cout << "Rendering video from file (with audio)...\n";
stat = process_local_video(localVideoPath);
break;
}
default: {
std::cout << "Invalid choice. Rendering image by default.\n";
std::cout << "Enter image URL: ";
std::cin >> url;
std::cout << "Rendering image...\n";
// Download image and calculate its dimensions
auto response = cpr::Get(cpr::Url{url});
if (response.status_code != 200) {
std::cerr << "Failed to download image. Status: " << response.status_code
<< std::endl;
return 1;
}
std::vector<uchar> imgData(response.text.begin(), response.text.end());
cv::Mat img = cv::imdecode(imgData, cv::IMREAD_COLOR);
if (img.empty()) {
std::cerr << "Failed to decode image" << std::endl;
return 1;
}
auto [outw, outh] =
calculateBestFitSize(img.cols, img.rows, termPixW, termPixH);
Sakura::RenderOptions options;
options.mode = Sakura::SIXEL;
options.dither = Sakura::FLOYD_STEINBERG;
options.terminalAspectRatio = 1.0;
options.width = outw;
options.height = outh;
stat = sakura.renderFromMat(img, options);
break;
}
}
if (!stat) {
std::cerr << "Failed to render content\n";
}
return 0;
}