Skip to content

Commit 9321553

Browse files
committed
fix: use the same render arguments for linux and windows
1 parent 5d2a893 commit 9321553

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

src/graphics/video_renderer.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ std::optional<std::string> VideoRenderer::render(
9393
}
9494

9595
m_renderer->present();
96-
96+
9797
backend.add_frame(m_surface.get());
9898
m_clock->increment_simulation_step_index();
9999

@@ -106,6 +106,20 @@ std::optional<std::string> VideoRenderer::render(
106106
return std::nullopt;
107107
}
108108

109+
std::vector<std::string>
110+
VideoRendererBackend::get_encoding_paramaters(u32 fps, shapes::UPoint size, std::filesystem::path destination_path) {
111+
112+
std::string resolution = fmt::format("{}x{}", size.x, size.y);
113+
114+
std::string framerate = fmt::format("{}", fps);
115+
116+
return {
117+
"-loglevel", "verbose", "-y", "-f", "rawvideo", "-pix_fmt", "bgra", "-s",
118+
resolution, "-r", framerate, "-i", "-", "-c:v", "libx264", "-vb",
119+
"2500k", "-c:a", "aac", "-ab", "200k", "-pix_fmt", "yuv420p", destination_path.string(),
120+
};
121+
}
122+
109123

110124
// implementation of ServiceProvider
111125

src/graphics/video_renderer.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ struct Decoder;
8686
// See e.g. https://github.com/Raveler/ffmpeg-cpp
8787
struct VideoRendererBackend {
8888
private:
89-
std::string m_destination_path;
89+
std::filesystem::path m_destination_path;
9090
std::unique_ptr<Decoder> m_decoder;
9191

92+
[[nodiscard]] static std::vector<std::string>
93+
get_encoding_paramaters(u32 fps, shapes::UPoint size, std::filesystem::path destination_path);
94+
9295
public:
9396
OOPETRIS_GRAPHICS_EXPORTED explicit VideoRendererBackend(const std::filesystem::path& destination_path);
9497

src/graphics/video_renderer_linux.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ std::optional<std::string> VideoRendererBackend::setup(u32 fps, shapes::UPoint s
5151
}
5252
close(pipefd[WRITE_END]);
5353

54-
std::string resolution = fmt::format("{}x{}", size.x, size.y);
5554

56-
std::string framerate = fmt::format("{}", fps);
55+
auto paramaters = VideoRendererBackend::get_encoding_paramaters(fps, size, m_destination_path);
56+
57+
std::vector<const char*> args = { "ffmpeg" };
58+
for (const auto& parameter : paramaters) {
59+
args.push_back(parameter.c_str());
60+
}
61+
62+
args.push_back(nullptr);
63+
5764

5865
//TODO(Totto): support audio, that loops the music as in the main game
59-
int ret =
60-
execlp("ffmpeg", "ffmpeg", "-loglevel", "verbose", "-y", "-f", "rawvideo", "-pix_fmt", "bgra", "-s",
61-
resolution.c_str(), "-r", framerate.c_str(), "-i", "-", "-c:v", "libx264", "-vb", "2500k",
62-
"-c:a", "aac", "-ab", "200k", "-pix_fmt", "yuv420p", m_destination_path.c_str(),
63-
static_cast<char*>(nullptr));
66+
int ret = execvp("ffmpeg", args.data());
6467
if (ret < 0) {
6568
std::cerr << "FFMPEG CHILD: could not run ffmpeg as a child process: " << strerror(errno) << "\n";
6669
std::exit(1);

src/graphics/video_renderer_windows.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,24 @@ std::optional<std::string> VideoRendererBackend::setup(u32 fps, shapes::UPoint s
6464
PROCESS_INFORMATION piProcInfo;
6565
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
6666

67-
//TODO: do this in better c++ fashion and deduplicate this
68-
char cmd_buffer[1024 * 2];
69-
snprintf(
70-
cmd_buffer, sizeof(cmd_buffer),
71-
"ffmpeg.exe -loglevel verbose -y -f rawvideo -pix_fmt rgba -s %dx%d -r %d -i - -i \"%s\" -c:v libx264 -vb "
72-
"2500k -c:a aac -ab 200k -pix_fmt yuv420p output.mp4",
73-
(int) width, (int) height, (int) fps, sound_file_path
74-
);
75-
76-
if (!CreateProcess(NULL, cmd_buffer, NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo)) {
67+
68+
auto paramaters = VideoRendererBackend::get_encoding_paramaters(fps, size, m_destination_path);
69+
70+
std::stringstream args = "ffmpeg.exe";
71+
for (const auto& parameter : paramaters) {
72+
args += " ";
73+
if (paramater.find(" ") != std::string::npos) {
74+
args += "\"";
75+
args += paramater;
76+
args += "\"";
77+
78+
} else {
79+
args += paramater;
80+
}
81+
}
82+
83+
84+
if (!CreateProcess(NULL, result.string().c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo)) {
7785
CloseHandle(pipe_write);
7886
CloseHandle(pipe_read);
7987

0 commit comments

Comments
 (0)