ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
yt_dlp.h File Reference

yt-dlp stream URL extraction More...

Go to the source code of this file.

Data Structures

struct  yt_dlp_request_options_t

Enumerations

enum  { YT_DLP_HEADERS_CAPACITY = 8192 , YT_DLP_COOKIES_CAPACITY = 8192 , YT_DLP_PROXY_CAPACITY = 2048 }

Functions

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_options, yt_dlp_request_options_t *audio_options, 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_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_url (const char *url, const char *yt_dlp_options, char *output_url, size_t output_size)
bool yt_dlp_is_available (void)
 Check if yt-dlp is installed and accessible.

Detailed Description

yt-dlp stream URL extraction

Encapsulates yt-dlp subprocess logic for extracting playable stream URLs from URLs that yt-dlp supports (YouTube, Twitch, etc.)

Definition in file yt_dlp.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
YT_DLP_HEADERS_CAPACITY 
YT_DLP_COOKIES_CAPACITY 
YT_DLP_PROXY_CAPACITY 

Definition at line 16 of file yt_dlp.h.

16 {
20};
@ YT_DLP_HEADERS_CAPACITY
Definition yt_dlp.h:17
@ YT_DLP_COOKIES_CAPACITY
Definition yt_dlp.h:18
@ YT_DLP_PROXY_CAPACITY
Definition yt_dlp.h:19

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 621 of file yt_dlp.c.

624 {
625 char audio_url[MEDIA_STREAM_URL_SIZE];
626 return yt_dlp_extract_stream_urls(url, yt_dlp_options, output_url,
627 output_size, audio_url, sizeof(audio_url));
628}
#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:613

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 602 of file yt_dlp.c.

605 {
606 yt_dlp_request_options_t video_request, audio_request;
608 url, yt_dlp_options, output_url, output_size, audio_url, audio_size,
609 &video_request, &audio_request, cancel, data);
610}
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:410

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_options,
yt_dlp_request_options_t * audio_options,
bool(* cancel )(void *),
void * data )

Definition at line 410 of file yt_dlp.c.

