Skip to content

Commit 6db0775

Browse files
committed
Add support for MinGW / Windows
1 parent 58e7f4a commit 6db0775

8 files changed

+74
-37
lines changed

CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project("PygaMasher")
2-
cmake_minimum_required(VERSION 3.13)
2+
cmake_minimum_required(VERSION 3.12)
33
set (CMAKE_CXX_STANDARD 17)
44

55
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
@@ -11,7 +11,9 @@ find_package(PkgConfig)
1111

1212
pkg_check_modules(GTKMM gtkmm-3.0)
1313

14-
add_executable(PygaMasher main.cpp mainwindow.cpp imagewriter.cpp imagereader.cpp commandrunner.cpp)
14+
add_executable(PygaMasher main.cpp mainwindow.cpp imagewriter.cpp imagereader.cpp commandrunner.cpp)
15+
16+
1517
target_link_libraries(PygaMasher stdc++fs pthread)
1618

1719
link_directories(${GTKMM_LIBRARY_DIRS})

commandrunner.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#include "commandrunner.hpp"
22

33
void CommandRunner::runCommand(const std::string &str) {
4+
#ifndef __MINGW32__
45
fp = popen(str.c_str(), "r");
56
d = fileno(fp);
67
fcntl(d, F_SETFL, O_NONBLOCK);
78
if(fp == NULL) {
89
printf("Failed to run command\n");
910
exit(1);
1011
}
12+
#else
13+
if((fp = _popen(str.c_str(), "r")) == NULL) {
14+
exit(1);
15+
}
16+
#endif
1117
closed = false;
1218
}
1319

