Compare commits

..

No commits in common. "d8b7b657eaf6884310be9d27f94c8a1dd197bc55" and "9e10d6e7d8c6845a51b9d15a6cb1ae8c8d35d0c6" have entirely different histories.

View File

@ -2,56 +2,43 @@
(ice-9 rdelim) (ice-9 rdelim)
(ice-9 regex) (ice-9 regex)
(ice-9 textual-ports) (ice-9 textual-ports)
(srfi srfi-1)
(os process)) (os process))
;; backtrace is set for debugging purposes
(debug-enable 'backtrace) (debug-enable 'backtrace)
;; 720p, high quality audio as default youtube-dl format codes (define defaults `((preferred-video-codes . ("234" "311"))
(define defaults `((preferred-audio-codes . ("234" "311")) (preferred-audio-codes . ("232"))))
(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 (define read-url
; (if (null? test-url) (if (not test-url)
(read-line)) (read-line read-url)
; test-url)) test-url))
;; Match a given line with regex for audio and video format strings
(define (match-audio-format line) (define (match-audio-format line)
(string-match "([0-9]{3,}-?).*audio only.*\\[en\\] Default\\, (high|low).*" line)) (string-match "([0-9]{3,}-?).*audio only.*\\[en\\] Default\\, (high|low).*" line))
(define (match-video-format 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)) (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))) (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" (format #t "URL: ~a\nPREFERRED-VIDEO-CODES: ~a\nPREFERRED-AUDIO-CODES: ~a\n\n"
read-url read-url
(cdr (assoc `preferred-video-codes defaults)) (cdr (assoc `preferred-video-codes defaults))
(cdr (assoc `preferred-audio-codes defaults))) (cdr (assoc `preferred-audio-codes defaults)))
;; A function to fetch formats from an input port (define (lines-to-values port)
(define (fetch-formats port)
;; To iterate through format strings, to control program resource footprint (define (iterate line out-list)
(define (iterate line audio-code-list video-code-list)
(let ((this-line (get-line port))) (let ((this-line (get-line port)))
;; End of file from input - are we there yet?
(if (eof-object? this-line) (if (eof-object? this-line)
;; Yes (values (reverse out-list))
(values (reverse (append audio-code-list video-code-list)))
;; No
(let ((video-match (match-video-format this-line)) (let ((video-match (match-video-format this-line))
(audio-match (match-audio-format this-line))) (audio-match (match-audio-format this-line)))
;; Switch on video or audio match
(cond (cond
(video-match (video-match
(let ((video-code (match:substring video-match 1)) (let ((video-code (match:substring video-match 1))
@ -60,65 +47,19 @@
(video-bitrate (match:substring video-match 4)) (video-bitrate (match:substring video-match 4))
(video-moreinfo (match:substring video-match 5))) (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 (acons video-code (string-append
(iterate this-line audio-code-list (acons video-code (string-append
video-resolution "," video-resolution ","
video-framerate "," video-framerate ","
video-bitrate) video-code-list)))) video-bitrate) out-list))))
(audio-match (audio-match
(let ((audio-code (match:substring audio-match 1)) (let ((audio-code (match:substring audio-match 1))
(audio-quality (match:substring audio-match 2))) (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 out-list))))
(iterate this-line (acons audio-code audio-quality audio-code-list) video-code-list)))
(else (else
;; No match (iterate this-line out-list)))))))
(iterate this-line audio-code-list video-code-list))))))) (iterate get-line '()))
;; 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))))
(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 `())))))
;; Main ;; Main
(let* ((formats (fetch-formats yt-dlp-port)) (display (lines-to-values 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) (close-port yt-dlp-port)