]> git.sesse.net Git - pkanalytics/blob - stats.cpp
7bce2a26dba3b893c42bc2fa4a248a30fabdadaf
[pkanalytics] / stats.cpp
1 #include <QMediaPlayer>
2 #include <QMainWindow>
3 #include <QApplication>
4 #include <QGridLayout>
5 #include <QVideoWidget>
6 #include <QShortcut>
7 #include <iostream>
8 #include "mainwindow.h"
9 #include "ui_mainwindow.h"
10
11 MainWindow::MainWindow()
12 {
13         player = new QMediaPlayer;
14         //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
15         player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
16         player->play();
17
18         Ui::MainWindow *ui = new Ui::MainWindow;
19         ui->setupUi(this);
20
21         connect(player, &QMediaPlayer::positionChanged, [ui, this](uint64_t pos) {
22                 int ms = pos % 1000;
23                 pos /= 1000;
24                 int sec = pos % 60;
25                 pos /= 60;
26                 int min = pos % 60;
27                 int hour = pos / 60;
28
29                 char buf[256];
30                 snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
31                 ui->timestamp->setText(buf);
32                 if (buffered_seek) {
33                         player->setPosition(*buffered_seek);
34                         buffered_seek.reset();
35                 }
36                 if (!playing) {
37                         player->pause();  // We only played to get a picture.
38                 }
39         });
40
41         player->setVideoOutput(ui->video);
42
43         QShortcut *key_k = new QShortcut(QKeySequence(Qt::Key_K), this);
44         connect(key_k, &QShortcut::activated, [this]() { seek(-10000); });
45
46         QShortcut *key_l = new QShortcut(QKeySequence(Qt::Key_L), this);
47         connect(key_l, &QShortcut::activated, [this]() { seek(10000); });
48
49         QShortcut *key_left = new QShortcut(QKeySequence(Qt::Key_Left), this);
50         connect(key_left, &QShortcut::activated, [this]() { seek(-1000); });
51
52         QShortcut *key_right = new QShortcut(QKeySequence(Qt::Key_Right), this);
53         connect(key_right, &QShortcut::activated, [this]() { seek(1000); });
54
55         // TODO: Would be nice to actually have a frame...
56         QShortcut *key_comma = new QShortcut(QKeySequence(Qt::Key_Comma), this);
57         connect(key_comma, &QShortcut::activated, [this]() { seek(-20); });
58
59         QShortcut *key_period = new QShortcut(QKeySequence(Qt::Key_Period), this);
60         connect(key_period, &QShortcut::activated, [this]() { seek(20); });
61
62         QShortcut *key_space = new QShortcut(QKeySequence(Qt::Key_Space), this);
63         connect(key_space, &QShortcut::activated, [this]() {
64                 if (playing) {
65                         player->pause();
66                 } else {
67                         player->setPlaybackRate(1.0);
68                         player->play();
69                 }
70                 playing = !playing;
71         });
72 }
73
74 void MainWindow::seek(int64_t delta_ms)
75 {
76         int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
77         uint64_t pos = std::max<int64_t>(current_pos + delta_ms, 0);
78         buffered_seek = pos;
79         if (!playing) {
80                 player->setPlaybackRate(0.01);
81                 player->play();  // Or Qt won't show the seek.
82         }
83 }
84
85 int main(int argc, char *argv[])
86 {
87         QApplication app(argc, argv);
88
89         MainWindow mainWindow;
90         mainWindow.resize(QSize(1280, 720));
91         mainWindow.show();
92
93         return app.exec();
94
95 }