diff --git a/mpv-yt-helper.scm b/mpv-yt-helper.scm index 1e57ef6..2732d3c 100644 --- a/mpv-yt-helper.scm +++ b/mpv-yt-helper.scm @@ -5,41 +5,53 @@ (srfi srfi-1) (os process)) +;; backtrace is set for debugging purposes (debug-enable 'backtrace) +;; 720p, high quality audio as default youtube-dl format codes (define defaults `((preferred-audio-codes . ("234" "311")) (preferred-video-codes . ("232")))) -(define test-url "https://www.youtube.com/embed/1xVkRh7mEe0") +;;(define test-url "https://www.youtube.com/embed/1xVkRh7mEe0") +;; Need a url (define read-url - (if (not test-url) - (read-line read-url) - test-url)) +; (if (null? test-url) + (read-line)) +; test-url)) +;; Match a given line with regex for audio and video format strings (define (match-audio-format line) (string-match "([0-9]{3,}-?).*audio only.*\\[en\\] Default\\, (high|low).*" line)) (define (match-video-format line) (string-match "([0-9]{3,}).* ([0-9]{3,}x[0-9]{3,}).*([0-9]{2,}) \\|.* ([0-9]{1,}k).*(video only).*" line)) +;; http request to youtube (define yt-dlp-port (open-input-pipe (string-append "yt-dlp --no-warnings --list-formats " read-url))) +;; Printing values while building in progress (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))) +;; A function to fetch formats from an input port (define (fetch-formats port) + ;; To iterate through format strings, to control program resource footprint (define (iterate line audio-code-list video-code-list) (let ((this-line (get-line port))) + ;; End of file from input - are we there yet? (if (eof-object? this-line) + ;; Yes (values (reverse (append audio-code-list video-code-list))) + ;; No (let ((video-match (match-video-format this-line)) (audio-match (match-audio-format this-line))) + ;; Switch on video or audio match (cond (video-match (let ((video-code (match:substring video-match 1)) @@ -48,6 +60,7 @@ (video-bitrate (match:substring video-match 4)) (video-moreinfo (match:substring video-match 5))) + ;; Call iterate with the current audio list and the conjunction of this line with the current video list (iterate this-line audio-code-list (acons video-code (string-append video-resolution "," video-framerate "," @@ -56,11 +69,15 @@ (let ((audio-code (match:substring audio-match 1)) (audio-quality (match:substring audio-match 2))) + ;; Call iterate with the conjunction of this line with the current audio list and the current video list (iterate this-line (acons audio-code audio-quality audio-code-list) video-code-list))) (else + ;; No match (iterate this-line audio-code-list video-code-list))))))) + ;; First call of iterate (iterate get-line '() '())) +;; Return a list of formats that match preferred codes (define (select-formats formats-list) (let* ((preferred-audio-codes (cdr (assoc `preferred-audio-codes defaults))) (preferred-video-codes (cdr (assoc `preferred-video-codes defaults)))) @@ -68,19 +85,28 @@ (format #t "CAAR FORMATS LIST: ~a\n" (caar formats-list)) (define (preferred-video-formats this-alist return-list) + ;; Check for empty list (if (not (null? this-alist)) (if (member (caar this-alist) preferred-video-codes) + ;; Preferred code found (preferred-video-formats (cdr this-alist) (cons (caar this-alist) return-list)) + ;; Not found (preferred-video-formats (cdr this-alist) return-list)) + ;; First in, last out (reverse return-list))) (define (preferred-audio-formats this-alist return-list) + ;; Check for empty list (if (not (null? this-alist)) (if (member (caar this-alist) preferred-audio-codes) + ;; Preferred code found (preferred-audio-formats (cdr this-alist) (cons (caar this-alist) return-list)) + ;; Not found (preferred-audio-formats (cdr this-alist) return-list)) + ;; First in, last out (reverse return-list))) + ;; Return lists as a list (values (cons (preferred-audio-formats formats-list `()) (preferred-video-formats formats-list `()))))) @@ -88,8 +114,11 @@ ;; Main (let* ((formats (fetch-formats yt-dlp-port)) (codes (select-formats formats)) + ;; Set the call to mpv on the command line, grabbing the first audio code and the first video code. (mpv-commandline (string-append "mpv --ytdl-format=" (caar codes) "+" (cadr codes) " " read-url))) + ;; Shell call (system mpv-commandline)) +;; All done with the youtube input port (close-port yt-dlp-port)