@@ -19,7 +25,7 @@ std::string CommandRunner::readAvail() {
1925
char buf[1024];
2026
size_t count = 1024;
2127
do {
22-
ssize_t r = read(d, buf, count);
28+
ssize_t r = fread(buf, 1, count, fp);
2329
if(r == -1 && errno == EAGAIN) {
2430
// no data yet
2531
dataInPipe = false;
@@ -37,4 +43,3 @@ std::string CommandRunner::readAvail() {
3743
}
3844

3945
void CommandRunner::close() { pclose(fp); }
40-

commandrunner.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#pragma once
22

3-
#include <fcntl.h>
3+
#include <iostream>
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <string>
7-
#include <unistd.h>
87

8+
#ifndef __MINGW32__
9+
# include <fcntl.h>
10+
# include <unistd.h>
11+
#endif
912
class CommandRunner {
1013
public:
1114
void runCommand(const std::string &str);

ffmpeg.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include <string>
3+
#ifdef _WIN32
4+
static std::string ffmpegName = "ffmpeg.exe";
5+
#else
6+
static std::string ffmpegName = "ffmpeg";
7+
#endif

imagereader.cpp

+22-11
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
ImageReader::ImageReader() {}
44
void ImageReader::start() {
5-
std::filesystem::remove_all(cachePath);
6-
std::filesystem::create_directory(cachePath);
7-
std::string command = "ffmpeg -y -i " + inputVideo
8-
+ " -vf \"select=not(mod(n\\,30))\" -vsync vfr -q:v 2 "
9-
+ cachePath + "/img_%05d.png";
5+
try {
6+
if(std::filesystem::exists(cachePath))
7+
std::filesystem::remove_all(cachePath);
8+
std::filesystem::create_directory(cachePath);
9+
} catch(const std::exception &e) {
10+
std::cout << e.what() << "\n";
11+
}
12+
13+
std::filesystem::path fullPath = cachePath / "img_%05d.png";
14+
15+
std::string command = ffmpegName + " -y -i \"" + inputVideo.string()
16+
+ "\" -vf \"fps=1\" -q:v 2 \"" + fullPath.string()
17+
+ "\"";
1018
CommandRunner runner;
1119
runner.runCommand(command);
1220
while(runner.readable()) {
@@ -16,7 +24,7 @@ void ImageReader::start() {
1624

1725
outFile.open(outputPath, std::ios::binary | std::ios::out);
1826

19-
std::vector<std::string> paths;
27+
std::vector<std::filesystem::path> paths;
2028
for(const auto &p : std::filesystem::directory_iterator(cachePath)) {
2129
if(p.path().extension() == ".png") {
2230
paths.push_back(p.path());
@@ -32,9 +40,9 @@ void ImageReader::start() {
3240
outFile.close();
3341
}
3442

35-
void ImageReader::readImage(const std::string &path) {
43+
void ImageReader::readImage(const std::filesystem::path &path) {
3644
int w, h, n;
37-
unsigned char *data = stbi_load(path.c_str(), &w, &h, &n, 1);
45+
unsigned char *data = stbi_load(path.string().c_str(), &w, &h, &n, 1);
3846

3947
auto getPixel = [&data, w](size_t x, size_t y) {
4048
size_t i = y * w + x;
@@ -64,6 +72,9 @@ void ImageReader::readImage(const std::string &path) {
6472
free(data);
6573
}
6674

67-
void ImageReader::setInputFile(const std::string &path) { inputVideo = path; }
68-
void ImageReader::setOutputFile(const std::string &path) { outputPath = path; }
69-
75+
void ImageReader::setInputFile(const std::filesystem::path &path) {
76+
inputVideo = path;
77+
}
78+
void ImageReader::setOutputFile(const std::filesystem::path &path) {
79+
outputPath = path;
80+
}

imagereader.hpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "commandrunner.hpp"
4+
#include "ffmpeg.hpp"
45
#include "stb/stb_image.h"
56

67
#include <algorithm>
@@ -11,17 +12,17 @@
1112
class ImageReader {
1213
public:
1314
ImageReader();
14-
void setInputFile(const std::string &path);
15-
void setOutputFile(const std::string &path);
15+
void setInputFile(const std::filesystem::path &path);
16+
void setOutputFile(const std::filesystem::path &path);
1617
void start();
1718

1819
private:
1920
std::ofstream outFile;
20-
void readImage(const std::string &path);
21+
void readImage(const std::filesystem::path &path);
2122

22-
std::string inputVideo = "out.mp4";
23-
std::string cachePath = "out";
24-
std::string outputPath = "file";
23+
std::filesystem::path inputVideo = "out.mp4";
24+
std::filesystem::path cachePath = "PygaMasher_wd";
25+
std::filesystem::path outputPath = "file";
2526

2627
size_t chunkSize = 8;
2728
};

imagewriter.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ void ImageWriter::start() {
55

66
currentByte = 0;
77
currentImage = 0;
8-
std::filesystem::remove_all(wd);
9-
std::filesystem::create_directory(wd);
8+
try {
9+
if(std::filesystem::exists(wd))
10+
std::filesystem::remove_all(wd);
11+
std::filesystem::create_directory(wd);
12+
} catch(const std::exception &e) {
13+
std::cout << e.what() << "\n";
14+
exit(1);
15+
}
1016
}
1117
void ImageWriter::writeData(char *data, size_t size) {
1218
size_t dataRemaining = size;
@@ -70,18 +76,20 @@ void ImageWriter::flushImage() {
7076
sprintf(t, "%05zu", currentImage++);
7177
std::string number = t;
7278

73-
std::string fileName = wd + "/" + number + ".png";
74-
stbi_write_png(fileName.c_str(), width, height, 1, &image[0], width);
79+
std::string fileName = number + ".png";
80+
std::filesystem::path filePath = wd / fileName;
81+
stbi_write_png(filePath.string().c_str(), width, height, 1, &image[0], width);
7582
currentImageData.resize(0);
7683
}
7784

7885
void ImageWriter::end() {
7986
// TODO: Mark data ending with something or have a header
8087
flushImage();
81-
std::string command = "ffmpeg -y -framerate 1 -pattern_type glob -i '" + wd
82-
+ "/*.png' -c:v libx264 -x264-params "
83-
"keyint=30:scenecut=0 -r 30 -pix_fmt rgb24 '"
84-
+ fileOutputPath + "' &> log.txt ";
88+
std::filesystem::path fullWdPath = wd / "%05d.png";
89+
90+
std::string command = ffmpegName + " -y -framerate 1 -i \""
91+
+ fullWdPath.string() + "\" -c:v libx264 \""
92+
+ fileOutputPath.string() + "\"";
8593
cmdRunner.runCommand(command);
8694

8795
std::string log;
@@ -97,4 +105,3 @@ void ImageWriter::end() {
97105
void ImageWriter::setOutputFile(const std::string &path) {
98106
fileOutputPath = path;
99107
}
100-

imagewriter.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "commandrunner.hpp"
4+
#include "ffmpeg.hpp"
45
#include "stb/stb_image_write.h"
56

67
#include <algorithm>
@@ -22,9 +23,9 @@ class ImageWriter {
2223
private:
2324
void flushImage();
2425
size_t width = 1280, height = 720, chunkSize = 8, bytesPerImage, currentByte;
25-
std::string wd = "out";
26-
std::string fileOutputPath;
27-
std::vector<uint8_t> currentImageData;
28-
size_t currentImage;
29-
CommandRunner cmdRunner;
26+
std::filesystem::path wd = "PygaMasher_wd";
27+
std::filesystem::path fileOutputPath;
28+
std::vector<uint8_t> currentImageData;
29+
size_t currentImage;
30+
CommandRunner cmdRunner;
3031
};

0 commit comments

Comments
 (0)