Skip to content

Commit 7f4f3fb

Browse files
committed
feat: compile the scenes (no alpha atm)
1 parent 8058d19 commit 7f4f3fb

File tree

13 files changed

+263
-77
lines changed

13 files changed

+263
-77
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ video/
1515
*.mkv
1616
*.jpg
1717
*.png
18+
*.mov
19+
*.webm
1820

1921
# Python
2022
__pycache__/

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,37 @@ LFLAGS = -O1 -Wl,-rpath-link,/usr/lib64
7272
SRC = $(SRC_MAIN) \
7373
$(SRC_VM) \
7474
$(SRC_INPUT) \
75-
$(SRC_TRANSFORM) \
75+
$(SRC_TRANSFORM) \
76+
$(SRC_COMPILER) \
7677

7778

7879
SRC_MAIN = src/Main.cpp
7980

80-
SRC_VM = src/vm/LiveWindow.cpp \
81-
src/vm/WindowEvent.cpp \
81+
SRC_VM = src/vm/VideoCode.cpp \
8282
src/vm/Register.cpp \
83+
src/vm/AppWindow.cpp \
84+
src/vm/AppEvent.cpp \
85+
86+
87+
SRC_COMPILER = src/compiler/generateVideo.cpp \
8388

8489

8590
SRC_INPUT = src/input/ABCInput.cpp \
8691
src/input/Image.cpp \
8792
src/input/Video.cpp \
8893

94+
8995
SRC_TRANSFORM = $(SRC_TSF_POS) \
9096
$(SRC_TSF_COLOR) \
9197
$(SRC_TSF_OTHER) \
9298

99+
93100
SRC_TSF_POS = src/transformation/position/translate.cpp \
94101

102+
95103
SRC_TSF_COLOR = src/transformation/color/fade.cpp \
96104

105+
97106
SRC_TSF_OTHER = src/transformation/other/overlay.cpp \
98107

99108

include/compiler/Compiler.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
** EPITECH PROJECT, 2025
3+
** video-code
4+
** File description:
5+
** Compiler
6+
*/
7+
8+
#pragma once
9+
10+
#include <vector>
11+
12+
#include "opencv2/core/mat.hpp"
13+
14+
namespace Compiler::Writer
15+
{
16+
int generateVideo(
17+
int width,
18+
int height,
19+
int fps,
20+
std::string filename,
21+
const std::vector<cv::Mat> &frames
22+
);
23+
};
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77

88
#pragma once
99

10-
#include <map>
10+
#include <qevent.h>
11+
#include <qlabel.h>
12+
#include <qwidget.h>
1113

12-
#include "qevent.h"
13-
#include "qwidget.h"
14+
#include <map>
1415

