Advertisement
Guest User

Untitled

a guest
Feb 5th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #define PIPE_OUT 0
  2. #define PIPE_IN 1
  3.  
  4. // based on https://github.com/tsoding/musializer/blob/master/src/ffmpeg_linux.c
  5. typedef struct{
  6. int out_fd;
  7. pid_t pid;
  8. } FFMPEG;
  9.  
  10. bool ffmpeg_start_screen_capture(
  11. FFMPEG* self,
  12. unsigned int pos_x,
  13. unsigned int pos_y,
  14. unsigned int res_x,
  15. unsigned int res_y
  16. ){
  17. int pipefd[2];
  18. if(pipe(pipefd) < 0){
  19. TraceLog(LOG_ERROR, "ffmpeg failed to init pipe");
  20. return false;
  21. }
  22.  
  23. pid_t child = fork();
  24. if(child < 0){
  25. TraceLog(LOG_ERROR, "ffmpeg failed to fork()");
  26. return false;
  27. }
  28.  
  29. if(child == 0){
  30. if(dup2(pipefd[PIPE_IN], STDOUT_FILENO) < 0){
  31. TraceLog(LOG_ERROR, "ffmpeg failed to bind parent pipe to child stdout");
  32. return false;
  33. }
  34. close(pipefd[PIPE_OUT]);
  35.  
  36. // redirects ffmpeg logging
  37. #if 1
  38. int null_fd = open("/dev/null", O_WRONLY);
  39. if(null_fd < 0){
  40. TraceLog(LOG_ERROR, "ffmpeg failed to open null pipe");
  41. return false;
  42. }
  43. if(dup2(null_fd, STDERR_FILENO) < 0){
  44. TraceLog(LOG_ERROR, "ffmpeg failed to bind null pipe to child stderr");
  45. return false;
  46. }
  47. #endif
  48.  
  49. char res_arg[128];
  50. snprintf(res_arg, sizeof(res_arg), "%ux%u", res_x, res_y);
  51. char scale_arg[256];
  52. int res = snprintf(scale_arg, sizeof(res_arg), "scale=%u:%u", res_x, res_y);
  53. assert(res > 10);
  54. char pos_arg[128];
  55. snprintf(pos_arg, sizeof(res_arg), ":0.0+%u,%u", pos_x, pos_y);
  56.  
  57. fprintf(stderr, "scale_arg res: %d\n", res);
  58. fprintf(stderr, "scale_arg: %s\n", res_arg);
  59. int ret = execlp(
  60. "ffmpeg",
  61. "ffmpeg",
  62. "-video_size", "1920x1080",//res_arg,
  63. "-framerate", "30",
  64. "-f", "x11grab",
  65. "-i", pos_arg,
  66. "-f", "rawvideo",
  67. "-vf", scale_arg,
  68. "-pix_fmt","rgba",
  69. "-an",
  70. "-",
  71. NULL
  72. );
  73. if (ret < 0) {
  74. TraceLog(LOG_ERROR, "FFMPEG CHILD: could not run ffmpeg as a child process: %s", strerror(errno));
  75. exit(1);
  76. }
  77. assert(0 && "unreachable");
  78. exit(1);
  79. }
  80.  
  81. self->pid = child;
  82. self->out_fd = pipefd[PIPE_OUT];
  83. return true;
  84. }
  85.  
  86. void ffmpeg_read(FFMPEG* self, unsigned char* buffer, unsigned int res_x, unsigned int res_y){
  87. int frame_size = res_x * res_y * sizeof(Color);
  88. while(frame_size > 0){
  89. int res = read(self->out_fd, buffer, frame_size);
  90. if(res < 0){
  91. TraceLog(LOG_ERROR, "ffmpeg read error: %s", strerror(errno));
  92. exit(1);
  93. }
  94. frame_size-=res;
  95. buffer+=res;
  96. }
  97. }
  98.  
  99. void ffmpeg_stop(FFMPEG* self){
  100. close(self->out_fd);
  101. kill(self->pid, SIGTERM);
  102. waitpid(self->pid, NULL, 0);
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement