diff options
author | nathansmith <nathansmith@posteo.com> | 2025-04-26 17:24:04 +0000 |
---|---|---|
committer | nathansmith <nathansmith@posteo.com> | 2025-04-26 17:24:04 +0000 |
commit | 6d338602aa98cc9dfab39b83702af0e548286596 (patch) | |
tree | 8127ea3ea5431d91a1c9c6e3c3a4a0f533807288 /src | |
parent | 607d9eacb2c3b0a7be5bba3cf5cc8dc80388342c (diff) | |
download | sldj-6d338602aa98cc9dfab39b83702af0e548286596.tar.gz sldj-6d338602aa98cc9dfab39b83702af0e548286596.tar.bz2 sldj-6d338602aa98cc9dfab39b83702af0e548286596.zip |
Working on ffmpeg finally
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/ffmpeg.c | 84 | ||||
-rw-r--r-- | src/ffmpeg.h | 25 | ||||
-rw-r--r-- | src/libsldj/Makefile | 5 | ||||
-rw-r--r-- | src/sldj.c | 2 |
5 files changed, 114 insertions, 4 deletions
diff --git a/src/Makefile b/src/Makefile index 8ff8cfe..4d2ca1f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -Isrc -I../include +CFLAGS = -Isrc -I../include -std=c99 LDFLAGS = -lm -lraylib -ltcc TARGET = sldj diff --git a/src/ffmpeg.c b/src/ffmpeg.c index 72dfef8..bbc94f7 100644 --- a/src/ffmpeg.c +++ b/src/ffmpeg.c @@ -1,2 +1,86 @@ #include "ffmpeg.h" +FFMPEG* ffmpegStart(const char* filename, int width, int height, int fps) +{ + int pipefd[2]; + + if (pipe(pipefd) < 0) + { + TraceLog(LOG_ERROR, "FFMPEG: Could not create a pipe: %s", strerror(errno)); + return NULL; + } + + pid_t child = fork(); + + if (child < 0) + { + TraceLog(LOG_ERROR, "FFMPEG: could not fork a child: %s", strerror(errno)); + return NULL; + } + + if (child == 0) + { + if (dup2(pipefd[READ_END], STDIN_FILENO) < 0) + { + TraceLog(LOG_ERROR, "FFMPEG CHILD: could not reopen read end of pipe as stdin: %s", strerror(errno)); + exit(1); + } + + close(pipefd[WRITE_END]); + + char resolution[64]; + snprintf(resolution, sizeof(resolution), "%zux%zu", (size_t)width, (size_t)height); + char framerate[64]; + snprintf(framerate, sizeof(framerate), "%zu", (size_t)fps); + + int ret = execlp("ffmpeg", + "ffmpeg", + "-loglevel", "verbose", + "-y", + + "-f", "rawvideo", + "-pix_fmt", "rgba", + "-s", resolution, + "-r", framerate, + "-i", "-", + "-c:v", "libx264", + "-vb", "2500k", + "-c:a", "aac", + "-ab", "200k", + "-pix_fmt", "yuv420p", + filename, + NULL + ); + + if (ret < 0) + { + TraceLog(LOG_ERROR, "FFMPEG CHILD: could not run ffmpeg as a child process: %s", strerror(errno)); + exit(1); + } + + assert(0 && "unreachable"); + exit(1); + } + + if (close(pipefd[READ_END]) < 0) + { + TraceLog(LOG_WARNING, "FFMPEG: could not close read end of the pipe on the parent's end: %s", strerror(errno)); + } + + FFMPEG *ffmpeg = malloc(sizeof(FFMPEG)); + assert(ffmpeg != NULL && "Buy MORE RAM lol!!"); + ffmpeg->pid = child; + ffmpeg->pipe = pipefd[WRITE_END]; + return ffmpeg; +} + +int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, int widtth, int height) +{ + return 0; +} + +int ffmpegEnd(FFMPEG* ffmpeg) +{ + return 0; +} + diff --git a/src/ffmpeg.h b/src/ffmpeg.h index 56ef6d9..71277a5 100644 --- a/src/ffmpeg.h +++ b/src/ffmpeg.h @@ -1,10 +1,35 @@ +#include <assert.h> #include <stdio.h> +#include <stdint.h> #include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include <signal.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +#include <raylib.h> #include "sldjConfig.h" +// stolen code: https://github.com/tsoding/musializer/blob/master/src/ffmpeg_posix.c + #ifndef FFMPEG_H #define FFMPEG_H +#define READ_END 0 +#define WRITE_END 1 + +typedef struct FFMPEG { + int pipe; + pid_t pid; +} FFMPEG; + +FFMPEG* ffmpegStart(const char* filename, int width, int height, int fps); +int ffmpegSendFrame(FFMPEG* ffmpeg, void* data, int widtth, int height); +int ffmpegEnd(FFMPEG* ffmpeg); + #endif diff --git a/src/libsldj/Makefile b/src/libsldj/Makefile index fc15314..afaf181 100644 --- a/src/libsldj/Makefile +++ b/src/libsldj/Makefile @@ -1,11 +1,12 @@ +CFLAGS = -std=c99 TARGET = libsldj.so SOURCES = $(shell find -name "*.c") OBJECTS = $(SOURCES:.c=.o) %.o: %.c - $(CC) -c -fPIC -o $@ $< + $(CC) $(CFLAGS) -c -fPIC -o $@ $< $(TARGET): $(OBJECTS) - $(CC) -shared -o $(TARGET) $(OBJECTS) + $(CC) $(CFLAGS) -shared -o $(TARGET) $(OBJECTS) clean: rm *.o rm $(TARGET) @@ -1,6 +1,6 @@ #include "sldj.h" -void initSldj(Sldj* sldj) +void initSldj(Sldj *sldj) { // Create window. InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Scanline DJ"); |