Fix `youtube-dl--pointed-item` return `item` is nil issue.
[youtube-dl.el.git] / youtube-dl.el
blob7e5abd2d89e0d6852f0920c14713eb8c96046462
1 ;;; youtube-dl.el --- manages a youtube-dl queue -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2021 stardiviner <numbchild@gmail.com>
5 ;; Original Author: Christopher Wellons <wellons@nullprogram.com>
6 ;; URL: https://github.com/skeeto/youtube-dl-emacs
7 ;; Version: 1.0
8 ;; Package-Requires: ((emacs "27.1") (transient "0.4.0"))
10 ;; This file is not part of GNU Emacs.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, you can either send email to this
24 ;; program's maintainer or write to: The Free Software Foundation,
25 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This package manages a video download queue for the youtube-dl
30 ;; command line program, which serves as its back end. It manages a
31 ;; single youtube-dl subprocess to download one video at a time. New
32 ;; videos can be queued at any time.
34 ;; The `youtube-dl' command queues a URL for download. Failures are
35 ;; retried up to `youtube-dl-max-failures'. Items can be paused or set
36 ;; to be downloaded at a slower download rate (`youtube-dl-slow-rate-limit').
38 ;; The `youtube-dl-download-playlist' command queues an entire playlist, just
39 ;; as if you had individually queued each video on the playlist.
41 ;; The `youtube-dl-list' command displays a list of all active video
42 ;; downloads. From this list, items under point can be canceled (d),
43 ;; paused (p), slowed (s), and have its priority adjusted ([ and ]).
45 ;;; Code:
47 (require 'json)
48 (require 'cl-lib)
49 (require 'hl-line)
50 (require 'notifications)
51 (require 'youtube-dl-transient)
53 (defgroup youtube-dl ()
54 "Download queue for the youtube-dl command line program."
55 :group 'external)
57 (defcustom youtube-dl-download-directory "~/Downloads/"
58 "Directory in which to run youtube-dl."
59 :group 'youtube-dl
60 :type 'directory)
62 (defcustom youtube-dl-program
63 ;; "yt-dlp" is a fork of "youtube-dl".
64 (let ((program (cond
65 ((executable-find "yt-dlp") "yt-dlp")
66 ((executable-find "youtube-dl") "youtube-dl"))))
67 (if (or (file-exists-p program) (file-regular-p program))
68 (file-name-base program)
69 program))
70 "The name of the program invoked for downloading YouTube videos.
71 NOTE: It's specified by program name instead of program path."
72 :type 'string
73 :safe #'stringp
74 :group 'youtube-dl)
76 (defcustom youtube-dl-extra-arguments
77 '("--newline" "--no-warnings")
78 "Arguments to be send to youtube-dl.
79 Instead of --limit-rate use custom option `youtube-dl-slow-rate-limit'."
80 :type '(repeat string)
81 :safe #'listp
82 :group 'youtube-dl)
84 (defcustom youtube-dl-omit-mtime t
85 "Whether to omit timestamp from the `Last-modified' header for downloaded files."
86 :type 'boolean
87 :safe #'booleanp
88 :group 'youtube-dl)
90 (defcustom youtube-dl-restrict-filenames t
91 "Whether to restrict downloaded filenames to only ASCII characters."
92 :type 'boolean
93 :safe #'booleanp
94 :group 'youtube-dl)
96 (defcustom youtube-dl-proxy ""
97 "Specify the proxy for youtube-dl command.
98 For example:
100 127.0.0.1:8118
101 socks5://127.0.0.1:1086"
102 :type 'string
103 :safe #'stringp
104 :group 'youtube-dl)
106 (defcustom youtube-dl-proxy-url-list '()
107 "A list of URL domains which should use proxy for youtube-dl."
108 :type 'list
109 :safe #'listp
110 :group 'youtube-dl)
112 (defcustom youtube-dl-auto-show-list t
113 "Auto show youtube-dl-list buffer."
114 :type 'boolean
115 :safe #'booleanp
116 :group 'youtube-dl)
118 (defcustom youtube-dl-process-model 'multiple-processes
119 "Determine youtube-dl.el downloading process model."
120 :safe #'symbolp
121 :type '(choice (symbol :tag "single process only" 'single-process)
122 (symbol :tag "multiple processes" 'multiple-processes)))
124 (defcustom youtube-dl-max-failures 8
125 "Maximum number of retries for a single video."
126 :type 'integer
127 :safe #'numberp
128 :group 'youtube-dl)
130 (defcustom youtube-dl-slow-rate-limit "2M"
131 "Download rate for slow item (corresponding to option --limit-rate)."
132 :type 'string
133 :safe #'stringp
134 :group 'youtube-dl)
136 (defcustom youtube-dl-notify nil
137 "Whether raise notifications."
138 :type 'boolean
139 :safe #'booleanp
140 :group 'youtube-dl)
142 (defcustom youtube-dl-destination-illegal-filename-chars-regex "[#$%+!<>&*|{}?=@:/\\\"'`]"
143 "Specify the regex of invalid destination filename.
144 This will avoid youtube-dl can't save invalid filename caused error.
145 https://www.mtu.edu/umc/services/websites/writing/characters-avoid/"
146 :type 'string
147 :safe #'stringp
148 :group 'youtube-dl)
150 ;;; TEST:
151 ;; (replace-regexp-in-string
152 ;; youtube-dl-destination-illegal-filename-chars-regex "-"
153 ;; "[#$%+!<>&*|{}?=@:/\\\"'`]")
155 (defface youtube-dl-active
156 '((t :inherit font-lock-function-name-face :foreground "forest green"))
157 "Face for highlighting the active download item."
158 :group 'youtube-dl)
160 (defface youtube-dl-slow
161 '((t :inherit font-lock-variable-name-face :foreground "orange"))
162 "Face for highlighting the slow (S) tag."
163 :group 'youtube-dl)
165 (defface youtube-dl-pause
166 '((t :inherit font-lock-type-face :foreground "gray"))
167 "Face for highlighting the pause (P) tag."
168 :group 'youtube-dl)
170 (defface youtube-dl-priority
171 '((t :inherit font-lock-keyword-face :foreground "light blue"))
172 "Face for highlighting the priority marker."
173 :group 'youtube-dl)
175 (defface youtube-dl-failure
176 '((t :inherit font-lock-warning-face :foreground "red"))
177 "Face for highlighting the failure marker."
178 :group 'youtube-dl)
180 (defvar-local youtube-dl--log-item nil
181 "Item currently being displayed in the log buffer.")
183 (cl-defstruct (youtube-dl-item (:constructor youtube-dl-item--create)
184 (:copier nil))
185 "Represents a single video to be downloaded with youtube-dl."
186 url ; Video URL (string)
187 vid ; Video ID (integer)
188 directory ; Working directory for youtube-dl (string or nil)
189 destination ; Preferred destination file (string or nil)
190 running ; Status indicator of process (boolean)
191 failures ; Number of video download failures (integer)
192 priority ; Download priority (integer)
193 title ; Listing display title (string or nil)
194 percentage ; Current download percentage (string or nil)
195 total-size ; Total download size (string or nil)
196 log ; All program output (list of strings)
197 log-end ; Last log item (list of strings)
198 paused-p ; Non-nil if download is paused (boolean)
199 slow-p ; Non-nil if download should be rate limited (boolean)
200 rate-limit ; Download rate limit (e.g. 50K or 2M)
201 buffer ; the process buffer name
202 process ; the process object
203 download-rate ; Download rate in bytes per second (e.g. 50K or 2M)
204 eta ; ETA time (e.g. 01:01:57)
205 type ; media type: video, audio, subtitle, thumbnail etc.
208 (defvar youtube-dl-items ()
209 "List of all items still to be downloaded.")
211 (defvar youtube-dl-process nil
212 "The currently active youtube-dl process.")
214 (defun youtube-dl--process-buffer-name (vid)
215 "Return the process buffer name which constructed with VID."
216 (format " *youtube-dl vid:%s*" vid))
218 (defun youtube-dl--proxy-append (url &optional option value)
219 "Decide whether append proxy option in youtube-dl command based on URL."
220 (let ((domain (url-domain (url-generic-parse-url url))))
221 (if (and (member domain youtube-dl-proxy-url-list) ; <-- whether toggle proxy?
222 (not (string-empty-p youtube-dl-proxy)))
223 (if option ; <-- whether has command-line option?
224 (list "--proxy" youtube-dl-proxy option value)
225 (list "--proxy" youtube-dl-proxy))
226 (if option ; <-- return original arguments for no proxy
227 (list option value)
228 nil ; <-- return nothing for url no need proxy
229 ))))
231 (defun youtube-dl--next ()
232 "Returns the next item to be downloaded."
233 (let (best best-score)
234 (dolist (item youtube-dl-items best)
235 (let* ((failures (youtube-dl-item-failures item))
236 (priority (youtube-dl-item-priority item))
237 (paused-p (youtube-dl-item-paused-p item))
238 (score (- priority failures)))
239 (when (and (not paused-p)
240 (< failures youtube-dl-max-failures))
241 (cond ((null best)
242 (setf best item
243 best-score score))
244 ((> score best-score)
245 (setf best item
246 best-score score))))))))
248 (defun youtube-dl--current ()
249 "Return the item currently being downloaded."
250 (when youtube-dl-process
251 (plist-get (process-plist youtube-dl-process) :item)))
253 (defun youtube-dl--remove (item)
254 "Remove ITEM from the queue and kill process."
255 (let ((proc (youtube-dl-item-process item)))
256 (when (process-live-p proc) ; `kill-process' only when `proc' is still alive.
257 (kill-process proc)))
258 (setf youtube-dl-items (cl-delete item youtube-dl-items)))
260 (defun youtube-dl--add (item)
261 "Add ITEM to the queue."
262 (setf youtube-dl-items (nconc youtube-dl-items (list item))))
264 (defvar youtube-dl--timer-item nil
265 "A global variable for passing ITEM into `youtube-dl--run' in `run-with-timer'.")
267 (defun youtube-dl--sentinel (proc status)
268 (when-let ((item (plist-get (process-plist proc) :item)))
269 ;; (setf youtube-dl-process proc) ; dont' need to set current process.
270 (cond
271 ;; process status is finished.
272 ((equal status "finished\n")
273 ;; mark item structure property `:running' to `nil'.
274 (setf (youtube-dl-item-running item) nil)
275 (youtube-dl--remove item)
276 (when youtube-dl-notify
277 (cl-case system-type
278 (gnu/linux
279 (notifications-notify :title "youtube-dl.el" :body "Download finished."))
280 (darwin
281 (ns-do-applescript
282 (format "display notification \"%s\" with title \"%s\"" "youtube-dl.el" "Download finished.")))
283 (windows-nt nil))))
284 ;; detect whether process is in "pause" process status?
285 ((youtube-dl-item-paused-p item)
286 ;; mark item structure property `:running' to `nil'.
287 (setf (youtube-dl-item-running item) nil)
288 (message "[youtube-dl] process %s is paused." proc))
289 ;; when process downloading failed, then retry downloading.
290 ((equal status "killed: 9\n")
291 ;; mark item structure property `:running' to `nil'.
292 (setf (youtube-dl-item-running item) nil)
293 (message "[youtube-dl] process %s is killed." proc))
295 ;; mark item structure property `:running' to `nil'.
296 (setf (youtube-dl-item-running item) nil)
297 (cl-incf (youtube-dl-item-failures item))
298 ;; record log output to process buffer
299 (let ((buf (youtube-dl--log-buffer item))
300 (inhibit-read-only t))
301 (with-current-buffer buf
302 (print status buf)
303 (print (process-plist proc) buf)))
304 ;; re-run process
305 (if (<= (youtube-dl-item-failures item) 10)
306 (youtube-dl--run)
307 (setq youtube-dl--timer-item item)
308 (run-with-timer (* 60 5) nil #'youtube-dl--run)
309 (message "[youtube-dl] delay process %s" proc))))))
311 (defun youtube-dl--progress (output)
312 "Return the download progress for the given output.
313 Progress lines that straddle output chunks are lost. That's fine
314 since this is just used for display purposes.
316 Return list: (\"34.6%\" \"~2.61GiB\" \"929.50KiB/s\") "
317 (let ((start 0)
318 (progress nil))
319 (cond
320 ;; [download] 34.6% of ~2.61GiB at 929.50KiB/s ETA 01:01:57
321 ;; +---+ +------+ +---------+ +------+
322 ((string-equal youtube-dl-program "youtube-dl")
323 (while (string-match "\\[download\\] +\\([^ ]+%\\) +of *~? *\\([^ ]+\\) +at +\\([^ ]+\\) +ETA +\\([^ \n]+\\)" output start)
324 (let ((percent (match-string 1 output))
325 (total-size (match-string 2 output))
326 (download-rate (match-string 3 output))
327 (eta (match-string 4 output)))
328 (setf progress (list percent total-size download-rate eta)
329 start (match-end 0)))))
330 ((string-equal youtube-dl-program "yt-dlp")
331 ;; The output has non-ASCII shell color codes. Disable it with command-line option "--no-colors".
332 ;; [download] 0.4% of ~ 314.23MiB at 7.54KiB/s ETA 11:48:26 (frag 0/216)
333 ;; +--+ +-------+ +-------+ +------+
334 (while (string-match "\\[download\\] +\\([^ ]+%\\) +of *~? *\\([^ ]+\\) +at +\\([^ ]+\\) +ETA +\\([^ \n]+\\)" output start)
335 (let ((percent (match-string 1 output))
336 (total-size (match-string 2 output))
337 (download-rate (match-string 3 output))
338 (eta (match-string 4 output)))
339 ;; filter out not correct matched data.
340 ;; DEBUG:
341 ;; (message "[DEBUG] [youtube-dl] download-rate regexp matched: >>> %s <<<" download-rate)
342 ;; (message "[DEBUG] [youtube-dl] eta regexp matched: >>> %s <<<" eta)
343 (unless (or (string-equal-ignore-case total-size "Unknown.*")
344 (string-equal-ignore-case eta "Unknown.*") ; [download] 0.0% of 2.14MiB at Unknown B/s ETA Unknown
346 (setf progress (list percent total-size download-rate eta)
347 start (match-end 0)))))))
348 ;; DEBUG:
349 ;; (when progress
350 ;; (cl-destructuring-bind (percentage total-size download-rate eta) progress
351 ;; (message "[DEBUG] [youtube-dl] progress: percent: %s, total-size: %s, speed: %s, eta: %s."
352 ;; percentage total-size download-rate eta)))
353 progress))
355 (defun youtube-dl--get-destination (url)
356 "Return the destination filename for the given `URL' (if any).
357 The destination filename may potentially straddle two output
358 chunks, but this is incredibly unlikely. It's only used for
359 display purposes anyway.
361 $ youtube-dl --get-filename <URL>"
362 (message "[youtube-dl] getting video destination.")
363 (let* ((get-destination-buffer " *youtube-dl-get-destination*")
364 (get-destination-error-buffer " *youtube-dl-get-destination error*")
365 (destination))
366 ;; clear destination buffer content to clear destination history.
367 (progn
368 (when (buffer-live-p (get-buffer get-destination-buffer))
369 (with-current-buffer get-destination-buffer (erase-buffer)))
370 (when (buffer-live-p (get-buffer get-destination-error-buffer))
371 (with-current-buffer get-destination-error-buffer (erase-buffer))))
372 ;; retrieve destination
373 (make-process
374 :command (list youtube-dl-program "--get-filename" url)
375 :sentinel (lambda (proc event)
376 (if (string= event "finished\n")
377 (setq destination
378 (replace-regexp-in-string ; replace illegal filename characters in `destination'.
379 youtube-dl-destination-illegal-filename-chars-regex "-"
380 (replace-regexp-in-string
381 "\\ +\\[.*\\]" "" ; delete trailing [...] in filename.
382 (replace-regexp-in-string
383 "\n" "" ; delete trailing "\n" character.
384 (with-current-buffer get-destination-buffer (buffer-string))))))
385 (let* ((buffer-str (with-current-buffer get-destination-error-buffer (buffer-string)))
386 (match-regex "\\(ERROR\\|Error\\): \\(.*\\)")
387 (error-p (string-match-p match-regex buffer-str))
388 (matched-str (when (string-match match-regex buffer-str) (match-string 2 buffer-str))))
389 (if error-p
390 (progn
391 (message "[youtube-dl] `youtube-dl--get-destination' error!\n%s" matched-str)
392 ;; TODO:
393 ;; (funcall 'youtube-dl--get-destination url)
395 (progn
396 (message "[youtube-dl] get output filename success.")
397 ;; (kill-process proc)
398 ;; (kill-buffer (process-buffer proc))
399 )))))
400 :name "youtube-dl-get-destination"
401 :buffer get-destination-buffer
402 :stderr get-destination-error-buffer)
404 (setq destination-not-returned t)
405 (dotimes (i 10)
406 (when destination-not-returned
407 (sleep-for 1)
408 (if destination (setq destination-not-returned nil))))
410 ;; Prompt for user the destination interactively.
411 (unless destination
412 (setq destination (substring-no-properties (read-string "[youtube-dl] input destination: "))))
414 (when destination
415 (kill-new destination) ; copy to Emacs kill-ring for later operation.
416 (message "[youtube-dl] have got destination : %s" destination)
417 destination)))
419 ;;; TEST:
420 ;; (youtube-dl--get-destination "https://www.pornhub.com/view_video.php?viewkey=ph603b575e00170")
422 (defun youtube-dl--filter (proc output)
423 (if (plist-get (process-plist proc) :item)
424 (let* ((item (plist-get (process-plist proc) :item))
425 (progress (youtube-dl--progress output))
426 (destination (youtube-dl-item-destination item)))
427 ;; Append to program log.
428 (let ((logged (list output)))
429 (if (and (youtube-dl-item-log item) (youtube-dl-item-log-end item)) ; make sure not nil.
430 (setf (cdr (youtube-dl-item-log-end item)) logged
431 (youtube-dl-item-log-end item) logged)
432 (setf (youtube-dl-item-log item) logged
433 (youtube-dl-item-log-end item) logged)))
434 ;; Update progress information.
435 (when progress
436 (cl-destructuring-bind (percentage total-size download-rate eta) progress
437 (setf (youtube-dl-item-percentage item) percentage
438 (youtube-dl-item-total-size item) total-size
439 (youtube-dl-item-download-rate item) download-rate
440 (youtube-dl-item-eta item) eta)))
441 ;; monitor process output whether error occurred?
442 (let* ((item-log-end (youtube-dl-item-log-end item))
443 (log-end-str (cond
444 ((stringp item-log-end) item-log-end)
445 ((listp item-log-end) (car (last item-log-end)))))
446 (error-hander (lambda () (setf (youtube-dl-item-running item) nil)
447 (message "[youtube-dl] ✖ <proc: %s>, destination: %s"
448 (youtube-dl-item-process item) (youtube-dl-item-destination item))))
449 (running-handler (lambda () (setf (youtube-dl-item-running item) t)))
450 (finished-handler (lambda () (setf (youtube-dl-item-running item) nil)
451 (message "[youtube-dl] ✔ %s downloaded." (youtube-dl-item-destination item)))))
452 (when (stringp log-end-str)
453 (cond
454 ;; progress finished when percentage is 100%.
455 ((or (string-equal (youtube-dl-item-percentage item) "100%")
456 (string-match "\\[download\\]\\ *100%.*" log-end-str)) ; ".*100%.*"
457 (funcall finished-handler))
458 ;; process exited abnormally with code 1
459 ((string-match "exited abnormally with code 1" log-end-str)
460 (funcall error-hander))
461 ;; file already downloaded
462 ((string-match "ERROR: Fixed output name but more than one file to download:.*" log-end-str)
463 ;; (funcall error-hander)
464 (message "[youtube-dl] ERROR: Already has existing downloading process or output file! (%s)" (youtube-dl-item-destination item)))
465 ;; any other errors
466 ((string-match "ERROR:.*" log-end-str)
467 (funcall error-hander))
468 (t (funcall running-handler)))))
469 ;; DEBUG:
470 ;; (message "[DEBUG] [youtube-dl] destination/title: %s" destination)
471 ;; Set item title to destination if it's empty.
472 (unless (youtube-dl-item-title item)
473 (setf (youtube-dl-item-title item) destination))
474 ;; write output to process associated buffer.
475 (with-current-buffer (process-buffer proc)
476 (let ((moving (= (point) (process-mark proc)))
477 ;; avoid process buffer read-only issue for `insert'.
478 (inhibit-read-only t))
479 (save-excursion
480 ;; Insert the text, advancing the process marker.
481 (goto-char (process-mark proc))
482 (insert output)
483 (set-marker (process-mark proc) (point)))
484 (if moving (goto-char (process-mark proc)))))
485 (youtube-dl--redisplay))))
487 (defun youtube-dl--construct-command (item)
488 "Construct full command of youtube-dl with arguments from ITEM."
489 (let ((url (youtube-dl-item-url item))
490 (slow-p (youtube-dl-item-slow-p item))
491 (rate-limit (youtube-dl-item-rate-limit item))
492 (destination (youtube-dl-item-destination item)))
493 (append
494 (list youtube-dl-program "--newline")
495 youtube-dl-extra-arguments
496 (when youtube-dl-omit-mtime (list "--no-mtime"))
497 (when youtube-dl-restrict-filenames (list "--restrict-filenames"))
498 ;; Disable non-ASCII shell color codes in output.
499 (cond
500 ((string-equal youtube-dl-program "youtube-dl") '("--no-color"))
501 ((string-equal youtube-dl-program "yt-dlp") '("--no-colors")))
502 (youtube-dl--proxy-append url)
503 (when slow-p `("--rate-limit" ,rate-limit))
504 (when destination `("--output" ,destination))
505 `("--" ,url))))
507 (defun youtube-dl--run-single-process ()
508 "Start youtube-dl downloading in single process."
509 (let ((item (youtube-dl--next))
510 (current-item (youtube-dl--current)))
511 (if (eq item current-item)
512 (youtube-dl--redisplay) ; do nothing, just display the youtube-dl-list buffer.
513 (if youtube-dl-process
514 (progn
515 ;; Switch to higher priority job, but offset error count first.
516 (cl-decf (youtube-dl-item-failures current-item))
517 (kill-process youtube-dl-process)) ; sentinel will clean up
518 ;; No subprocess running, start a one.
519 (let* ((url (substring-no-properties (youtube-dl-item-url item)))
520 (directory (youtube-dl-item-directory item))
521 (destination (youtube-dl-item-destination item))
522 (vid (youtube-dl-item-vid item))
523 (proc-buffer-name (youtube-dl--process-buffer-name vid))
524 (default-directory
525 (if directory
526 (concat (directory-file-name directory) "/")
527 (concat (directory-file-name youtube-dl-download-directory) "/")))
528 (_ (mkdir default-directory t))
529 (slow-p (youtube-dl-item-slow-p item))
530 (rate-limit (or (youtube-dl-item-rate-limit item) youtube-dl-slow-rate-limit))
531 (proc (make-process
532 :name proc-buffer-name
533 :command (let ((command (youtube-dl--construct-command item)))
534 ;; Insert complete command into process buffer for debugging.
535 (with-current-buffer (get-buffer-create proc-buffer-name)
536 (insert (format "Command: %s" command)))
537 command)
538 :sentinel #'youtube-dl--sentinel
539 :filter #'youtube-dl--filter
540 :buffer proc-buffer-name)))
541 (set-process-plist proc (list :item item))
542 (setf youtube-dl-process proc)
543 ;; mark item structure property `:running' to `t'.
544 (setf (youtube-dl-item-running item) t))))
545 (youtube-dl--redisplay)))
547 (defun youtube-dl--run-multiple-processes (&optional item)
548 "Start youtube-dl downloading in multiple processes."
549 (let* ((item (or item
550 youtube-dl--timer-item ; use item from `run-with-timer'.
551 (with-current-buffer (youtube-dl--list-buffer)
552 (nth (1- (line-number-at-pos)) youtube-dl-items))))
553 (current-item (youtube-dl--current)) ; `youtube-dl-process' currently actived process.
554 (proc (youtube-dl-item-process item)))
555 (unless (and item (youtube-dl-item-running item)) ; whether item structure property `:running' is `t'?
556 (let* ((url (substring-no-properties (youtube-dl-item-url item)))
557 (directory (youtube-dl-item-directory item))
558 (destination (youtube-dl-item-destination item))
559 (vid (youtube-dl-item-vid item))
560 (proc-buffer-name (youtube-dl--process-buffer-name vid))
561 (default-directory
562 (if directory
563 (concat (directory-file-name directory) "/")
564 (concat (directory-file-name youtube-dl-download-directory) "/")))
565 (_ (mkdir default-directory t))
566 (slow-p (youtube-dl-item-slow-p item))
567 (rate-limit (or (youtube-dl-item-rate-limit item) youtube-dl-slow-rate-limit))
568 (failures (youtube-dl-item-failures item))
569 (proc (when (<= failures 10) ; re-run process only when failures <= 10.
570 (make-process
571 :name proc-buffer-name
572 :command (let ((command (youtube-dl--construct-command item)))
573 ;; Insert complete command into process buffer for debugging.
574 (let ((inhibit-read-only t))
575 (with-current-buffer (get-buffer-create proc-buffer-name)
576 (insert (format "Command: %s" command))))
577 command)
578 :sentinel #'youtube-dl--sentinel
579 :filter #'youtube-dl--filter
580 :buffer proc-buffer-name))))
581 ;; clear temporary item variable `youtube-dl--timer-item'.
582 (setq youtube-dl--timer-item nil)
583 (when (processp proc)
584 ;; set process property list.
585 (set-process-plist proc (list :item item))
586 ;; assign `proc' object to item slot `:process'.
587 (setf (youtube-dl-item-process item) proc)
588 ;; set current youtube-dl process variable.
589 (setf youtube-dl-process proc)
590 ;; mark item structure property `:running' to `t'.
591 (setf (youtube-dl-item-running item) t))))
592 (youtube-dl--redisplay)))
594 (defun youtube-dl--run (&optional item)
595 "Start youtube-dl downloading."
596 ;; if single process model, then start download next item.
597 ;; if multiple processes model, then don't start next item `youtube-dl--run'.
598 (cl-case youtube-dl-process-model
599 (single-process (youtube-dl--run-single-process item))
600 (multiple-processes (youtube-dl--run-multiple-processes item))))
602 (defun youtube-dl--get-vid (url)
603 "Get video `URL' video vid with youtube-dl option `--get-id'."
604 (message "[youtube-dl] getting video vid.")
605 (let* ((parsed-url (url-generic-parse-url url))
606 (domain (url-domain parsed-url))
607 (parameters (url-filename parsed-url)))
608 ;; URL domain matching with regexp to extract vid.
609 (pcase domain
610 ("bilibili.com" ; "/video/BV1B8411L7te/", "/video/BV1uj411N7cp?spm_id_from=..0.0"
611 (when (string-match "/video/\\([^/?&]*\\)" parameters)
612 (match-string 1 parameters)))
613 ("pornhub.com" ; "/view_video.php?viewkey=ph6238f9c7cc9e2"
614 (when (string-match "/view_video\\.php\\?viewkey=\\([^/?&]*\\)" parameters)
615 (match-string 1 parameters)))
616 ("youtube.com" ; "/watch?v=q-iLEteyeUQ", "/watch?v=48JlgiBpw_I&t=311s"
617 (when (string-match "/watch\\?v=\\([^/?&]*\\)" parameters)
618 (match-string 1 parameters)))
619 (_ (progn
620 (let* ((output (with-temp-buffer
621 (apply #'call-process
622 youtube-dl-program
623 nil t nil
624 (youtube-dl--proxy-append url "--get-id" url))
625 (buffer-string)))
626 (output-lines-list (string-lines output))
627 (error-p (string-match-p "ERROR" output)))
628 (cond
629 ;; ("VrAfJvZGGXE" "")
630 ;; TEST: (youtube-dl--get-vid "https://www.youtube.com/watch?v=VROjLiq9LeQ")
631 ((length= output-lines-list 2) (car output-lines-list))
632 ;; TEST: (youtube-dl--get-vid "https://hanime1.me/watch?v=22454")
633 ;; ("WARNING: Falling back on generic information extractor." "watch?v=22454" "")
634 ((length= output-lines-list 3) (car (last output-lines-list 1)))
635 ;; TEST: (youtube-dl--get-vid "https://www.pornhub.com/view_video.php?viewkey=ph637ed6daf1795")
636 ;; WARNING: unable to extract view count; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
637 ((string-match-p "WARNING" (car output-lines-list))
638 (car (last output-lines-list 1)))
639 ;; get vid failed, use URL string regex matching instead.
640 (t (progn
641 (when error-p
642 ;; WARNING: Could not send HEAD request to https://hanime1.me/watch?v=22709:
643 ;; HTTP Error 503: Service Temporarily Unavailable
644 ;; ERROR: Unable to download webpage: HTTP Error 503: Service Temporarily
645 ;; Unavailable (caused by <HTTPError 503: 'Service Temporarily
646 ;; Unavailable'>); please report this issue on https://yt-dl.org/bug . Make
647 ;; sure you are using the latest version; see https://yt-dl.org/update on
648 ;; how to update. Be sure to call youtube-dl with the --verbose flag and
649 ;; include its complete output.
651 ;; WARNING: unable to extract view count; please report this
652 ;; issue on https://yt-dl.org/bug . Make sure you are using
653 ;; the latest version; see https://yt-dl.org/update on how
654 ;; to update. Be sure to call youtube-dl with the --verbose
655 ;; flag and include its complete output.
656 (error (format "[youtube-dl] `youtube-dl--get-vid' retrive video vid error!\n%s" output)))
657 parameters)))))))))
659 ;;; TEST:
660 ;; (let ((parameters "/video/BV1B8411L7te/"))
661 ;; (when (string-match "/video/\\([^/]*\\)" parameters)
662 ;; (match-string 1 parameters)))
664 ;; (youtube-dl--get-vid "https://www.youtube.com/watch?v=VROjLiq9LeQ")
665 ;; (youtube-dl--get-vid "https://www.pornhub.com/view_video.php?viewkey=ph637ed6daf1795")
667 ;;;###autoload
668 (cl-defun youtube-dl
669 (url &key title (priority 0) directory destination paused slow)
670 "Queues URL for download using youtube-dl, returning the new item.
671 By default, it downloads to ~/Downloads/."
672 (interactive
673 (list (substring-no-properties
674 (read-from-minibuffer
675 "URL: " (or (thing-at-point 'url)
676 (when interprogram-paste-function
677 (funcall interprogram-paste-function)))))))
678 ;; remove this ID failure only on youtube.com, use URL as ID. or use youtube-dl extracted title, or hash on URL.
679 (let* ((vid (youtube-dl--get-vid url))
680 (destination (or (youtube-dl--get-destination url) destination))
681 (title (or (replace-regexp-in-string (format "-%s.*" vid) "" destination)
682 (car (split-string destination "-"))))
683 (type (youtube-dl--get-type-for-extension (file-name-extension destination)))
684 (proc-buffer-name (youtube-dl--process-buffer-name vid))
685 (full-dir (expand-file-name (or directory "") youtube-dl-download-directory))
686 (item (youtube-dl-item--create :url url
687 :vid vid
688 :failures 0
689 :priority priority
690 :paused-p paused
691 :slow-p slow
692 :rate-limit nil
693 :directory full-dir
694 :destination destination
695 :title title
696 :type type
697 :buffer proc-buffer-name
698 :process nil)))
699 (prog1 item
700 (unless (youtube-dl-item-running item)
701 (youtube-dl--add item) ; Add ITEM to the queue.
702 (youtube-dl--run item) ; Start ITEM in the queue.
703 (when youtube-dl-auto-show-list
704 (youtube-dl-list))))))
706 (defalias 'youtube-dl-download-video 'youtube-dl)
708 (defun youtube-dl--playlist-list (playlist)
709 "For each video, return one plist with :index, :vid, and :title."
710 (with-temp-buffer
711 (when (zerop (call-process youtube-dl-program nil t nil
712 "--ignore-config"
713 "--dump-json"
714 "--flat-playlist"
715 playlist))
716 (goto-char (point-min))
717 (cl-loop with json-object-type = 'plist
718 for index upfrom 1
719 for video = (ignore-errors (json-read))
720 while video
721 collect (list :index index
722 :vid (plist-get video :vid)
723 :title (plist-get video :title))))))
725 (defun youtube-dl--playlist-reverse (list)
726 "Return a copy of LIST with the indexes reversed."
727 (let ((max (cl-loop for entry in list
728 maximize (plist-get entry :index))))
729 (cl-loop for entry in list
730 for index = (plist-get entry :index)
731 for copy = (copy-sequence entry)
732 collect (plist-put copy :index (- (1+ max) index)))))
734 (defun youtube-dl--playlist-cutoff (list n)
735 "Return a sorted copy of LIST with all items except where :index < N."
736 (let ((key (lambda (v) (plist-get v :index)))
737 (filter (lambda (v) (< (plist-get v :index) n)))
738 (copy (copy-sequence list)))
739 (cl-delete-if filter (cl-stable-sort copy #'< :key key))))
741 ;;;###autoload
742 (cl-defun youtube-dl-download-playlist
743 (url &key directory (first 1) paused (priority 0) reverse slow)
744 "Add entire playlist to download queue, with index prefixes.
746 :directory PATH -- Destination directory for all videos.
748 :first INDEX -- Start downloading from a given one-based index.
750 :paused BOOL -- Start all download entries as paused.
752 :priority PRIORITY -- Use this priority for all download entries.
754 :reverse BOOL -- Reverse the video numbering, solving the problem
755 of reversed playlists.
757 :slow BOOL -- Start all download entries in slow mode."
758 (interactive
759 (list (read-from-minibuffer
760 "URL: "
761 (when interprogram-paste-function
762 (funcall interprogram-paste-function)))))
763 (message "[youtube-dl] fetching playlist ...")
764 (let ((videos (youtube-dl--playlist-list url)))
765 (if (null videos)
766 (error "Failed to fetch playlist (%s)." url)
767 (let* ((max (cl-loop for entry in videos
768 maximize (plist-get entry :index)))
769 (width (1+ (floor (log max 10))))
770 (prefix-format (format "%%0%dd" width)))
771 (when reverse
772 (setf videos (youtube-dl--playlist-reverse videos)))
773 (dolist (video (youtube-dl--playlist-cutoff videos first))
774 (let* ((index (plist-get video :index))
775 (prefix (format prefix-format index))
776 (title (format "%s-%s" prefix (plist-get video :title)))
777 (dest (format "%s-%s" prefix "%(title)s-%(id)s.%(ext)s")))
778 (youtube-dl (plist-get video :url)
779 :title title
780 :priority priority
781 :directory directory
782 :destination dest
783 :paused paused
784 :slow slow)))))))
786 ;; List user interface:
788 ;;; refresh youtube-dl-list buffer (2).
789 (defun youtube-dl-list-redisplay ()
790 "Immediately redraw the queue list buffer."
791 (interactive)
792 (with-current-buffer (youtube-dl--list-buffer)
793 (let ((save-point (point))
794 (window (get-buffer-window (current-buffer))))
795 (youtube-dl--fill-listing)
796 (goto-char save-point)
797 (when window
798 (set-window-point window save-point))
799 (when hl-line-mode
800 (hl-line-highlight)))))
802 ;;; refresh youtube-dl-list buffer (1).
803 (defun youtube-dl--redisplay ()
804 "Redraw the queue list buffer only if visible."
805 (let ((log-buffer (youtube-dl--log-buffer)))
806 (when log-buffer
807 (with-current-buffer log-buffer
808 (let ((inhibit-read-only t)
809 (saved-point (point))
810 (saved-point-max (point-max))
811 (window (get-buffer-window log-buffer)))
812 (erase-buffer)
813 (mapc #'insert (youtube-dl-item-log youtube-dl--log-item))
814 (when window
815 (set-window-point window (if (< saved-point saved-point-max)
816 saved-point
817 (point-max))))))))
818 (when (get-buffer-window (youtube-dl--list-buffer))
819 (youtube-dl-list-redisplay)))
821 (defun youtube-dl--pointed-item ()
822 "Get item under point. But signal an error if no item under point."
823 (unless (eq (current-buffer) (youtube-dl--list-buffer))
824 (user-error "The operation is ONLY available in `%s' buffer." youtube-dl--list-buffer-name))
825 (let ((item (nth (1- (line-number-at-pos)) youtube-dl-items)))
826 (if item item (error "[youtube-dl] No item at point."))))
828 (defun youtube-dl-list-log ()
829 "Display the log of the video under point."
830 (interactive)
831 (let* ((item (youtube-dl--pointed-item))
832 (buffer (youtube-dl--log-buffer item)))
833 (when item
834 (display-buffer buffer)
835 (select-window (get-buffer-window buffer))
836 (youtube-dl--redisplay))))
838 (defun youtube-dl-list-kill-log ()
839 "Kill the youtube-dl log buffer."
840 (interactive)
841 (let ((buffer (youtube-dl--log-buffer)))
842 (when buffer
843 (kill-buffer buffer))))
845 (defun youtube-dl-list-yank ()
846 "Copy the URL of the video under point to the clipboard."
847 (interactive)
848 (when-let* ((item (youtube-dl--pointed-item))
849 (url (youtube-dl-item-url item)))
850 (kill-new url)
851 (message "[youtube-dl] yanked %s" url)))
853 (defun youtube-dl-list-kill ()
854 "Remove the selected item from the queue."
855 (interactive)
856 (when-let* ((_ youtube-dl-items) ; avoid `youtube-dl-items' is `nil'.
857 (item (youtube-dl--pointed-item))
858 (proc (youtube-dl-item-process item)))
859 (youtube-dl--remove item)
860 (youtube-dl-list-redisplay)
861 (message "[youtube-dl] process %s is killed." proc)))
863 (defun youtube-dl-list-pause (&optional item)
864 "Pause downloading of item under point."
865 (interactive)
866 (let* ((item (or item (youtube-dl--pointed-item)))
867 (proc (youtube-dl-item-process item))
868 (paused-p (youtube-dl-item-paused-p item)))
869 (unless (and item paused-p)
870 ;; kill the process, but keep item on list buffer for pause status.
871 (when (process-live-p proc) (kill-process proc))
872 ;; after killed process, also setting item structure status properties.
873 (setf (youtube-dl-item-paused-p item) t)
874 (setf (youtube-dl-item-running item) nil))
875 (youtube-dl-list-redisplay)
876 (message "[youtube-dl] process %s is paused." proc)))
878 (defun youtube-dl-list-resume (&optional item)
879 "Resume failure/paused downloading item under point."
880 (interactive)
881 (when-let* ((item (or item (youtube-dl--pointed-item)))
882 (proc (youtube-dl-item-process item)))
883 (when item
884 (setf (youtube-dl-item-failures item) 0)
885 (cond
886 ((youtube-dl-item-paused-p item)
887 (setf (youtube-dl-item-paused-p item) nil)
888 (youtube-dl--run))
889 ((not (youtube-dl-item-running item))
890 (youtube-dl--run))
892 (setf (youtube-dl-item-running item) nil)
893 (youtube-dl--run)
894 ;; (user-error (format "[youtube-dl] Can't resume process %s correctly. Try resume again." proc))
896 (youtube-dl-list-redisplay)
897 (message "[youtube-dl] process %s is resumed." proc))))
899 (defun youtube-dl-list-toggle-pause (&optional item)
900 "Toggle pause downloading of item under point."
901 (interactive)
902 (let* ((item (or item (youtube-dl--pointed-item)))
903 (paused-p (youtube-dl-item-paused-p item)))
904 (if (and item paused-p)
905 (youtube-dl-list-resume item)
906 (youtube-dl-list-pause item))))
908 (defun youtube-dl-list-toggle-pause-all ()
909 "Toggle pause downloading of all items."
910 (interactive)
911 (let* ((item (youtube-dl--pointed-item))
912 (paused-p (youtube-dl-item-paused-p item))))
913 (if paused-p
914 (dolist (item youtube-dl-items)
915 (youtube-dl-list-pause item))
916 (dolist (item youtube-dl-items)
917 (youtube-dl-list-resume item))))
919 (defun youtube-dl-list-toggle-slow (item &optional rate-limit)
920 "Set slow rate limit on item under point."
921 (interactive (youtube-dl--pointed-item))
922 (when item
923 (let ((proc (youtube-dl-item-process item))
924 (slow-p (youtube-dl-item-slow-p item))
925 (rate-limit (substring-no-properties
926 (or rate-limit
927 (completing-read "[youtube-dl] limit download rate (e.g. 100K): "
928 '("20K" "50K" "100K" "200K" "500K" "1M" "2M"))))))
929 ;; restart with a slower download rate.
930 (when (process-live-p proc) (kill-process proc))
931 (setf (youtube-dl-item-slow-p item) (not slow-p))
932 (setf (youtube-dl-item-rate-limit item) rate-limit)
933 (youtube-dl--run)))
934 (youtube-dl-list-redisplay))
936 (defun youtube-dl-list-toggle-slow-all ()
937 "Set slow rate on all items."
938 (interactive)
939 (let* ((count (length youtube-dl-items))
940 (slow-count (cl-count-if #'youtube-dl-item-slow-p youtube-dl-items))
941 (target (< slow-count (- count slow-count)))
942 (rate-limit (completing-read "[youtube-dl] limit download rate (e.g. 100K): "
943 '("20K" "50K" "100K" "200K" "500K" "1M" "2M"))))
944 (dolist (item youtube-dl-items)
945 (youtube-dl-list-toggle-slow item rate-limit)))
946 (youtube-dl--redisplay))
948 (defun youtube-dl-list-priority-modify (delta)
949 "Change priority of item under point by DELTA."
950 (when-let ((item (youtube-dl--pointed-item)))
951 (cl-incf (youtube-dl-item-priority item) delta)
952 (youtube-dl--run)))
954 (defun youtube-dl-list-priority-up ()
955 "Decrease priority of item under point."
956 (interactive)
957 (youtube-dl-list-priority-modify 1))
959 (defun youtube-dl-list-priority-down ()
960 "Increase priority of item under point."
961 (interactive)
962 (youtube-dl-list-priority-modify -1))
964 (defun youtube-dl-list-next-item ()
965 "Move to next item in listing buffer."
966 (interactive)
967 (unless (zerop (forward-line 1)) (user-error "End of buffer"))
968 (unless (eobp) (beginning-of-line)))
970 (defun youtube-dl-list-prev-item ()
971 "Move to previous item in listing buffer."
972 (interactive)
973 (when (<= (line-number-at-pos) 1)
974 (user-error "Beginning of buffer"))
975 (forward-line -1)
976 (beginning-of-line))
978 (defvar youtube-dl-list-mode-map
979 (let ((map (make-sparse-keymap)))
980 (prog1 map
981 (define-key map "a" #'youtube-dl)
982 (define-key map "g" #'youtube-dl-list-redisplay)
983 (define-key map "l" #'youtube-dl-list-log)
984 (define-key map "L" #'youtube-dl-list-kill-log)
985 (define-key map "y" #'youtube-dl-list-yank)
986 (define-key map "k" #'youtube-dl-list-kill)
987 (define-key map "p" #'youtube-dl-list-toggle-pause)
988 (define-key map "P" #'youtube-dl-list-toggle-pause-all)
989 (define-key map "r" #'youtube-dl-list-resume)
990 (define-key map "s" #'youtube-dl-list-toggle-slow)
991 (define-key map "S" #'youtube-dl-list-toggle-slow-all)
992 (define-key map "]" #'youtube-dl-list-priority-up)
993 (define-key map "[" #'youtube-dl-list-priority-down)
994 (define-key map [down] #'youtube-dl-list-next-item)
995 (define-key map [up] #'youtube-dl-list-prev-item)))
996 "Keymap for `youtube-dl-list-mode'")
998 (defvar-local youtube-dl-list--auto-close-window-timer nil
999 "A timer to auto close youtube-dl list window.")
1001 (defun youtube-dl-list--auto-close-window ()
1002 "Auto close '*youtube-dl list*' buffer after finished all downloading."
1003 (when (equal (length youtube-dl-items) 0)
1004 (when-let ((buf (get-buffer-window (youtube-dl--list-buffer))))
1005 (delete-window buf)
1006 (when (timerp youtube-dl-list--auto-close-window-timer)
1007 (cancel-timer youtube-dl-list--auto-close-window-timer)))))
1009 (defvar youtube-dl-list--format
1010 ;; (percentage download-rate)
1011 ;; v
1012 ;;space,vid,progress size eta fails
1013 ;;| | | | | | status
1014 ;;| | | | | | | title
1015 ;;v v v v v v v v
1016 "%s%-16s %-22.22s %-10.10s %-6.6s %-7.7s %-8.8s %s"
1017 "Define the `youtube-dl-list-mode' `header-line-format' and list item format.")
1019 (define-derived-mode youtube-dl-list-mode special-mode "youtube-dl"
1020 "Major mode for listing the youtube-dl download queue."
1021 :group 'youtube-dl
1022 (use-local-map youtube-dl-list-mode-map)
1023 (hl-line-mode)
1024 (setq-local truncate-lines t) ; truncate long line text.
1025 (setf header-line-format
1026 (propertize
1027 (format youtube-dl-list--format
1028 (propertize " " 'display '((space :align-to 0))) ; space
1029 " vid " "| progress" "| size" "| ETA" "| fails" "| status"
1030 (concat "| title " (make-string 100 (string-to-char " "))))
1031 'face '(:inverse-video t :extend t))))
1033 (defvar youtube-dl--list-buffer-name " *youtube-dl list*"
1034 "The buffer name of `youtube-dl-list-mode'.")
1036 (defun youtube-dl--list-buffer ()
1037 "Returns the queue listing buffer."
1038 (if-let ((buf (get-buffer-create youtube-dl--list-buffer-name)))
1039 (with-current-buffer buf
1040 ;; TODO: use `tabulated-list-mode'.
1041 ;; (tabulated-list-mode)
1042 (youtube-dl-list-mode)
1043 (current-buffer))))
1045 (defun youtube-dl--log-buffer (&optional item)
1046 "Returns a youtube-dl log buffer for ITEM."
1047 (when item
1048 (let* ((name (youtube-dl-item-buffer item))
1049 (buffer (get-buffer-create name)))
1050 (with-current-buffer buffer
1051 (unless (eq major-mode 'special-mode)
1052 (special-mode))
1053 (setf youtube-dl--log-item item)
1054 (setf (youtube-dl-item-log item) item)
1055 (current-buffer)))))
1057 (defface youtube-dl-type-video
1058 '((t :foreground "green"))
1059 "Face for video type."
1060 :group 'youtube-dl)
1062 (defface youtube-dl-type-audio
1063 '((t :foreground "DeepSkyBlue"))
1064 "Face for audio type."
1065 :group 'youtube-dl)
1067 (defface youtube-dl-type-subtitle
1068 '((t :foreground "dark gray"))
1069 "Face for subtitle type."
1070 :group 'youtube-dl)
1072 (defface youtube-dl-type-thumbnail
1073 '((t :foreground "pink"))
1074 "Face for thumbnail type."
1075 :group 'youtube-dl)
1077 (defun youtube-dl--face-for-type (type)
1078 "Return the face for TYPE."
1079 (cl-case type
1080 (video 'youtube-dl-type-video)
1081 (audio 'youtube-dl-type-audio)
1082 (subtitle 'youtube-dl-type-subtitle)
1083 (thumbnail 'youtube-dl-type-thumbnail)))
1085 (defun youtube-dl--get-type-for-extension (extension)
1086 "Return media type depend on EXTENSION."
1087 (cond
1088 ((member extension '("avi" "rmvb" "ogg" "ogv" "mp4" "mkv" "mov" "webm" "flv" "ts" "mpg"))
1089 'video)
1090 ((member extension '("flac" "mp3" "wav" "m4a"))
1091 'audio)
1092 ((member extension '("ass" "srt" "sub" "vtt" "ssf"))
1093 'subtitle)
1094 ((member extension '("heic" "svg" "webp" "png" "gif" "tiff" "jpeg" "jpg" "xpm" "xbm" "pbm"))
1095 'thumbnail)))
1097 ;;; refresh youtube-dl-list buffer (3).
1098 (defun youtube-dl--fill-listing ()
1099 "Erase and redraw the queue in the queue listing buffer."
1100 (with-current-buffer (youtube-dl--list-buffer)
1101 (let* ((inhibit-read-only t)
1102 (active (youtube-dl--current)))
1103 (erase-buffer)
1104 (dolist (item youtube-dl-items)
1105 (let ((vid (youtube-dl-item-vid item))
1106 (running (youtube-dl-item-running item))
1107 (failures (youtube-dl-item-failures item))
1108 (priority (youtube-dl-item-priority item))
1109 (percentage (youtube-dl-item-percentage item))
1110 (download-rate (youtube-dl-item-download-rate item))
1111 (paused-p (youtube-dl-item-paused-p item))
1112 (slow-p (youtube-dl-item-slow-p item))
1113 (rate-limit (youtube-dl-item-rate-limit item))
1114 (total-size (youtube-dl-item-total-size item))
1115 (eta (youtube-dl-item-eta item))
1116 (type (youtube-dl-item-type item))
1117 (title (youtube-dl-item-title item))
1118 (url (youtube-dl-item-url item)))
1119 (insert
1120 (propertize
1121 (format (concat youtube-dl-list--format "\n")
1122 ;; space
1124 ;; (propertize " " 'display '((space :align-to 0)))
1125 ;; vid
1126 (if running ; update `:running' property every time process update.
1127 (propertize vid 'face 'default)
1128 (propertize vid 'face 'youtube-dl-pause))
1129 ;; progress (percentage download-rate)
1130 (if (and percentage download-rate)
1131 (propertize (format "⦼%-5.5s ⇩%-17.17s " percentage download-rate) 'face 'youtube-dl-active)
1132 (propertize (format "⦼%-5.5s ⇩%-17.17s " percentage download-rate) 'face 'youtube-dl-pause))
1133 ;; size
1134 (or (propertize (format "%s" total-size) 'face 'youtube-dl-pause) "-")
1135 ;; eta
1136 (or (propertize (format "%s" eta) 'face 'youtube-dl-pause) "?")
1137 ;; failure
1138 (if (= failures 0)
1140 (propertize (format " [%d] " failures) 'face 'youtube-dl-failure))
1141 ;; priority
1142 ;; (if (= priority 0)
1143 ;; ""
1144 ;; (propertize (format "%+d " priority) 'face 'youtube-dl-priority))
1145 ;; status
1146 (concat
1147 (if slow-p (propertize "SLOW" 'face 'youtube-dl-slow))
1148 (if rate-limit (propertize (format "≤ %s" rate-limit) 'face 'youtube-dl-slow))
1149 (if paused-p (propertize "PAUSE" 'face 'youtube-dl-pause)))
1150 ;; title
1151 (or (propertize title 'face (youtube-dl--face-for-type type)) ""))
1152 'url url)))))))
1154 ;;;###autoload
1155 (defun youtube-dl-list ()
1156 "Display a list of all videos queued for download."
1157 (interactive)
1158 (youtube-dl--fill-listing)
1159 ;; set timer to auto close `youtube-dl--list-buffer' "*youtube-dl list*" buffer after finished all downloading.
1160 (with-current-buffer (youtube-dl--list-buffer)
1161 (unless (timerp youtube-dl-list--auto-close-window-timer)
1162 (setq-local youtube-dl-list--auto-close-window-timer
1163 (run-with-timer 0 20 #'youtube-dl-list--auto-close-window)))
1164 ;; jump to beginning of buffer.
1165 (goto-char (point-min)))
1166 (display-buffer (youtube-dl--list-buffer)))
1168 ;;;###autoload
1169 (defun youtube-dl-download-audio (url)
1170 "Download audio format of URL."
1171 (interactive
1172 (list (read-from-minibuffer
1173 "URL: " (or (thing-at-point 'url)
1174 (when interprogram-paste-function
1175 (funcall interprogram-paste-function))))))
1176 (let ((youtube-dl-extra-arguments (append youtube-dl-extra-arguments '("-x" "--audio-format" "best"))))
1177 (youtube-dl url)))
1179 ;;;###autoload
1180 (defun youtube-dl-download-subtitle (url)
1181 "Download video subtitle of URL."
1182 (interactive
1183 (list (read-from-minibuffer
1184 "URL: " (or (thing-at-point 'url)
1185 (when interprogram-paste-function
1186 (funcall interprogram-paste-function))))))
1187 (let ((youtube-dl-extra-arguments (append youtube-dl-extra-arguments
1188 '("--skip-download"
1189 ;; "--write-auto-sub" ; YouTube only, for auto-generated subtitle.
1190 "--write-sub"
1191 "--sub-lang" "en,zh-Hans"
1192 "--sub-format" "ass/srt/best" ; ass/srt/best, srt, vtt, ttml, srv3, srv2, srv1
1193 ))))
1194 (youtube-dl url)))
1196 ;;;###autoload
1197 (defun youtube-dl-download-thumbnail (url)
1198 "Download video thumbnail of URL."
1199 (interactive
1200 (list (read-from-minibuffer
1201 "URL: " (or (thing-at-point 'url)
1202 (when interprogram-paste-function
1203 (funcall interprogram-paste-function))))))
1204 (let ((youtube-dl-extra-arguments (append youtube-dl-extra-arguments
1205 '("--skip-download"
1206 "--write-thumbnail"))))
1207 (youtube-dl url)))
1209 ;;; transient interface
1210 (define-key youtube-dl-list-mode-map (kbd "C-c C-c") 'youtube-dl-list-dispatch)
1213 (provide 'youtube-dl)
1215 ;;; youtube-dl.el ends here