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 File Reference

yt-dlp stream URL extraction implementation More...

Go to the source code of this file.

Data Structures

struct  yt_dlp_cache_entry_t
 Cached yt-dlp result for one media source and its audio/video requests. More...

Macros

#define MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE   512
#define MEDIA_YT_DLP_FULL_OUTPUT_BUFFER_SIZE   16384

Functions

bool yt_dlp_is_available (void)
 Check if yt-dlp is installed and accessible.
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)
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)
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. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record.
asciichat_error_t yt_dlp_extract_stream_url (const char *url, const char *yt_dlp_options, char *output_url, size_t output_size)

Detailed Description

yt-dlp stream URL extraction implementation

Definition in file yt_dlp.c.

Macro Definition Documentation

◆ MEDIA_YT_DLP_FULL_OUTPUT_BUFFER_SIZE

#define MEDIA_YT_DLP_FULL_OUTPUT_BUFFER_SIZE   16384

Definition at line 4 of file yt_dlp.c.

Referenced by yt_dlp_extract_with_options().

◆ MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE

#define MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE   512

Definition at line 3 of file yt_dlp.c.

Referenced by yt_dlp_extract_with_options().

Function Documentation

◆ yt_dlp_extract_stream_url()

asciichat_error_t yt_dlp_extract_stream_url ( const char * url,
const char * yt_dlp_options,
char * output_url,
size_t output_size )

Extract the first playable stream URL. Prefer the two-output API for audio.

Definition at line 527 of file yt_dlp.c.

527 {
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}
#define MEDIA_STREAM_URL_SIZE
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

References MEDIA_STREAM_URL_SIZE, and yt_dlp_extract_stream_urls().

◆ yt_dlp_extract_stream_urls_interruptible()

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 at line 517 of file yt_dlp.c.

518 {
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}
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

References yt_dlp_extract_with_options().

Referenced by yt_dlp_extract_stream_urls().

◆ yt_dlp_extract_with_options()

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 at line 361 of file yt_dlp.c.

362 {
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}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_YOUTUBE_EXTRACT_FAILED
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
#define LOG_IO_IF(condition, prefix, block)
Capture output from a code block and log it.
Definition io.h:93
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
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
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
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

References ASCII_OTEL_ATTR_STRING, ascii_otel_metric(), ASCII_OTEL_SCOPE, ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, ERROR_YOUTUBE_EXTRACT_FAILED, log_debug, log_error, LOG_IO_IF, MEDIA_STREAM_URL_SIZE, MEDIA_YT_DLP_FULL_OUTPUT_BUFFER_SIZE, MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE, platform_execute_subprocess(), platform_isatty(), platform_strncasecmp(), yt_dlp_request_options_t::proxy, yt_dlp_request_options_t::proxy_specified, and SET_ERRNO.

Referenced by yt_dlp_extract_stream_urls_interruptible().