From 1bd6d12276041266f3c5afd4fa3f998dcf3e7553 Mon Sep 17 00:00:00 2001 From: M00T Date: Sat, 2 Aug 2025 03:46:23 -0400 Subject: [PATCH] Second draft --- mpv-yt-helper.scm | 100 ++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/mpv-yt-helper.scm b/mpv-yt-helper.scm index b888561..cf2ea92 100644 --- a/mpv-yt-helper.scm +++ b/mpv-yt-helper.scm @@ -4,55 +4,59 @@ (ice-9 textual-ports) (os process)) -;;; (display "Define void port for stderr") -;;; (newline) -;;; (define void (%make-void-port "w")) -;;; -;;; (display "Store original error port") -;;; (newline) -;;; (define original-error-port (current-error-port)) -;;; -;;; (display "Redirect stderr to /dev/null") -;;; (set-current-error-port void) -;;; (newline) +(debug-enable 'backtrace) -(define (parse-format-line line) - (let* ((video-match (string-match "([0-9]{3,}).*([0-9]{3,}x[0-9]{3,}).*(video only).*" line)) - (audio-match (string-match "([0-9]{3,}-?).*(audio only).*" line))) - - (cond - (video-match - (let ((video-code (match:substring video-match 1)) - (video-resolution (match:substring video-match 2)) - (video-moreinfo (match:substring video-match 3))) - video-code)) +(define defaults `((preferred-video-codes . ("234" "311")) + (preferred-audio-codes . ("232")))) - (audio-match - (let ((audio-code (match:substring audio-match 1)) - (audio-moreinfo (match:substring audio-match 2))) - (if (string=? "234" audio-code) - audio-code)))))) +(define test-url "https://www.youtube.com/embed/1xVkRh7mEe0") + +(define read-url + (if (not test-url) + (read-line read-url) + test-url)) + +(define (match-audio-format line) + (string-match "([0-9]{3,}-?).*(audio only).*" line)) + +(define (match-video-format line) + (string-match "([0-9]{3,}).*([0-9]{3,}x[0-9]{3,}).*(video only).*" line)) + +(define yt-dlp-port (open-input-pipe (string-append "yt-dlp --no-warnings --list-formats " read-url))) + +(format #t "URL: ~a\nPREFERRED-VIDEO-CODES: ~a\nPREFERRED-AUDIO-CODES: ~a\n\n" + read-url + (cdr (assoc `preferred-video-codes defaults)) + (cdr (assoc `preferred-audio-codes defaults))) + +(define (fetch-formats port) + (define* (codes #:optional new-codes) + (if (procedure? codes) + "PROCEDURE" + (format #t "NOT PROCEDURE\nNEW-CODES: ~a\n" new-codes))) + + (let* ((this-line (get-line port))) + + (if (not (eof-object? this-line)) + (let ((video-match (match-video-format this-line)) + (audio-match (match-audio-format this-line))) + + (cond + (video-match + (let ((video-code (match:substring video-match 1)) + (video-resolution (match:substring video-match 2)) + (video-moreinfo (match:substring video-match 3))) + (format #t "~a\n" video-code))) + (audio-match + (let ((audio-code (match:substring audio-match 1)) + (audio-moreinfo (match:substring audio-match 2))) + (format #t "~a\n" audio-code)))) + (fetch-formats port)) + (codes)))) + +;;(define (format-codes ytdl-formats) -(define fetch-format-lines - (let* ((url "https://www.youtube.com/watch?v=m5RuTLhUPU4") - (yt-dlp-port (open-input-pipe (string-append "yt-dlp --no-warnings --list-formats " url)))) - - (let loop ((this-line (get-line yt-dlp-port))) - - (if (not (eof-object? this-line)) - (begin - (if (parse-format-line this-line) - (format #t "CODE: ~a\n" (parse-format-line this-line))) - (loop (get-line yt-dlp-port))) - (format #t "EOF\n"))) - - (close-pipe yt-dlp-port))) - -(define defaults - `((preferred-video-codes . ("234" "311")) - (preferred-audio-codes . ("232")))) - -;; Not working, pseudoscheme -(define (call-mpv video-code audio-code) - (system (string-append "mpv --ytdl-format=" audio-code "+" video-code url))) +;; Main +(format #t "~a\n" (fetch-formats yt-dlp-port)) +(close-port yt-dlp-port)