comparison 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
comparison
equal deleted inserted replaced
241:9c2eec61a152 242:543df0fe7168
1 #include "third_party/ffmpeg/ffmpeg_cli.h"
2
3 #include <assert.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 static char *read_file(const char *path)
10 {
11 FILE *file = fopen(path, "rb");
12 if (!file)
13 return NULL;
14 fseek(file, 0, SEEK_END);
15 long length = ftell(file);
16 fseek(file, 0, SEEK_SET);
17 char *content = malloc((size_t)length + 1);
18 fread(content, 1, (size_t)length, file);
19 content[length] = '\0';
20 fclose(file);
21 return content;
22 }
23
24 int main(int argc, char **argv)
25 {
26 assert(argc == 2);
27 char temporary[] = "/tmp/ffmpeg-hls-test-XXXXXX";
28 assert(mkdtemp(temporary));
29
30 Fmp4HlsOpts options = ffmpeg_fmp4_hls_opts_default();
31 options.max_width = 480;
32 options.max_height = 360;
33 options.video_bitrate = 350;
34 options.codec = FFMPEG_HLS_VP9_OPUS;
35 FfmpegResult result = ffmpeg_video_to_fmp4_hls(
36 argv[1],
37 temporary,
38 "test",
39 &options);
40 if (result != FFMPEG_OK)
41 {
42 fprintf(stderr, "%s\n", ffmpeg_last_error());
43 return 1;
44 }
45
46 char playlist_path[512];
47 snprintf(playlist_path, sizeof(playlist_path), "%s/test-stream.m3u8", temporary);
48 char *playlist = read_file(playlist_path);
49 assert(playlist);
50 assert(strstr(playlist, "#EXT-X-MAP:URI=\"test-init.mp4\""));
51 assert(strstr(playlist, "test-segment000.m4s"));
52 assert(strstr(playlist, "#EXT-X-ENDLIST"));
53 free(playlist);
54
55 options.codec = FFMPEG_HLS_H264_AAC;
56 options.use_mpeg_ts = true;
57 result = ffmpeg_video_to_fmp4_hls(
58 argv[1],
59 temporary,
60 "test-ts",
61 &options);
62 if (result != FFMPEG_OK)
63 {
64 fprintf(stderr, "%s\n", ffmpeg_last_error());
65 return 1;
66 }
67 snprintf(
68 playlist_path,
69 sizeof(playlist_path),
70 "%s/test-ts-stream.m3u8",
71 temporary);
72 playlist = read_file(playlist_path);
73 assert(playlist);
74 assert(!strstr(playlist, "#EXT-X-MAP"));
75 assert(strstr(playlist, "test-ts-segment000.ts"));
76 assert(strstr(playlist, "#EXT-X-ENDLIST"));
77 free(playlist);
78
79 char cleanup[640];
80 snprintf(cleanup, sizeof(cleanup), "rm -rf '%s'", temporary);
81 return system(cleanup) == 0 ? 0 : 1;
82 }