|
| 1 | +#include <stdlib.h> |
| 2 | + |
| 3 | +#include "os.h" |
| 4 | +#include "m3u8errors.h" |
| 5 | +#include "ffmpegc_muxer.h" |
| 6 | + |
| 7 | +static const char* const FFMPEG_DEFAULT_INPUT_FLAGS[] = { |
| 8 | + "-y", "-nostdin", "-nostats", "-loglevel", "error" |
| 9 | +}; |
| 10 | + |
| 11 | +static const char* const FFMPEG_DEFAULT_OUTPUT_FLAGS[] = { |
| 12 | + "-map", "a", |
| 13 | + "-c", "copy", |
| 14 | + "-movflags", "+faststart", |
| 15 | + "-max_interleave_delta", "0", |
| 16 | + "-fflags", "+bitexact+autobsf" |
| 17 | +}; |
| 18 | + |
| 19 | +int ffmpegc_mux_streams(char* const* const sources, const char* const destination) { |
| 20 | + |
| 21 | + int err = M3U8ERR_SUCCESS; |
| 22 | + |
| 23 | + char* executable = NULL; |
| 24 | + char* command = NULL; |
| 25 | + char**argv = NULL; |
| 26 | + |
| 27 | + size_t index = 0; |
| 28 | + size_t argvpos = 0; |
| 29 | + size_t size = 0; |
| 30 | + size_t argc = 0; |
| 31 | + |
| 32 | + argc += sizeof(FFMPEG_DEFAULT_INPUT_FLAGS) / sizeof(*FFMPEG_DEFAULT_INPUT_FLAGS); |
| 33 | + argc += sizeof(FFMPEG_DEFAULT_OUTPUT_FLAGS) / sizeof(*FFMPEG_DEFAULT_OUTPUT_FLAGS); |
| 34 | + |
| 35 | + for (index = 0; 1; index++) { |
| 36 | + const char* const item = sources[index]; |
| 37 | + |
| 38 | + if (item == NULL) { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + argc += 1; /* <command> */ |
| 44 | + argc += index * 2; /* -i <input> */ |
| 45 | + argc += 1; /* <output> */ |
| 46 | + |
| 47 | + executable = find_exe("ffmpeg"); |
| 48 | + |
| 49 | + if (executable == NULL) { |
| 50 | + err = M3U8ERR_FFMPEG_COMMAND_NOT_FOUND; |
| 51 | + goto end; |
| 52 | + } |
| 53 | + |
| 54 | + argv = malloc(sizeof(*argv) * argc); |
| 55 | + |
| 56 | + if (argv == NULL) { |
| 57 | + err = M3U8ERR_MEMORY_ALLOCATE_FAILURE; |
| 58 | + goto end; |
| 59 | + } |
| 60 | + |
| 61 | + argv[argvpos++] = executable; |
| 62 | + |
| 63 | + for (index = 0; index < sizeof(FFMPEG_DEFAULT_INPUT_FLAGS) / sizeof(*FFMPEG_DEFAULT_INPUT_FLAGS); index++) { |
| 64 | + const char* const item = FFMPEG_DEFAULT_INPUT_FLAGS[index]; |
| 65 | + argv[argvpos++] = (char*) item; |
| 66 | + } |
| 67 | + |
| 68 | + for (index = 0; 1; index++) { |
| 69 | + const char* const item = sources[index]; |
| 70 | + |
| 71 | + if (item == NULL) { |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + argv[argvpos++] = "-i"; |
| 76 | + argv[argvpos++] = (char*) item; |
| 77 | + } |
| 78 | + |
| 79 | + for (index = 0; index < sizeof(FFMPEG_DEFAULT_OUTPUT_FLAGS) / sizeof(*FFMPEG_DEFAULT_OUTPUT_FLAGS); index++) { |
| 80 | + const char* const item = FFMPEG_DEFAULT_OUTPUT_FLAGS[index]; |
| 81 | + argv[argvpos++] = (char*) item; |
| 82 | + } |
| 83 | + |
| 84 | + argv[argvpos++] = (char*) destination; |
| 85 | + |
| 86 | + for (index = 0; index < argvpos; index++) { |
| 87 | + const char* const arg = argv[index]; |
| 88 | + size += strlen(arg) + 1; |
| 89 | + } |
| 90 | + |
| 91 | + size += 1; /* \0 */ |
| 92 | + |
| 93 | + command = malloc(size); |
| 94 | + |
| 95 | + if (command == NULL) { |
| 96 | + err = M3U8ERR_MEMORY_ALLOCATE_FAILURE; |
| 97 | + goto end; |
| 98 | + } |
| 99 | + |
| 100 | + command[0] = '\0'; |
| 101 | + |
| 102 | + for (index = 0; index < argvpos; index++) { |
| 103 | + const char* const arg = argv[index]; |
| 104 | + strcat(command, arg); |
| 105 | + strcat(command, " "); |
| 106 | + } |
| 107 | + puts(command); |
| 108 | + |
| 109 | + if (execute_shell_command(command) != 0) { |
| 110 | + err = M3U8ERR_FFMPEG_MUXING_FAILURE; |
| 111 | + goto end; |
| 112 | + } |
| 113 | + |
| 114 | + end:; |
| 115 | + |
| 116 | + free(executable); |
| 117 | + free(argv); |
| 118 | + free(command); |
| 119 | + |
| 120 | + return err; |
| 121 | + |
| 122 | +} |
0 commit comments