Add comments
This commit is contained in:
		
							parent
							
								
									fd970ec833
								
							
						
					
					
						commit
						d8b7b657ea
					
				@ -5,41 +5,53 @@
 | 
				
			|||||||
	(srfi srfi-1)
 | 
						(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-audio-codes . ("234" "311"))
 | 
					(define defaults `((preferred-audio-codes . ("234" "311"))
 | 
				
			||||||
 		  (preferred-video-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 (not test-url)
 | 
					;  (if (null? test-url)
 | 
				
			||||||
    (read-line read-url)
 | 
					    (read-line))
 | 
				
			||||||
    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 (fetch-formats port)
 | 
					(define (fetch-formats port)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ;; To iterate through format strings, to control program resource footprint
 | 
				
			||||||
  (define (iterate line audio-code-list video-code-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 (append audio-code-list video-code-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))
 | 
				
			||||||
@ -48,6 +60,7 @@
 | 
				
			|||||||
                    (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 audio-code-list (acons video-code (string-append
 | 
					                (iterate this-line audio-code-list (acons video-code (string-append
 | 
				
			||||||
						       video-resolution ","
 | 
											       video-resolution ","
 | 
				
			||||||
						       video-framerate ","
 | 
											       video-framerate ","
 | 
				
			||||||
@ -56,11 +69,15 @@
 | 
				
			|||||||
              (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 audio-code-list) video-code-list)))
 | 
					                (iterate this-line (acons audio-code audio-quality audio-code-list) video-code-list)))
 | 
				
			||||||
	    (else
 | 
						    (else
 | 
				
			||||||
 | 
						      ;; No match
 | 
				
			||||||
	      (iterate this-line audio-code-list video-code-list)))))))
 | 
						      (iterate this-line audio-code-list video-code-list)))))))
 | 
				
			||||||
 | 
					    ;; First call of iterate
 | 
				
			||||||
    (iterate get-line '() '()))
 | 
					    (iterate get-line '() '()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;; Return a list of formats that match preferred codes
 | 
				
			||||||
(define (select-formats formats-list)
 | 
					(define (select-formats formats-list)
 | 
				
			||||||
  (let* ((preferred-audio-codes (cdr (assoc `preferred-audio-codes defaults)))
 | 
					  (let* ((preferred-audio-codes (cdr (assoc `preferred-audio-codes defaults)))
 | 
				
			||||||
	(preferred-video-codes (cdr (assoc `preferred-video-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))
 | 
					    (format #t "CAAR FORMATS LIST: ~a\n" (caar formats-list))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (define (preferred-video-formats this-alist return-list)
 | 
					    (define (preferred-video-formats this-alist return-list)
 | 
				
			||||||
 | 
					      ;; Check for empty list
 | 
				
			||||||
      (if (not (null? this-alist))
 | 
					      (if (not (null? this-alist))
 | 
				
			||||||
	(if (member (caar this-alist) preferred-video-codes)
 | 
						(if (member (caar this-alist) preferred-video-codes)
 | 
				
			||||||
 | 
						  ;; Preferred code found
 | 
				
			||||||
          (preferred-video-formats (cdr this-alist) (cons (caar this-alist) return-list))
 | 
					          (preferred-video-formats (cdr this-alist) (cons (caar this-alist) return-list))
 | 
				
			||||||
 | 
						  ;; Not found
 | 
				
			||||||
	  (preferred-video-formats (cdr this-alist) return-list))
 | 
						  (preferred-video-formats (cdr this-alist) return-list))
 | 
				
			||||||
 | 
						;; First in, last out
 | 
				
			||||||
	(reverse return-list)))
 | 
						(reverse return-list)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (define (preferred-audio-formats this-alist return-list)
 | 
					    (define (preferred-audio-formats this-alist return-list)
 | 
				
			||||||
 | 
					      ;; Check for empty list
 | 
				
			||||||
      (if (not (null? this-alist))
 | 
					      (if (not (null? this-alist))
 | 
				
			||||||
	(if (member (caar this-alist) preferred-audio-codes)
 | 
						(if (member (caar this-alist) preferred-audio-codes)
 | 
				
			||||||
 | 
						  ;; Preferred code found
 | 
				
			||||||
          (preferred-audio-formats (cdr this-alist) (cons (caar this-alist) return-list))
 | 
					          (preferred-audio-formats (cdr this-alist) (cons (caar this-alist) return-list))
 | 
				
			||||||
 | 
						  ;; Not found
 | 
				
			||||||
	  (preferred-audio-formats (cdr this-alist) return-list))
 | 
						  (preferred-audio-formats (cdr this-alist) return-list))
 | 
				
			||||||
 | 
						;; First in, last out
 | 
				
			||||||
	(reverse return-list)))
 | 
						(reverse return-list)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ;; Return lists as a list
 | 
				
			||||||
    (values (cons (preferred-audio-formats formats-list `()) (preferred-video-formats formats-list `())))))
 | 
					    (values (cons (preferred-audio-formats formats-list `()) (preferred-video-formats formats-list `())))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -88,8 +114,11 @@
 | 
				
			|||||||
;; Main
 | 
					;; Main
 | 
				
			||||||
(let* ((formats (fetch-formats yt-dlp-port))
 | 
					(let* ((formats (fetch-formats yt-dlp-port))
 | 
				
			||||||
      (codes (select-formats formats))
 | 
					      (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)))
 | 
					      (mpv-commandline (string-append "mpv --ytdl-format=" (caar codes) "+" (cadr codes) " " read-url)))
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
 | 
					  ;; Shell call
 | 
				
			||||||
  (system mpv-commandline))
 | 
					  (system mpv-commandline))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					;; All done with the youtube input port
 | 
				
			||||||
(close-port yt-dlp-port)
 | 
					(close-port yt-dlp-port)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user