Second draft

This commit is contained in:
M00T 2025-08-02 03:46:23 -04:00
parent fc6fba70e7
commit 1bd6d12276

View File

@ -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)