415 {
416 if (!video_request || !audio_request)
418 "Missing yt-dlp request options output");
419 memset(video_request, 0, sizeof(*video_request));
420 memset(audio_request, 0, sizeof(*audio_request));
421 ASCII_OTEL_SCOPE(YTDLP_EXTRACT);
422 ASCII_OTEL_ATTR_STRING("media.extractor", "yt-dlp");
423 if (output_url && output_size) {
424 output_url[0] = '\0';
425 output_url[output_size - 1] = '\0';
426 }
427 if (audio_url && audio_size) {
428 audio_url[0] = '\0';
429 audio_url[audio_size - 1] = '\0';
430 }
431 if (!url || !output_url || output_size < 256 || !audio_url ||
432 audio_size < 256) {
434 "Invalid parameters for yt-dlp URL extraction");
435 }
436 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
437 bool cached =
438 yt_dlp_cache_get(url, yt_dlp_options, output_url, output_size, audio_url,
439 audio_size, video_request, audio_request);
440 static_mutex_unlock(&g_yt_dlp_cache_lock);
441 if (cached) {
442 ascii_otel_metric(ASCII_OTEL_METRIC_YTDLP_HIT, 1);
443 ASCII_OTEL_ATTR_STRING("cache.result",
444 *output_url ? "hit" : "negative_hit");
445 if (!*output_url) {
446 ascii_otel_metric(ASCII_OTEL_METRIC_YTDLP_NEGATIVE, 1);
448 }
449 return ASCIICHAT_OK;
450 }
451
452 ascii_otel_metric(ASCII_OTEL_METRIC_YTDLP_MISS, 1);
453 ASCII_OTEL_ATTR_STRING("cache.result", "miss");
454
455 enum { YT_DLP_MAX_ARGS = 64 };
456 const char *argv[YT_DLP_MAX_ARGS];
457 char option_storage[MEDIA_YT_DLP_YT_DLP_OPTIONS_BUFFER_SIZE];
458 size_t argc = 0;
459 argv[argc++] = "yt-dlp";
460 argv[argc++] = "--quiet";
461 argv[argc++] = "--no-warnings";
462 argv[argc++] = "--no-playlist";
463 argv[argc++] = "--user-agent";
464 argv[argc++] =
465 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36";
466 argv[argc++] = "-f";
467 // Only select separate streams when no combined format is available.
468 // Prefer H.264 for fallback: some FFmpeg builds cannot decode AV1.
469 argv[argc++] = "b/bv[vcodec^=avc1]+ba/bv+ba";
470 // A user-provided -f or --format overrides the default selector.
471 if (!yt_dlp_parse_options(yt_dlp_options, option_storage,
472 sizeof(option_storage), argv, &argc,
473 YT_DLP_MAX_ARGS - 5)) {
474 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid or too long yt-dlp options");
475 }
476 // Pass the explicit yt-dlp proxy to the independent FFmpeg requests.
477 bool skip_proxy_value = false;
478 for (size_t i = 0; i < argc; ++i) {
479 if (skip_proxy_value) {
480 skip_proxy_value = false;
481 continue;
482 }
483 const char *proxy = NULL;
484 if (!strcmp(argv[i], "--proxy") && i + 1 < argc) {
485 proxy = argv[i + 1];
486 skip_proxy_value = true;
487 } else if (!strncmp(argv[i], "--proxy=", sizeof("--proxy=") - 1)) {
488 proxy = argv[i] + sizeof("--proxy=") - 1;
489 }
490 if (proxy) {
491 const size_t proxy_length = strlen(proxy);
492 if (proxy_length >= sizeof(video_request->proxy))
494 "yt-dlp proxy is too long for media requests");
495 memcpy(video_request->proxy, proxy, proxy_length + sizeof(char));
496 memcpy(audio_request->proxy, proxy, proxy_length + sizeof(char));
497 video_request->proxy_specified = audio_request->proxy_specified = true;
498 }
499 }
500 if (video_request->proxy_specified && video_request->proxy[0] &&
501 strncmp(video_request->proxy, "http://", sizeof("http://") - 1))
503 "FFmpeg media requests require an HTTP proxy; the yt-dlp "
504 "proxy is unsupported");
505 if (!yt_dlp_is_available_interruptible(cancel, data)) {
507 "yt-dlp is not installed. Please install it with: "
508 "pip install yt-dlp (or: brew install yt-dlp on macOS)");
509 }
510 argv[argc++] = "--print";
511 argv[argc++] = "{\"base\":%(.{url,http_headers,cookies})j,\"formats\":%("
512 "requested_formats.:.{url,http_headers,cookies}|null)j}";
513 argv[argc++] = "--";
514 argv[argc++] = url;
515 argv[argc] = NULL;
516
517 // Two URLs can each occupy the full stream URL capacity, plus diagnostics.
519 0};
520 int status = 0;
521 // Interactive extractors need their prompts visible on the real terminal.
522 LOG_IO_IF(!platform_isatty(STDIN_FILENO), "yt-dlp", {
523 status = platform_execute_subprocess("yt-dlp", argv, output, sizeof(output),
524 cancel, data);
525 });
526 if (cancel && cancel(data)) {
528 }
529 if (status != 0 || strlen(output) == sizeof(output) - 1) {
530 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
531 yt_dlp_cache_set(url, yt_dlp_options, NULL, NULL, video_request,
532 audio_request);
533 static_mutex_unlock(&g_yt_dlp_cache_lock);
534 log_error("yt-dlp failed to extract streams (exit %d)", status);
536 "yt-dlp failed to extract streams");
537 }
538
539 size_t count = 0;
540 yyjson_doc *doc = yyjson_read(output, strlen(output), 0);
541 yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
542 yyjson_val *base = yt_dlp_object_get(root, "base");
543 yyjson_val *formats = yt_dlp_object_get(root, "formats");
544 if (!yyjson_is_arr(formats))
545 formats = NULL;
546 size_t total = yyjson_is_arr(formats) ? yyjson_arr_size(formats) : 1;
547 if (!yyjson_is_obj(base) || total > 2) {
548 if (doc)
549 yyjson_doc_free(doc);
551 "yt-dlp returned invalid stream metadata");
552 }
553 // Walk the array once; indexed lookup in yyjson uses an unsigned countdown
554 // that trips integer sanitizers on non-flat format objects.
555 yyjson_arr_iter format_iter = yyjson_arr_iter_with(formats);
556 for (size_t i = 0; i < total; ++i) {
557 yyjson_val *format = formats ? yyjson_arr_iter_next(&format_iter) : base;
558 yyjson_val *stream_value = yt_dlp_object_get(format, "url");
559 const char *stream = yyjson_get_str(stream_value);
560 char *dest = i == 0 ? output_url : audio_url;
561 size_t capacity = i == 0 ? output_size : audio_size;
562 yt_dlp_request_options_t *request = i == 0 ? video_request : audio_request;
563 const size_t stream_length = stream ? strlen(stream) : 0;
564 if (stream && request->proxy_specified && request->proxy[0] &&
565 platform_strncasecmp(stream, "http://", sizeof("http://") - 1) &&
566 platform_strncasecmp(stream, "https://", sizeof("https://") - 1)) {
567 yyjson_doc_free(doc);
568 output_url[0] = audio_url[0] = '\0';
569 return SET_ERRNO(
571 "The selected proxy cannot be used with this media protocol");
572 }
573 if (!stream || stream_length != yyjson_get_len(stream_value) ||
574 !yt_dlp_is_stream_url(stream) || stream_length >= capacity ||
575 !yt_dlp_parse_request(format, base, stream, request)) {
576 yyjson_doc_free(doc);
577 output_url[0] = audio_url[0] = '\0';
579 "yt-dlp returned invalid stream metadata");
580 }
581 memcpy(dest, stream, stream_length + sizeof(char));
582 ++count;
583 }
584 yyjson_doc_free(doc);
585 if (count == 0) {
586 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
587 yt_dlp_cache_set(url, yt_dlp_options, NULL, NULL, video_request,
588 audio_request);
589 static_mutex_unlock(&g_yt_dlp_cache_lock);
590 log_error("yt-dlp returned no playable stream URL");
592 "yt-dlp returned no playable formats");
593 }
594 static_mutex_lock_named(&g_yt_dlp_cache_lock, "yt_dlp_cache");
595 yt_dlp_cache_set(url, yt_dlp_options, output_url, audio_url, video_request,
596 audio_request);
597 static_mutex_unlock(&g_yt_dlp_cache_lock);
598 log_debug("yt-dlp extracted %zu stream URL(s)", count);
599 return ASCIICHAT_OK;
600}
#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:74
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define LOG_IO_IF(condition, prefix, block)
Capture output from a code block and log it.
Definition io.h:95
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:175
#define ASCII_OTEL_SCOPE(id)
Definition otel.h:102
#define ASCII_OTEL_ATTR_STRING(key, value)
Definition otel.h:92
int platform_strncasecmp(const char *s1, const char *s2, size_t n)
char proxy[YT_DLP_PROXY_CAPACITY]
Definition yt_dlp.h:26
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().