ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
yt_dlp.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE 512
4#define MEDIA_YT_DLP_FULL_OUTPUT_BUFFER_SIZE 16384
5
7#include <ascii-chat/otel.h>
13
15#include <ascii-chat/common.h>
16#include <ascii-chat/log/log.h>
17#include <ascii-chat/log/io.h>
19#include <libplatform/process.h>
20#include <libplatform/terminal.h>
23#include <ascii-chat/util/url.h>
24#include <yyjson.h>
25#ifndef __EMSCRIPTEN__
26#include <libavformat/avformat.h>
27#include <libavformat/avio.h>
28#endif
29#include <stdlib.h>
30#include <string.h>
31#include <time.h>
32#include <stdio.h>
33#include <ctype.h>
34
35/* ============================================================================
36 * URL Extraction Cache
37 * ============================================================================ */
38
50typedef struct {
51 char url[MEDIA_URL_CACHE_SIZE]; // Original URL
52 char stream_url[MEDIA_STREAM_URL_SIZE]; // Video or combined stream URL
53 char audio_url[MEDIA_STREAM_URL_SIZE]; // Separate audio stream, if needed
56 time_t extracted_time; // When URL was extracted
57 bool valid; // Whether cache entry is valid
59
60static yt_dlp_cache_entry_t g_yt_dlp_cache = {0};
61static static_mutex_t g_yt_dlp_cache_lock = STATIC_MUTEX_INIT;
62
69static bool yt_dlp_cache_is_valid(void) {
70 if (!g_yt_dlp_cache.valid) {
71 return false;
72 }
73
74 time_t now = time(NULL);
75 time_t age = now - g_yt_dlp_cache.extracted_time;
76
77 // Cache valid for 30 seconds
78 return (age >= 0 && age < 30);
79}
80
87static bool yt_dlp_cache_get(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size, yt_dlp_request_options_t *video_request,
88 yt_dlp_request_options_t *audio_request) {
89 if (!yt_dlp_cache_is_valid()) {
90 return false;
91 }
92
93 // Check if cache matches requested URL and options
94 if (strcmp(g_yt_dlp_cache.url, url) != 0) {
95 return false;
96 }
97
98 // For cache key, compare options (NULL == "" for consistency)
99 const char *cached_opts = g_yt_dlp_cache.yt_dlp_options;
100 const char *req_opts = yt_dlp_options ? yt_dlp_options : "";
101 if (strcmp(cached_opts, req_opts) != 0) {
102 return false;
103 }
104
105 // Return cached URL (may be empty if cached failure)
106 size_t url_len = strlen(g_yt_dlp_cache.stream_url);
107 if (url_len >= output_size || strlen(g_yt_dlp_cache.audio_url) >= audio_size) {
108 return false;
109 }
110
111 SAFE_STRNCPY(output_url, g_yt_dlp_cache.stream_url, output_size - 1);
112 output_url[output_size - 1] = '\0';
113 SAFE_STRNCPY(audio_url, g_yt_dlp_cache.audio_url, audio_size - 1);
114 audio_url[audio_size - 1] = '\0';
115 *video_request = g_yt_dlp_cache.video_request;
116 *audio_request = g_yt_dlp_cache.audio_request;
117
118 if (url_len == 0) {
119 log_debug("Using cached yt-dlp failure for URL (failed %ld seconds ago)", (long)(time(NULL) - g_yt_dlp_cache.extracted_time));
120 } else {
121 log_debug("Using cached yt-dlp stream URL (extracted %ld seconds ago)", (long)(time(NULL) - g_yt_dlp_cache.extracted_time));
122 }
123 return true;
124}
125
132static void yt_dlp_cache_set(const char *url, const char *yt_dlp_options, const char *stream_url, const char *audio_url, const yt_dlp_request_options_t *video_request,
133 const yt_dlp_request_options_t *audio_request) {
134 if (strlen(url) >= sizeof(g_yt_dlp_cache.url)) {
135 return; // URL too long to cache
136 }
137
138 const char *opts = yt_dlp_options ? yt_dlp_options : "";
139 if (strlen(opts) >= sizeof(g_yt_dlp_cache.yt_dlp_options)) {
140 return; // Options too long to cache
141 }
142
143 if ((stream_url && strlen(stream_url) >= sizeof(g_yt_dlp_cache.stream_url)) || (audio_url && strlen(audio_url) >= sizeof(g_yt_dlp_cache.audio_url))) {
144 return; // Stream URL too long to cache
145 }
146
147 SAFE_STRNCPY(g_yt_dlp_cache.url, url, sizeof(g_yt_dlp_cache.url) - 1);
148 g_yt_dlp_cache.url[sizeof(g_yt_dlp_cache.url) - 1] = '\0';
149
150 SAFE_STRNCPY(g_yt_dlp_cache.yt_dlp_options, opts, sizeof(g_yt_dlp_cache.yt_dlp_options) - 1);
151 g_yt_dlp_cache.yt_dlp_options[sizeof(g_yt_dlp_cache.yt_dlp_options) - 1] = '\0';
152
153 if (stream_url) {
154 SAFE_STRNCPY(g_yt_dlp_cache.stream_url, stream_url, sizeof(g_yt_dlp_cache.stream_url) - 1);
155 } else {
156 // Mark as empty (failure state)
157 g_yt_dlp_cache.stream_url[0] = '\0';
158 }
159 g_yt_dlp_cache.stream_url[sizeof(g_yt_dlp_cache.stream_url) - 1] = '\0';
160 SAFE_STRNCPY(g_yt_dlp_cache.audio_url, audio_url ? audio_url : "", sizeof(g_yt_dlp_cache.audio_url) - 1);
161 g_yt_dlp_cache.audio_url[sizeof(g_yt_dlp_cache.audio_url) - 1] = '\0';
162
163 g_yt_dlp_cache.video_request = *video_request;
164 g_yt_dlp_cache.audio_request = *audio_request;
165 g_yt_dlp_cache.extracted_time = time(NULL);
166 g_yt_dlp_cache.valid = true;
167}
168
169/* ============================================================================
170 * Public API
171 * ============================================================================ */
172
174 ASCII_OTEL_SCOPE(YTDLP_AVAILABLE);
175 ASCII_OTEL_ATTR_STRING("media.extractor", "yt-dlp");
176 const char *argv[] = {"yt-dlp", "--version", NULL};
177 int ret = 0;
178 LOG_IO("yt-dlp", { ret = platform_execute_subprocess("yt-dlp", argv, NULL, 0, NULL, NULL); });
179 return (ret == 0);
180}
181
182static bool yt_dlp_is_available_interruptible(bool (*cancel)(void *), void *data) {
183 const char *argv[] = {"yt-dlp", "--version", NULL};
184 int ret = 0;
185 LOG_IO("yt-dlp", { ret = platform_execute_subprocess("yt-dlp", argv, NULL, 0, cancel, data); });
186 return ret == 0;
187}
188
189// Parse the documented options string into arguments without invoking a shell.
190// Quotes group whitespace; backslashes escape quotes and whitespace.
191static bool yt_dlp_parse_options(const char *options, char *storage, size_t storage_size, const char **argv, size_t *argc, size_t argv_size) {
192 size_t used = 0;
193 const char *p = options ? options : "";
194 while (*p) {
195 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
196 ++p;
197 if (!*p)
198 break;
199 if (*argc >= argv_size || used >= storage_size)
200 return false;
201 argv[(*argc)++] = storage + used;
202 char quote = 0;
203 while (*p && (quote || (*p != ' ' && *p != '\t' && *p != '\r' && *p != '\n'))) {
204 if ((*p == '\'' || *p == '"') && (!quote || quote == *p)) {
205 quote = quote ? 0 : *p;
206 ++p;
207 continue;
208 }
209 if (quote != '\'' && *p == '\\' && p[1] && (p[1] == quote || (!quote && (p[1] == ' ' || p[1] == '\t' || p[1] == '\'' || p[1] == '"'))))
210 ++p;
211 if (used + 1 >= storage_size)
212 return false;
213 storage[used++] = *p++;
214 }
215 if (quote || used >= storage_size)
216 return false;
217 storage[used++] = '\0';
218 }
219 return true;
220}
221
222// FFmpeg accepts network streams beyond the schemes supported by url_parse().
223static bool yt_dlp_is_stream_url(const char *line) {
224 static const char *const prefixes[] = {"http://", "https://", "rtmp://", "rtmps://", "rtmpe://", "rtmpt://", "rtmpte://", "rtmpts://", "rtsp://", "rtsps://", "rtp://", "srtp://",
225 "mms://", "mmsh://", "mmst://", "ftp://", "ftps://", "sftp://", "srt://", "rist://", "tcp://", "udp://", "udplite://"};
226 for (size_t i = 0; i < ARRAY_SIZE(prefixes); ++i) {
227 size_t length = strlen(prefixes[i]);
228 if (platform_strncasecmp(line, prefixes[i], length) != 0)
229 continue;
230 const char *authority = line + length;
231 if (!*authority || *authority == '/' || strpbrk(authority, " \t\r\n"))
232 return false;
233 if (i < 2)
234 return url_is_valid(line);
235#ifndef __EMSCRIPTEN__
236 // RTSP is a demuxer rather than an AVIO protocol.
237 if (strcmp(prefixes[i], "rtsp://") == 0 || strcmp(prefixes[i], "rtsps://") == 0)
238 return av_find_input_format("rtsp") != NULL;
239 const char *protocol = avio_find_protocol_name(line);
240 return protocol && strcmp(protocol, "file") != 0;
241#else
242 return false;
243#endif
244 }
245 return false;
246}
247
248static char *yt_dlp_next_cookie_attribute(char **cursor) {
249 char *start = *cursor;
250 if (!start || !*start)
251 return NULL;
252 bool quoted = false, escaped = false;
253 for (char *p = start; *p; ++p) {
254 if (escaped) {
255 escaped = false;
256 continue;
257 }
258 if (*p == '\\' && quoted) {
259 escaped = true;
260 continue;
261 }
262 if (*p == '"')
263 quoted = !quoted;
264 if (*p == ';' && !quoted) {
265 *p = '\0';
266 *cursor = p + 1;
267 return start;
268 }
269 }
270 if (quoted)
271 return NULL;
272 *cursor = NULL;
273 return start;
274}
275
276// yyjson's indexed object lookup uses an unsigned countdown on a missing key.
277// Iteration avoids that wraparound while retaining the same missing-key behavior.
278static yyjson_val *yt_dlp_object_get(yyjson_val *object, const char *name) {
279 yyjson_obj_iter iter = yyjson_obj_iter_with(object);
280 yyjson_val *key;
281 while ((key = yyjson_obj_iter_next(&iter))) {
282 if (yyjson_equals_str(key, name))
283 return yyjson_obj_iter_get_val(key);
284 }
285 return NULL;
286}
287
288// FFmpeg expects CRLF-terminated request headers and newline-separated cookies.
289static bool yt_dlp_parse_request(yyjson_val *format, yyjson_val *root, const char *stream, yt_dlp_request_options_t *request) {
290 yyjson_val *headers = yt_dlp_object_get(format, "http_headers");
291 if (!yyjson_is_obj(headers))
292 headers = yt_dlp_object_get(root, "http_headers");
293 size_t index, max, used = 0;
294 yyjson_val *key, *value;
295 if (yyjson_is_obj(headers)) {
296 yyjson_obj_foreach(headers, index, max, key, value) {
297 const char *name = yyjson_get_str(key), *text = yyjson_get_str(value);
298 if (!name || !text || strlen(name) != yyjson_get_len(key) || strlen(text) != yyjson_get_len(value) || strpbrk(name, ":\r\n") || strpbrk(text, "\r\n"))
299 return false;
300 int n = snprintf(request->headers + used, sizeof(request->headers) - used, "%s: %s\r\n", name, text);
301 if (n < 0 || (size_t)n >= sizeof(request->headers) - used)
302 return false;
303 used += (size_t)n;
304 }
305 }
306 yyjson_val *cookie_value = yt_dlp_object_get(format, "cookies");
307 if (!yyjson_is_str(cookie_value))
308 cookie_value = yt_dlp_object_get(root, "cookies");
309 const char *cookies = yyjson_get_str(cookie_value);
310 if (cookies && *cookies) {
311 const size_t cookie_length = strlen(cookies);
312 if (cookie_length != yyjson_get_len(cookie_value) || cookie_length >= sizeof(request->cookies) || strpbrk(cookies, "\r\n"))
313 return false;
314 // yt-dlp serializes scoped cookies as semicolon-separated attributes.
315 // Start a new Set-Cookie line at each cookie name, retaining its scope.
316 char copy[YT_DLP_COOKIES_CAPACITY];
317 memcpy(copy, cookies, cookie_length + sizeof(char));
318 url_parts_t parts = {0};
320 int port = url_parse(stream, &parts) == ASCIICHAT_OK ? parts.port : 0;
321 if (parts.scheme && ((!platform_strcasecmp(parts.scheme, "http") && port == 80) || (!platform_strcasecmp(parts.scheme, "https") && port == 443)))
322 port = 0;
323 url_parts_destroy(&parts);
325 char *context = copy;
326 char *token = yt_dlp_next_cookie_attribute(&context);
327 used = 0;
328 bool first = true;
329 while (token) {
330 while (isspace((unsigned char)*token))
331 ++token;
332 bool attribute = !platform_strncasecmp(token, "domain=", 7) || !platform_strncasecmp(token, "path=", 5) || !platform_strncasecmp(token, "expires=", 8) ||
333 !platform_strncasecmp(token, "version=", 8) || !platform_strcasecmp(token, "secure") || !platform_strcasecmp(token, "httponly");
334 if (!attribute && !strchr(token, '='))
335 return false;
336 // FFmpeg compares cookie domains against its Host header, including a
337 // non-default port. Preserve domain scoping when that port is present.
338 bool domain = !platform_strncasecmp(token, "domain=", 7);
339 int n = port && domain && !strchr(token + 7, ':') ? snprintf(request->cookies + used, sizeof(request->cookies) - used, "%s%s:%d", first ? "" : "; ", token, port)
340 : snprintf(request->cookies + used, sizeof(request->cookies) - used, "%s%s",
341 first ? ""
342 : attribute ? "; "
343 : "\n",
344 token);
345 if (n < 0 || (size_t)n >= sizeof(request->cookies) - used)
346 return false;
347 used += (size_t)n;
348 first = false;
349 token = yt_dlp_next_cookie_attribute(&context);
350 }
351 if (context)
352 return false;
353 if (used + 1 >= sizeof(request->cookies))
354 return false;
355 request->cookies[used++] = '\n';
356 request->cookies[used] = '\0';
357 }
358 return true;
359}
360
361asciichat_error_t yt_dlp_extract_with_options(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size,
362 yt_dlp_request_options_t *video_request, yt_dlp_request_options_t *audio_request, bool (*cancel)(void *), void *data) {
363 if (!video_request || !audio_request)
364 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing yt-dlp request options output");
365 memset(video_request, 0, sizeof(*video_request));
366 memset(audio_request, 0, sizeof(*audio_request));
367 ASCII_OTEL_SCOPE(YTDLP_EXTRACT);
368 ASCII_OTEL_ATTR_STRING("media.extractor", "yt-dlp");
369 if (output_url && output_size) {
370 output_url[0] = '\0';
371 output_url[output_size - 1] = '\0';
372 }
373 if (audio_url && audio_size) {
374 audio_url[0] = '\0';
375 audio_url[audio_size - 1] = '\0';
376 }
377 if (!url || !output_url || output_size < 256 || !audio_url || audio_size < 256) {
378 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters for yt-dlp URL extraction");
379 }
380 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
381 bool cached = yt_dlp_cache_get(url, yt_dlp_options, output_url, output_size, audio_url, audio_size, video_request, audio_request);
382 static_mutex_unlock(&g_yt_dlp_cache_lock);
383 if (cached) {
384 ascii_otel_metric(ASCII_OTEL_METRIC_YTDLP_HIT, 1);
385 ASCII_OTEL_ATTR_STRING("cache.result", *output_url ? "hit" : "negative_hit");
386 if (!*output_url) {
387 ascii_otel_metric(ASCII_OTEL_METRIC_YTDLP_NEGATIVE, 1);
389 }
390 return ASCIICHAT_OK;
391 }
392
393 ascii_otel_metric(ASCII_OTEL_METRIC_YTDLP_MISS, 1);
394 ASCII_OTEL_ATTR_STRING("cache.result", "miss");
395
396 enum { YT_DLP_MAX_ARGS = 64 };
397 const char *argv[YT_DLP_MAX_ARGS];
398 char option_storage[MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE];
399 size_t argc = 0;
400 argv[argc++] = "yt-dlp";
401 argv[argc++] = "--quiet";
402 argv[argc++] = "--no-warnings";
403 argv[argc++] = "--no-playlist";
404 argv[argc++] = "--user-agent";
405 argv[argc++] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36";
406 argv[argc++] = "-f";
407 // Only select separate streams when no combined format is available.
408 // Prefer H.264 for fallback: some FFmpeg builds cannot decode AV1.
409 argv[argc++] = "b/bv[vcodec^=avc1]+ba/bv+ba";
410 // A user-provided -f or --format overrides the default selector.
411 if (!yt_dlp_parse_options(yt_dlp_options, option_storage, sizeof(option_storage), argv, &argc, YT_DLP_MAX_ARGS - 5)) {
412 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid or too long yt-dlp options");
413 }
414 // Pass the explicit yt-dlp proxy to the independent FFmpeg requests.
415 bool skip_proxy_value = false;
416 for (size_t i = 0; i < argc; ++i) {
417 if (skip_proxy_value) {
418 skip_proxy_value = false;
419 continue;
420 }
421 const char *proxy = NULL;
422 if (!strcmp(argv[i], "--proxy") && i + 1 < argc) {
423 proxy = argv[i + 1];
424 skip_proxy_value = true;
425 } else if (!strncmp(argv[i], "--proxy=", sizeof("--proxy=") - 1)) {
426 proxy = argv[i] + sizeof("--proxy=") - 1;
427 }
428 if (proxy) {
429 const size_t proxy_length = strlen(proxy);
430 if (proxy_length >= sizeof(video_request->proxy))
431 return SET_ERRNO(ERROR_NOT_SUPPORTED, "yt-dlp proxy is too long for media requests");
432 memcpy(video_request->proxy, proxy, proxy_length + sizeof(char));
433 memcpy(audio_request->proxy, proxy, proxy_length + sizeof(char));
434 video_request->proxy_specified = audio_request->proxy_specified = true;
435 }
436 }
437 if (video_request->proxy_specified && video_request->proxy[0] && strncmp(video_request->proxy, "http://", sizeof("http://") - 1))
438 return SET_ERRNO(ERROR_NOT_SUPPORTED, "FFmpeg media requests require an HTTP proxy; the yt-dlp proxy is unsupported");
439 if (!yt_dlp_is_available_interruptible(cancel, data)) {
440 return SET_ERRNO(ERROR_YOUTUBE_EXTRACT_FAILED, "yt-dlp is not installed. Please install it with: "
441 "pip install yt-dlp (or: brew install yt-dlp on macOS)");
442 }
443 argv[argc++] = "--print";
444 argv[argc++] = "{\"base\":%(.{url,http_headers,cookies})j,\"formats\":%(requested_formats.:.{url,http_headers,cookies}|null)j}";
445 argv[argc++] = "--";
446 argv[argc++] = url;
447 argv[argc] = NULL;
448
449 // Two URLs can each occupy the full stream URL capacity, plus diagnostics.
451 int status = 0;
452 // Interactive extractors need their prompts visible on the real terminal.
453 LOG_IO_IF(!platform_isatty(STDIN_FILENO), "yt-dlp", { status = platform_execute_subprocess("yt-dlp", argv, output, sizeof(output), cancel, data); });
454 if (cancel && cancel(data)) {
456 }
457 if (status != 0 || strlen(output) == sizeof(output) - 1) {
458 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
459 yt_dlp_cache_set(url, yt_dlp_options, NULL, NULL, video_request, audio_request);
460 static_mutex_unlock(&g_yt_dlp_cache_lock);
461 log_error("yt-dlp failed to extract streams (exit %d)", status);
462 return SET_ERRNO(ERROR_YOUTUBE_EXTRACT_FAILED, "yt-dlp failed to extract streams");
463 }
464
465 size_t count = 0;
466 yyjson_doc *doc = yyjson_read(output, strlen(output), 0);
467 yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
468 yyjson_val *base = yt_dlp_object_get(root, "base");
469 yyjson_val *formats = yt_dlp_object_get(root, "formats");
470 if (!yyjson_is_arr(formats))
471 formats = NULL;
472 size_t total = yyjson_is_arr(formats) ? yyjson_arr_size(formats) : 1;
473 if (!yyjson_is_obj(base) || total > 2) {
474 if (doc)
475 yyjson_doc_free(doc);
476 return SET_ERRNO(ERROR_YOUTUBE_EXTRACT_FAILED, "yt-dlp returned invalid stream metadata");
477 }
478 // Walk the array once; indexed lookup in yyjson uses an unsigned countdown
479 // that trips integer sanitizers on non-flat format objects.
480 yyjson_arr_iter format_iter = yyjson_arr_iter_with(formats);
481 for (size_t i = 0; i < total; ++i) {
482 yyjson_val *format = formats ? yyjson_arr_iter_next(&format_iter) : base;
483 yyjson_val *stream_value = yt_dlp_object_get(format, "url");
484 const char *stream = yyjson_get_str(stream_value);
485 char *dest = i == 0 ? output_url : audio_url;
486 size_t capacity = i == 0 ? output_size : audio_size;
487 yt_dlp_request_options_t *request = i == 0 ? video_request : audio_request;
488 const size_t stream_length = stream ? strlen(stream) : 0;
489 if (stream && request->proxy_specified && request->proxy[0] && platform_strncasecmp(stream, "http://", sizeof("http://") - 1) && platform_strncasecmp(stream, "https://", sizeof("https://") - 1)) {
490 yyjson_doc_free(doc);
491 output_url[0] = audio_url[0] = '\0';
492 return SET_ERRNO(ERROR_NOT_SUPPORTED, "The selected proxy cannot be used with this media protocol");
493 }
494 if (!stream || stream_length != yyjson_get_len(stream_value) || !yt_dlp_is_stream_url(stream) || stream_length >= capacity || !yt_dlp_parse_request(format, base, stream, request)) {
495 yyjson_doc_free(doc);
496 output_url[0] = audio_url[0] = '\0';
497 return SET_ERRNO(ERROR_YOUTUBE_EXTRACT_FAILED, "yt-dlp returned invalid stream metadata");
498 }
499 memcpy(dest, stream, stream_length + sizeof(char));
500 ++count;
501 }
502 yyjson_doc_free(doc);
503 if (count == 0) {
504 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
505 yt_dlp_cache_set(url, yt_dlp_options, NULL, NULL, video_request, audio_request);
506 static_mutex_unlock(&g_yt_dlp_cache_lock);
507 log_error("yt-dlp returned no playable stream URL");
508 return SET_ERRNO(ERROR_YOUTUBE_EXTRACT_FAILED, "yt-dlp returned no playable formats");
509 }
510 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
511 yt_dlp_cache_set(url, yt_dlp_options, output_url, audio_url, video_request, audio_request);
512 static_mutex_unlock(&g_yt_dlp_cache_lock);
513 log_debug("yt-dlp extracted %zu stream URL(s)", count);
514 return ASCIICHAT_OK;
515}
516
517asciichat_error_t yt_dlp_extract_stream_urls_interruptible(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size,
518 bool (*cancel)(void *), void *data) {
519 yt_dlp_request_options_t video_request, audio_request;
520 return yt_dlp_extract_with_options(url, yt_dlp_options, output_url, output_size, audio_url, audio_size, &video_request, &audio_request, cancel, data);
521}
522
523asciichat_error_t yt_dlp_extract_stream_urls(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size) {
524 return yt_dlp_extract_stream_urls_interruptible(url, yt_dlp_options, output_url, output_size, audio_url, audio_size, NULL, NULL);
525}
526
527asciichat_error_t yt_dlp_extract_stream_url(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size) {
528 char audio_url[MEDIA_STREAM_URL_SIZE];
529 return yt_dlp_extract_stream_urls(url, yt_dlp_options, output_url, output_size, audio_url, sizeof(audio_url));
530}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_DISMISSED
Application limits and constraints.
Platform initialization and static synchronization helpers.
#define SAFE_STRNCPY(dst, src, size)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_YOUTUBE_EXTRACT_FAILED
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
#define MEDIA_URL_CACHE_SIZE
#define MEDIA_STREAM_URL_SIZE
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
asciichat_error_t yt_dlp_extract_stream_urls(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size)
For yt dlp request options, this is the live record that ties proxy_specified together....
Definition yt_dlp.c:523
bool yt_dlp_is_available(void)
Check if yt-dlp is installed and accessible.
Definition yt_dlp.c:173
#define STATIC_MUTEX_INIT
void url_parts_destroy(url_parts_t *parts)
Free parsed URL components.
Definition url.c:299
bool url_is_valid(const char *url)
URL validation using shared parsing and ICU hostname checks.
Definition url.c:156
asciichat_error_t url_parse(const char *url, url_parts_t *parts_out)
Parse HTTP(S), WebSocket, and TCP URL into components.
Definition url.c:163
⚙️ Common definitions, error codes, macros, and types shared throughout the application
Capture stdout/stderr and redirect to logging system.
#define LOG_IO(prefix, block)
Definition io.h:104
#define LOG_IO_IF(condition, prefix, block)
Capture output from a code block and log it.
Definition io.h:93
📝 Logging API with multiple log levels and terminal output control
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:159
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:77
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:71
static bool size_t index
Definition queries.h:8
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
int platform_strcasecmp(const char *s1, const char *s2)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
Static mutex structure for global mutexes requiring static initialization.
Definition common/sync.h:49
Structure containing parsed URL components.
Definition url.h:69
char * scheme
Extracted scheme: "http" or "https" (allocated).
Definition url.h:70
int port
Extracted port number (0 if not specified).
Definition url.h:74
Cached yt-dlp result for one media source and its audio/video requests.
Definition yt_dlp.c:50
char yt_dlp_options[512]
Definition yt_dlp.c:54
yt_dlp_request_options_t video_request
Definition yt_dlp.c:55
char audio_url[MEDIA_STREAM_URL_SIZE]
Definition yt_dlp.c:53
yt_dlp_request_options_t audio_request
Definition yt_dlp.c:55
bool valid
Definition yt_dlp.c:57
char url[MEDIA_URL_CACHE_SIZE]
Definition yt_dlp.c:51
char stream_url[MEDIA_STREAM_URL_SIZE]
Definition yt_dlp.c:52
time_t extracted_time
Definition yt_dlp.c:56
char headers[YT_DLP_HEADERS_CAPACITY]
Definition yt_dlp.h:20
char cookies[YT_DLP_COOKIES_CAPACITY]
Definition yt_dlp.h:21
char proxy[YT_DLP_PROXY_CAPACITY]
Definition yt_dlp.h:22
int platform_execute_subprocess(const char *executable, const char **argv, char *output_buffer, size_t output_size, bool(*cancel)(void *), void *data)
Definition system.c:35
⏱️ High-precision timing utilities using sokol_time.h and uthash
🌐 Production-Grade URL Validation and Parsing
int platform_isatty(int fd)
Definition util.c:70
#define MEDIA_YT_DLP_FULL_OUTPUT_BUFFER_SIZE
Definition yt_dlp.c:4
#define MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE
Definition yt_dlp.c:3
asciichat_error_t yt_dlp_extract_stream_url(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size)
Definition yt_dlp.c:527
asciichat_error_t yt_dlp_extract_with_options(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size, yt_dlp_request_options_t *video_request, yt_dlp_request_options_t *audio_request, bool(*cancel)(void *), void *data)
Definition yt_dlp.c:361
asciichat_error_t yt_dlp_extract_stream_urls_interruptible(const char *url, const char *yt_dlp_options, char *output_url, size_t output_size, char *audio_url, size_t audio_size, bool(*cancel)(void *), void *data)
Definition yt_dlp.c:517
yt-dlp stream URL extraction
@ YT_DLP_COOKIES_CAPACITY
Definition yt_dlp.h:16