Compare commits
No commits in common. "d8b7b657eaf6884310be9d27f94c8a1dd197bc55" and "9e10d6e7d8c6845a51b9d15a6cb1ae8c8d35d0c6" have entirely different histories.
d8b7b657ea
...
9e10d6e7d8
@ -2,56 +2,43 @@
|
||||
(ice-9 rdelim)
|
||||
(ice-9 regex)
|
||||
(ice-9 textual-ports)
|
||||
(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 defaults `((preferred-video-codes . ("234" "311"))
|
||||
(preferred-audio-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 (null? test-url)
|
||||
(read-line))
|
||||
; test-url))
|
||||
(if (not test-url)
|
||||
(read-line read-url)
|
||||
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)
|
||||
(define (lines-to-values port)
|
||||
|
||||
;; To iterate through format strings, to control program resource footprint
|
||||
(define (iterate line audio-code-list video-code-list)
|
||||
(define (iterate line out-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
|
||||
(values (reverse out-list))
|
||||
(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))
|
||||
@ -60,65 +47,19 @@
|
||||
(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
|
||||
(iterate this-line (acons video-code (string-append
|
||||
video-resolution ","
|
||||
video-framerate ","
|
||||
video-bitrate) video-code-list))))
|
||||
video-bitrate) out-list))))
|
||||
(audio-match
|
||||
(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)))
|
||||
(iterate this-line (acons audio-code audio-quality out-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))))
|
||||
|
||||
(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 `())))))
|
||||
|
||||
|
||||
(iterate this-line out-list)))))))
|
||||
(iterate get-line '()))
|
||||
|
||||
;; 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
|
||||
(display (lines-to-values yt-dlp-port))
|
||||
(close-port yt-dlp-port)
|
||||
|
Loading…
Reference in New Issue
Block a user