15-
class WindowEvent : public QWidget
16+
class AppEvent : public QWidget
1617
{
1718
public:
1819

19-
WindowEvent(const std::map<int, std::function<void()>> &events, std::string &sourceFile, int width, int height, std::function<void()> &&mainLoop);
20-
~WindowEvent() = default;
20+
AppEvent(const std::map<int, std::function<void()>> &events, const std::string &sourceFile, int width, int height, std::function<void()> &&mainLoop);
21+
~AppEvent() = default;
2122

2223
void mainLoop();
2324

include/vm/AppWindow.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
** EPITECH PROJECT, 2025
3+
** video-code
4+
** File description:
5+
** AppWindow
6+
*/
7+
8+
#pragma once
9+
10+
#include "qapplication.h"
11+
#include "qboxlayout.h"
12+
#include "qlabel.h"
13+
#include "qtimer.h"
14+
#include "vm/AppEvent.hpp"
15+
16+
class AppWindow
17+
{
18+
public:
19+
20+
AppWindow(int argc, char **argv, const std::map<int, std::function<void()>> &events, const std::string &sourceFile, int width, int height, int frameRate, std::function<void(QLabel &imageLabel)> &&mainLoop);
21+
22+
///< Start the app
23+
int run();
24+
25+
private:
26+
27+
///< QT app
28+
QApplication _app;
29+
30+
///< QT window
31+
AppEvent _window;
32+
33+
///< Label containing the image
34+
QLabel _imageLabel;
35+
36+
///< Layout linking the label and the window
37+
QVBoxLayout _imageLayout;
38+
39+
///< Encapsulation of the Main loop which updates the current frame
40+
QTimer _timer;
41+
42+
///< Main loop
43+
std::function<void(QLabel &imageLabel)> _mainLoop;
44+
};
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,32 @@
1313
#include <cstddef>
1414
#include <functional>
1515
#include <map>
16+
#include <memory>
1617
#include <nlohmann/json.hpp>
1718
#include <opencv2/core/mat.hpp>
1819
#include <string>
1920
#include <vector>
2021

21-
#include "qboxlayout.h"
2222
#include "qlabel.h"
2323
#include "qnamespace.h"
24-
#include "qtimer.h"
24+
#include "vm/AppWindow.hpp"
2525
#include "vm/Register.hpp"
26-
#include "vm/WindowEvent.hpp"
2726

2827
#define bindCmd(x) ([this]() { this->x(); })
29-
#define bindInst(x) ([this](const json::array_t &args) { return this->x(args); })
30-
#define bindTsf(x) ([this](std::shared_ptr<_IInput> input, const json::array_t &args) { return this->x(input, args); })
3128

3229
using json = nlohmann::json;
3330

34-
class LiveWindow
31+
class VideoCode
3532
{
3633
public:
3734

38-
LiveWindow(int &argc, char **argv, int width, int height, std::string &&_sourceFile = "video.py");
39-
~LiveWindow();
35+
VideoCode(int argc, char **argv, int width, int height, std::string sourceFile, bool generate, std::string outputFile);
4036

4137
///< Start the program
4238
int run();
4339

4440
///< Called every loop iteration to update the current frame being displayed
45-
void updateFrame();
41+
void updateFrame(QLabel &imageLabel);
4642

4743
///< Set the index of the timeline to a label
4844
void goToLabel(const std::string &label);
@@ -75,30 +71,15 @@ class LiveWindow
7571
const int _frameRate{24};
7672

7773
///< Window size
78-
const int _width{1920};
79-
const int _height{1080};
74+
const int _width;
75+
const int _height;
8076

8177
///< source file
8278
const std::string _sourceFile;
8379

8480
///< Black frame for empty timelines
8581
const cv::Mat _defaultBlackFrame;
8682

87-
///< QT app
88-
QApplication _app;
89-
90-
///< QT window
91-
WindowEvent _window;
92-
93-
///< Label containing the image
94-
QLabel _imageLabel;
95-
96-
///< Layout linking the label and the window
97-
QVBoxLayout _imageLayout;
98-
99-
///< Main loop updating the current frame
100-
QTimer _timer;
101-
10283
///< Current index of the frame of the timeline being displayed
10384
std::size_t _index{0};
10485

@@ -139,4 +120,10 @@ class LiveWindow
139120

140121
///< Stack containing the transformations to apply to the Inputs
141122
json::array_t _actionStack{};
123+
124+
///< Window to modify the video in real time
125+
std::unique_ptr<AppWindow> _app{nullptr};
126+
127+
///< Output File for the generation
128+
std::string _outputFile;
142129
};

src/Main.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
** Main
66
*/
77

8+
#include <argparse/argparse.hpp>
89
#include <opencv2/opencv.hpp>
910

10-
#include "vm/LiveWindow.hpp"
11+
#include "vm/VideoCode.hpp"
1112

1213
int main(int argc, char *argv[])
1314
{
14-
LiveWindow win(argc, argv, 1920, 1080);
15+
argparse::ArgumentParser parser("Video-Code", "", argparse::default_arguments::help);
1516

16-
return win.run();
17+
parser.add_argument("--sourceFile").default_value("video.py").help("file containing the steps to create the video.");
18+
parser.add_argument("--generate").nargs(0, 1).default_value("output.mov").help("generate the video.");
19+
parser.parse_args(argc, argv);
20+
21+
VideoCode vc(argc, argv, 1920, 1080, parser.get("--sourceFile"), parser.is_used("--generate"), parser.get<std::string>("--generate"));
22+
23+
return vc.run();
1724
}

src/compiler/generateVideo.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
** EPITECH PROJECT, 2025
3+
** video-code
4+
** File description:
5+
** generateVideo
6+
*/
7+
8+
#include <cstdio>
9+
#include <format>
10+
11+
#include "compiler/Compiler.hpp"
12+
#include "opencv2/imgcodecs.hpp"
13+
#include "opencv2/imgproc.hpp"
14+
#include "utils/Debug.hpp"
15+
#include "utils/Exception.hpp"
16+
17+
int Compiler::Writer::generateVideo(
18+
int width,
19+
int height,
20+
int fps,
21+
std::string filename,
22+
const std::vector<cv::Mat> &frames
23+
)
24+
{
25+
FILE *ffmpegPipe = popen(
26+
std::format(
27+
"ffmpeg"
28+
" -y" // override existing file
29+
" -f rawvideo" // rawvideo codec
30+
" -pixel_format rgba" // red green blue alpha
31+
" -video_size {}x{}" // width and height
32+
" -framerate {}" // fps
33+
" -an" // tells ffmpeg to expect no audio
34+
" -i -" // the inputs comes from stdin (pipe)
35+
" -c:v prores_ks" // Use VP9 codec (supports alpha)
36+
" -pix_fmt yuva444p10le" // Ensure alpha channel is preserved
37+
" -loglevel warning" // display only warnings
38+
" {}", // output filename
39+
width,
40+
height,
41+
fps,
42+
filename
43+
)
44+
.c_str(),
45+
"w"
46+
);
47+
48+
if (!ffmpegPipe)
49+
{
50+
throw Error("Could not start the ffmpeg pipe.");
51+
}
52+
53+
int i = 0;
54+
for (const auto &frame : frames)
55+
{
56+
i++;
57+
cv::Mat rgba_frame = frame;
58+
cv::cvtColor(frame, rgba_frame, cv::COLOR_BGRA2RGBA);
59+
60+
if (i > 10)
61+
{
62+
break;
63+
}
64+
65+
fwrite(rgba_frame.data, 1, rgba_frame.total() * rgba_frame.elemSize(), ffmpegPipe);
66+
fflush(ffmpegPipe);
67+
}
68+
69+
pclose(ffmpegPipe);
70+
71+
VC_LOG_DEBUG("video generated as:")
72+
VC_LOG_DEBUG(filename.c_str())
73+
74+
return 0;
75+
}

src/input/Image.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ Image::Image(const std::string& inputName)
3131

3232
_frames = {image};
3333
}
34+
35+
///< After lots of tests, I feel like opencv crashs with the handling of RGBA.
36+
///< So I'll keep them BGRA until the last second which is the display.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
** WindowEvent
66
*/
77

8-
#include "vm/WindowEvent.hpp"
8+
#include "vm/AppEvent.hpp"
99

10-
WindowEvent::WindowEvent(const std::map<int, std::function<void()>> &events, std::string &sourceFile, int width, int height, std::function<void()> &&mainLoop)
10+
AppEvent::AppEvent(const std::map<int, std::function<void()>> &events, const std::string &sourceFile, int width, int height, std::function<void()> &&mainLoop)
1111
: _mainLoop(mainLoop)
1212
, _events(events)
1313
{
@@ -19,7 +19,7 @@ WindowEvent::WindowEvent(const std::map<int, std::function<void()>> &events, std
1919
show();
2020
}
2121

22-
void WindowEvent::mainLoop()
22+
void AppEvent::mainLoop()
2323
{
2424
_mainLoop();
2525
}

0 commit comments

Comments
 (0)