Mercurial
diff third_party/ffmpeg/ffmpeg_hls_test.c @ 242:543df0fe7168
[tools] Add full HLS player support
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 03 Aug 2026 13:14:41 -0700 |
| parents | |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/third_party/ffmpeg/ffmpeg_hls_test.c Mon Aug 03 13:14:41 2026 -0700 @@ -0,0 +1,82 @@ +#include "third_party/ffmpeg/ffmpeg_cli.h" + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static char *read_file(const char *path) +{ + FILE *file = fopen(path, "rb"); + if (!file) + return NULL; + fseek(file, 0, SEEK_END); + long length = ftell(file); + fseek(file, 0, SEEK_SET); + char *content = malloc((size_t)length + 1); + fread(content, 1, (size_t)length, file); + content[length] = '\0'; + fclose(file); + return content; +} + +int main(int argc, char **argv) +{ + assert(argc == 2); + char temporary[] = "/tmp/ffmpeg-hls-test-XXXXXX"; + assert(mkdtemp(temporary)); + + Fmp4HlsOpts options = ffmpeg_fmp4_hls_opts_default(); + options.max_width = 480; + options.max_height = 360; + options.video_bitrate = 350; + options.codec = FFMPEG_HLS_VP9_OPUS; + FfmpegResult result = ffmpeg_video_to_fmp4_hls( + argv[1], + temporary, + "test", + &options); + if (result != FFMPEG_OK) + { + fprintf(stderr, "%s\n", ffmpeg_last_error()); + return 1; + } + + char playlist_path[512]; + snprintf(playlist_path, sizeof(playlist_path), "%s/test-stream.m3u8", temporary); + char *playlist = read_file(playlist_path); + assert(playlist); + assert(strstr(playlist, "#EXT-X-MAP:URI=\"test-init.mp4\"")); + assert(strstr(playlist, "test-segment000.m4s")); + assert(strstr(playlist, "#EXT-X-ENDLIST")); + free(playlist); + + options.codec = FFMPEG_HLS_H264_AAC; + options.use_mpeg_ts = true; + result = ffmpeg_video_to_fmp4_hls( + argv[1], + temporary, + "test-ts", + &options); + if (result != FFMPEG_OK) + { + fprintf(stderr, "%s\n", ffmpeg_last_error()); + return 1; + } + snprintf( + playlist_path, + sizeof(playlist_path), + "%s/test-ts-stream.m3u8", + temporary); + playlist = read_file(playlist_path); + assert(playlist); + assert(!strstr(playlist, "#EXT-X-MAP")); + assert(strstr(playlist, "test-ts-segment000.ts")); + assert(strstr(playlist, "#EXT-X-ENDLIST")); + free(playlist); + + char cleanup[640]; + snprintf(cleanup, sizeof(cleanup), "rm -rf '%s'", temporary); + return system(cleanup) == 0 ? 0 : 1; +}