ffmpeg_linux.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. #define READ_END 0
  11. #define WRITE_END 1
  12. typedef struct {
  13. int pid;
  14. int pipe;
  15. } FFMPEG;
  16. FFMPEG *ffmpeg_start_rendering(size_t width, size_t height, size_t fps)
  17. {
  18. int pipefd[2];
  19. if (pipe(pipefd) < 0) {
  20. fprintf(stderr, "ERROR: could not create a pipe: %s\n", strerror(errno));
  21. return NULL;
  22. }
  23. pid_t child = fork();
  24. if (child < 0) {
  25. fprintf(stderr, "ERROR: could not fork a child: %s\n", strerror(errno));
  26. return NULL;
  27. }
  28. if (child == 0) {
  29. if (dup2(pipefd[READ_END], STDIN_FILENO) < 0) {
  30. fprintf(stderr, "ERROR: could not reopen read end of pipe as stdin: %s\n", strerror(errno));
  31. exit(1);
  32. }
  33. close(pipefd[WRITE_END]);
  34. char resolution[64];
  35. snprintf(resolution, sizeof(resolution), "%zux%zu", width, height);
  36. char framerate[64];
  37. snprintf(framerate, sizeof(framerate), "%zu", fps);
  38. int ret = execlp("ffmpeg",
  39. "ffmpeg",
  40. "-loglevel", "verbose",
  41. "-y",
  42. "-f", "rawvideo",
  43. "-pix_fmt", "rgba",
  44. "-s", resolution,
  45. "-r", framerate,
  46. "-i", "-",
  47. "-c:v", "libx264",
  48. "-vb", "2500k",
  49. "-c:a", "aac",
  50. "-ab", "200k",
  51. "-pix_fmt", "yuv420p",
  52. "output.mp4",
  53. NULL
  54. );
  55. if (ret < 0) {
  56. fprintf(stderr, "ERROR: could not run ffmpeg as a child process: %s\n", strerror(errno));
  57. exit(1);
  58. }
  59. assert(0 && "unreachable");
  60. }
  61. close(pipefd[READ_END]);
  62. FFMPEG *ffmpeg = malloc(sizeof(FFMPEG));
  63. assert(ffmpeg != NULL && "Buy MORE RAM lol!!");
  64. ffmpeg->pid = child;
  65. ffmpeg->pipe = pipefd[WRITE_END];
  66. return ffmpeg;
  67. }
  68. void ffmpeg_end_rendering(FFMPEG *ffmpeg)
  69. {
  70. close(ffmpeg->pipe);
  71. waitpid(ffmpeg->pid, NULL, 0);
  72. }
  73. void ffmpeg_send_frame(FFMPEG *ffmpeg, void *data, size_t width, size_t height)
  74. {
  75. write(ffmpeg->pipe, data, sizeof(uint32_t)*width*height);
  76. }
  77. void ffmpeg_send_frame_flipped(FFMPEG *ffmpeg, void *data, size_t width, size_t height)
  78. {
  79. for (size_t y = height; y > 0; --y) {
  80. write(ffmpeg->pipe, (uint32_t*)data + (y - 1)*width, sizeof(uint32_t)*width);
  81. }
  82. }