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