Fix process downloaded 100% finished logic
[youtube-dl.el.git] / youtube-dl.el
blob9a04acfa7554ef0b66e96e17106c5ed5e672fe5e
1 ;;; youtube-dl.el --- manages a youtube-dl queue -*- lexical-binding: t; -*-
3 ;; This is free and unencumbered software released into the public domain.
5 ;; 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 ;;; Commentary:
12 ;; This package manages a video download queue for the youtube-dl
13 ;; command line program, which serves as its back end. It manages a
14 ;; single youtube-dl subprocess to download one video at a time. New
15 ;; videos can be queued at any time.
17 ;; The `youtube-dl' command queues a URL for download. Failures are
18 ;; retried up to `youtube-dl-max-failures'. Items can be paused or set
19 ;; to be downloaded at a slower download rate (`youtube-dl-slow-rate-limit').
21 ;; The `youtube-dl-download-playlist' command queues an entire playlist, just
22 ;; as if you had individually queued each video on the playlist.
24 ;; The `youtube-dl-list' command displays a list of all active video
25 ;; downloads. From this list, items under point can be canceled (d),
26 ;; paused (p), slowed (s), and have its priority adjusted ([ and ]).
28 ;;; Code:
30 (require 'json)
31 (require 'cl-lib)
32 (require 'hl-line)
33 (require 'notifications)
35 (defgroup youtube-dl ()
36 "Download queue for the youtube-dl command line program."
37 :group 'external)
39 (defcustom youtube-dl-directory "~/Downloads/"
40 "Directory in which to run youtube-dl."
41 :group 'youtube-dl
42 :type 'directory)
44 (defcustom youtube-dl-program
45 ;; "yt-dlp" is a fork of "youtube-dl".
46 (cond
47 ((executable-find "yt-dlp") "yt-dlp")
48 ((executable-find "youtube-dl") "youtube-dl"))
49 "The name of the program invoked for downloading YouTube videos.
50 NOTE: It's specified by program name instead of program path."
51 :type 'string
52 :safe #'stringp
53 :group 'youtube-dl)
55 (defcustom youtube-dl-arguments
56 '("--newline" "--no-mtime" "--restrict-filenames" "--no-warnings")
57 "Arguments to be send to youtube-dl.
58 Instead of --limit-rate use custom option `youtube-dl-slow-rate-limit'."
59 :type '(repeat string)
60 :safe #'listp
61 :group 'youtube-dl)
63 (defcustom youtube-dl-proxy ""
64 "Specify the proxy for youtube-dl command.
65 For example:
67 127.0.0.1:8118
68 socks5://127.0.0.1:1086"
69 :type 'string
70 :safe #'stringp
71 :group 'youtube-dl)
73 (defcustom youtube-dl-proxy-url-list '()
74 "A list of URL domains which should use proxy for youtube-dl."
75 :type 'list
76 :safe #'listp
77 :group 'youtube-dl)
79 (defcustom youtube-dl-auto-show-list t
80 "Auto show youtube-dl-list buffer."
81 :type 'boolean
82 :safe #'booleanp
83 :group 'youtube-dl)
85 (defcustom youtube-dl-process-model 'multiple-processes
86 "Determine youtube-dl.el downloading process model."
87 :safe #'symbolp
88 :type '(choice (symbol :tag "single process only" 'single-process)
89 (symbol :tag "multiple processes" 'multiple-processes)))
91 (defcustom youtube-dl-max-failures 8
92 "Maximum number of retries for a single video."
93 :type 'integer
94 :safe #'numberp
95 :group 'youtube-dl)
97 (defcustom youtube-dl-slow-rate-limit "2M"
98 "Download rate for slow item (corresponding to option --limit-rate)."
99 :type 'string
100 :safe #'stringp
101 :group 'youtube-dl)
103 (defcustom youtube-dl-notify (cl-case system-type
104 (gnu/linux t)
105 (darwin nil)
106 (windows-nt nil))
107 "Whether raise notifications."
108 :type 'boolean
109 :safe #'booleanp
110 :group 'youtube-dl)
112 (defcustom youtube-dl-destination-illegal-filename-chars-regex "[#$%+!<>&*|{}?=@:/\\\"'`]"
113 "Specify the regex of invalid destination filename.
114 This will avoid youtube-dl can't save invalid filename caused error.
115 https://www.mtu.edu/umc/services/websites/writing/characters-avoid/"
116 :type 'string
117 :safe #'stringp
118 :group 'youtube-dl)
120 ;;; TEST:
121 ;; (replace-regexp-in-string
122 ;; youtube-dl-destination-illegal-filename-chars-regex "-"
123 ;; "[#$%+!<>&*|{}?=@:/\\\"'`]")
125 (defface youtube-dl-active
126 '((t :inherit font-lock-function-name-face :foreground "forest green"))
127 "Face for highlighting the active download item."
128 :group 'youtube-dl)
130 (defface youtube-dl-slow
131 '((t :inherit font-lock-variable-name-face :foreground "orange"))
132 "Face for highlighting the slow (S) tag."
133 :group 'youtube-dl)
135 (defface youtube-dl-pause
136 '((t :inherit font-lock-type-face :foreground "gray"))
137 "Face for highlighting the pause (P) tag."
138 :group 'youtube-dl)
140 (defface youtube-dl-priority
141 '((t :inherit font-lock-keyword-face :foreground "light blue"))
142 "Face for highlighting the priority marker."
143 :group 'youtube-dl)
145 (defface youtube-dl-failure
146 '((t :inherit font-lock-warning-face :foreground "red"))
147 "Face for highlighting the failure marker."
148 :group 'youtube-dl)
150 (defvar-local youtube-dl--log-item nil
151 "Item currently being displayed in the log buffer.")
153 (cl-defstruct (youtube-dl-item (:constructor youtube-dl-item--create)
154 (:copier nil))
155 "Represents a single video to be downloaded with youtube-dl."
156 url ; Video URL (string)
157 vid ; Video ID (integer)
158 directory ; Working directory for youtube-dl (string or nil)
159 destination ; Preferred destination file (string or nil)
160 running ; Status indicator of process (boolean)
161 failures ; Number of video download failures (integer)
162 priority ; Download priority (integer)
163 title ; Listing display title (string or nil)
164 percentage ; Current download percentage (string or nil)
165 total-size ; Total download size (string or nil)
166 log ; All program output (list of strings)
167 log-end ; Last log item (list of strings)
168 paused-p ; Non-nil if download is paused (boolean)
169 slow-p ; Non-nil if download should be rate limited (boolean)
170 rate-limit ; Download rate limit (e.g. 50K or 2M)
171 buffer ; the process buffer name
172 process ; the process object
173 download-rate ; Download rate in bytes per second (e.g. 50K or 2M)
174 eta ; ETA time (e.g. 01:01:57)
177 (defvar youtube-dl-items ()
178 "List of all items still to be downloaded.")
180 (defvar youtube-dl-process nil
181 "The currently active youtube-dl process.")
183 (defun youtube-dl--process-buffer-name (vid)
184 "Return the process buffer name which constructed with VID."
185 (format " *youtube-dl vid:%s*" vid))
187 (defun youtube-dl--proxy-append (url &optional option value)
188 "Decide whether append proxy option in youtube-dl command based on URL."
189 (let ((domain (url-domain (url-generic-parse-url url))))
190 (if (and (member domain youtube-dl-proxy-url-list) ; <-- whether toggle proxy?
191 (not (string-empty-p youtube-dl-proxy)))
192 (if option ; <-- whether has command-line option?
193 (list "--proxy" youtube-dl-proxy option value)
194 (list "--proxy" youtube-dl-proxy))
195 (if option ; <-- return original arguments for no proxy
196 (list option value)
197 nil ; <-- return nothing for url no need proxy
198 ))))
200 (defun youtube-dl--next ()
201 "Returns the next item to be downloaded."
202 (let (best best-score)
203 (dolist (item youtube-dl-items best)
204 (let* ((failures (youtube-dl-item-failures item))
205 (priority (youtube-dl-item-priority item))
206 (paused-p (youtube-dl-item-paused-p item))
207 (score (- priority failures)))
208 (when (and (not paused-p)
209 (< failures youtube-dl-max-failures))
210 (cond ((null best)
211 (setf best item
212 best-score score))
213 ((> score best-score)
214 (setf best item
215 best-score score))))))))
217 (defun youtube-dl--current ()
218 "Return the item currently being downloaded."
219 (when youtube-dl-process
220 (plist-get (process-plist youtube-dl-process) :item)))
222 (defun youtube-dl--remove (item)
223 "Remove ITEM from the queue and kill process."
224 (let ((proc (youtube-dl-item-process item)))
225 (when (process-live-p proc) ; `kill-process' only when `proc' is still alive.
226 (kill-process proc)))
227 (setf youtube-dl-items (cl-delete item youtube-dl-items)))
229 (defun youtube-dl--add (item)
230 "Add ITEM to the queue."
231 (setf youtube-dl-items (nconc youtube-dl-items (list item))))
233 (defvar youtube-dl--timer-item nil
234 "A global variable for passing ITEM into `youtube-dl--run' in `run-with-timer'.")
236 (defun youtube-dl--sentinel (proc status)
237 (when-let ((item (plist-get (process-plist proc) :item)))
238 ;; (setf youtube-dl-process proc) ; dont' need to set current process.
239 (cond
240 ;; process status is finished.
241 ((equal status "finished\n")
242 ;; mark item structure property `:running' to `nil'.
243 (setf (youtube-dl-item-running item) nil)
244 (youtube-dl--remove item)
245 (when youtube-dl-notify
246 (notifications-notify :title "youtube-dl.el" :body "Download finished.")))
247 ;; detect whether process is in "pause" process status?
248 ((youtube-dl-item-paused-p item)
249 ;; mark item structure property `:running' to `nil'.
250 (setf (youtube-dl-item-running item) nil)
251 (message "[youtube-dl] process %s is paused." proc))
252 ;; when process downloading failed, then retry downloading.
253 ((equal status "killed: 9\n")
254 ;; mark item structure property `:running' to `nil'.
255 (setf (youtube-dl-item-running item) nil)
256 (message "[youtube-dl] process %s is killed." proc))
258 ;; mark item structure property `:running' to `nil'.
259 (setf (youtube-dl-item-running item) nil)
260 (cl-incf (youtube-dl-item-failures item))
261 ;; record log output to process buffer
262 (let ((buf (youtube-dl--log-buffer item))
263 (inhibit-read-only t))
264 (with-current-buffer buf
265 (print status buf)
266 (print (process-plist proc) buf)))
267 ;; re-run process
268 (if (<= (youtube-dl-item-failures item) 10)
269 (youtube-dl--run)
270 (setq youtube-dl--timer-item item)
271 (run-with-timer (* 60 5) nil #'youtube-dl--run)
272 (message "[youtube-dl] delay process %s" proc))))))
274 (defun youtube-dl--progress (output)
275 "Return the download progress for the given output.
276 Progress lines that straddle output chunks are lost. That's fine
277 since this is just used for display purposes.
279 Return list: (\"34.6%\" \"~2.61GiB\" \"929.50KiB/s\") "
280 (let ((start 0)
281 (progress nil))
282 (cond
283 ;; [download] 34.6% of ~2.61GiB at 929.50KiB/s ETA 01:01:57
284 ;; +---+ +------+ +---------+ +------+
285 ((string-equal youtube-dl-program "youtube-dl")
286 (while (string-match "\\[download\\] +\\([^ ]+%\\) +of *~? *\\([^ ]+\\) +at +\\([^ ]+\\) +ETA +\\([^ \n]+\\)" output start)
287 (let ((percent (match-string 1 output))
288 (total-size (match-string 2 output))
289 (download-rate (match-string 3 output))
290 (eta (match-string 4 output)))
291 (setf progress (list percent total-size download-rate eta)
292 start (match-end 0)))))
293 ((string-equal youtube-dl-program "yt-dlp")
294 ;; The output has non-ASCII shell color codes. Disable it with command-line option "--no-colors".
295 ;; [download] 0.4% of ~ 314.23MiB at 7.54KiB/s ETA 11:48:26 (frag 0/216)
296 ;; +--+ +-------+ +-------+ +------+
297 (while (string-match "\\[download\\] +\\([^ ]+%\\) +of *~? *\\([^ ]+\\) +at +\\([^ ]+\\) +ETA +\\([^ \n]+\\)" output start)
298 (let ((percent (match-string 1 output))
299 (total-size (match-string 2 output))
300 (download-rate (match-string 3 output))
301 (eta (match-string 4 output)))
302 ;; filter out not correct matched data.
303 ;; DEBUG:
304 ;; (message "[DEBUG] [youtube-dl] download-rate regexp matched: >>> %s <<<" download-rate)
305 ;; (message "[DEBUG] [youtube-dl] eta regexp matched: >>> %s <<<" eta)
306 (unless (or (string-equal-ignore-case total-size "Unknown.*")
307 (string-equal-ignore-case eta "Unknown.*") ; [download] 0.0% of 2.14MiB at Unknown B/s ETA Unknown
309 (setf progress (list percent total-size download-rate eta)
310 start (match-end 0)))))))
311 ;; DEBUG:
312 ;; (when progress
313 ;; (cl-destructuring-bind (percentage total-size download-rate eta) progress
314 ;; (message "[DEBUG] [youtube-dl] progress: percent: %s, total-size: %s, speed: %s, eta: %s."
315 ;; percentage total-size download-rate eta)))
316 progress))
318 (defun youtube-dl--get-destination (url)
319 "Return the destination filename for the given `URL' (if any).
320 The destination filename may potentially straddle two output
321 chunks, but this is incredibly unlikely. It's only used for
322 display purposes anyway.
324 $ youtube-dl --get-filename <URL>"
325 (message "[youtube-dl] getting video destination.")
326 (let* ((get-destination-buffer " *youtube-dl-get-destination*")
327 (get-destination-error-buffer " *youtube-dl-get-destination error*")
328 (destination))
329 ;; clear destination buffer content to clear destination history.
330 (progn
331 (when (buffer-live-p (get-buffer get-destination-buffer))
332 (with-current-buffer get-destination-buffer (erase-buffer)))
333 (when (buffer-live-p (get-buffer get-destination-error-buffer))
334 (with-current-buffer get-destination-error-buffer (erase-buffer))))
335 ;; retrieve destination
336 (make-process
337 :command (list youtube-dl-program "--get-filename" url)
338 :sentinel (lambda (proc event)
339 (if (string= event "finished\n")
340 (setq destination
341 (replace-regexp-in-string
342 "\\ +\\[.*\\]" "" ; delete trailing [...] in filename.
343 (replace-regexp-in-string
344 "\n" "" ; delete trailing "\n" character.
345 (with-current-buffer get-destination-buffer (buffer-string)))))
346 (let* ((buffer-str (with-current-buffer get-destination-error-buffer (buffer-string)))
347 (match-regex "\\(ERROR\\|Error\\): \\(.*\\)")
348 (error-p (string-match-p match-regex buffer-str))
349 (matched-str (when (string-match match-regex buffer-str) (match-string 2 buffer-str))))
350 (if error-p
351 (progn
352 (message "[youtube-dl] `youtube-dl--get-destination' error!\n%s" matched-str)
353 ;; TODO:
354 ;; (funcall 'youtube-dl--get-destination url)
356 (progn
357 (message "[youtube-dl] get output filename success.")
358 ;; (kill-process proc)
359 ;; (kill-buffer (process-buffer proc))
360 )))))
361 :name "youtube-dl-get-destination"
362 :buffer get-destination-buffer
363 :stderr get-destination-error-buffer)
365 (setq destination-not-returned t)
366 (dotimes (i 10)
367 (when destination-not-returned
368 (sleep-for 1)
369 (if destination (setq destination-not-returned nil))))
371 ;; Prompt for user the destination interactively.
372 (unless destination
373 (setq destination (substring-no-properties (read-string "[youtube-dl] input destination: "))))
375 (when destination
376 (kill-new destination) ; copy to Emacs kill-ring for later operation.
377 (message "[youtube-dl] destination (filename.mp4): %s" destination)
378 destination)))
380 (defun youtube-dl--filter (proc output)
381 (if (plist-get (process-plist proc) :item)
382 (let* ((item (plist-get (process-plist proc) :item))
383 (progress (youtube-dl--progress output))
384 (destination (youtube-dl-item-destination item)))
385 ;; Append to program log.
386 (let ((logged (list output)))
387 (if (and (youtube-dl-item-log item) (youtube-dl-item-log-end item)) ; make sure not nil.
388 (setf (cdr (youtube-dl-item-log-end item)) logged
389 (youtube-dl-item-log-end item) logged)
390 (setf (youtube-dl-item-log item) logged
391 (youtube-dl-item-log-end item) logged)))
392 ;; Update progress information.
393 (when progress
394 (cl-destructuring-bind (percentage total-size download-rate eta) progress
395 (setf (youtube-dl-item-percentage item) percentage
396 (youtube-dl-item-total-size item) total-size
397 (youtube-dl-item-download-rate item) download-rate
398 (youtube-dl-item-eta item) eta)))
399 ;; monitor process output whether error occurred?
400 (let* ((item-log-end (youtube-dl-item-log-end item))
401 (log-end-str (cond
402 ((stringp item-log-end) item-log-end)
403 ((listp item-log-end) (car (last item-log-end)))))
404 (error-hander (lambda () (setf (youtube-dl-item-running item) nil)
405 (message "[youtube-dl] ✖ <proc: %s>, destination: %s"
406 (youtube-dl-item-process item) (youtube-dl-item-destination item))))
407 (running-handler (lambda () (setf (youtube-dl-item-running item) t)))
408 (finished-handler (lambda () (setf (youtube-dl-item-running item) nil)
409 (message "[youtube-dl] ✔ %s downloaded." (youtube-dl-item-destination item)))))
410 (when (stringp log-end-str)
411 (cond
412 ;; progress finished when percentage is 100%.
413 ((or (string-equal (youtube-dl-item-percentage item) "100%")
414 (string-match "\\[download\\]\\ *100%.*" log-end-str)) ; ".*100%.*"
415 (funcall finished-handler))
416 ;; process exited abnormally with code 1
417 ((string-match "exited abnormally with code 1" log-end-str)
418 (funcall error-hander))
419 ;; file already downloaded
420 ((string-match "ERROR: Fixed output name but more than one file to download:.*" log-end-str)
421 ;; (funcall error-hander)
422 (message "[youtube-dl] ERROR: Already has existing downloading process or output file! (%s)" (youtube-dl-item-destination item)))
423 ;; any other errors
424 ((string-match "ERROR:.*" log-end-str)
425 (funcall error-hander))
426 (t (funcall running-handler)))))
427 ;; DEBUG:
428 ;; (message "[DEBUG] [youtube-dl] destination/title: %s" destination)
429 ;; Set item title to destination if it's empty.
430 (unless (youtube-dl-item-title item)
431 (setf (youtube-dl-item-title item) destination))
432 ;; write output to process associated buffer.
433 (with-current-buffer (process-buffer proc)
434 (let ((moving (= (point) (process-mark proc)))
435 ;; avoid process buffer read-only issue for `insert'.
436 (inhibit-read-only t))
437 (save-excursion
438 ;; Insert the text, advancing the process marker.
439 (goto-char (process-mark proc))
440 (insert output)
441 (set-marker (process-mark proc) (point)))
442 (if moving (goto-char (process-mark proc)))))
443 (youtube-dl--redisplay))))
445 (defun youtube-dl--construct-command (item)
446 "Construct full command of youtube-dl with arguments from ITEM."
447 (let ((url (youtube-dl-item-url item))
448 (slow-p (youtube-dl-item-slow-p item))
449 (rate-limit (youtube-dl-item-rate-limit item))
450 (destination (youtube-dl-item-destination item)))
451 (append
452 (list youtube-dl-program "--newline")
453 youtube-dl-arguments
454 ;; Disable non-ASCII shell color codes in output.
455 (cond
456 ((string-equal youtube-dl-program "youtube-dl") '("--no-color"))
457 ((string-equal youtube-dl-program "yt-dlp") '("--no-colors")))
458 (youtube-dl--proxy-append url)
459 (when slow-p `("--rate-limit" ,rate-limit))
460 (when destination `("--output" ,destination))
461 `("--" ,url))))
463 (defun youtube-dl--run-single-process ()
464 "Start youtube-dl downloading in single process."
465 (let ((item (youtube-dl--next))
466 (current-item (youtube-dl--current)))
467 (if (eq item current-item)
468 (youtube-dl--redisplay) ; do nothing, just display the youtube-dl-list buffer.
469 (if youtube-dl-process
470 (progn
471 ;; Switch to higher priority job, but offset error count first.
472 (cl-decf (youtube-dl-item-failures current-item))
473 (kill-process youtube-dl-process)) ; sentinel will clean up
474 ;; No subprocess running, start a one.
475 (let* ((url (substring-no-properties (youtube-dl-item-url item)))
476 (directory (youtube-dl-item-directory item))
477 (destination (youtube-dl-item-destination item))
478 (vid (youtube-dl-item-vid item))
479 (proc-buffer-name (youtube-dl--process-buffer-name vid))
480 (default-directory
481 (if directory
482 (concat (directory-file-name directory) "/")
483 (concat (directory-file-name youtube-dl-directory) "/")))
484 (_ (mkdir default-directory t))
485 (slow-p (youtube-dl-item-slow-p item))
486 (rate-limit (or (youtube-dl-item-rate-limit item) youtube-dl-slow-rate-limit))
487 (proc (make-process
488 :name proc-buffer-name
489 :command (let ((command (youtube-dl--construct-command item)))
490 ;; Insert complete command into process buffer for debugging.
491 (with-current-buffer (get-buffer-create proc-buffer-name)
492 (insert (format "Command: %s" command)))
493 command)
494 :sentinel #'youtube-dl--sentinel
495 :filter #'youtube-dl--filter
496 :buffer proc-buffer-name)))
497 (set-process-plist proc (list :item item))
498 (setf youtube-dl-process proc)
499 ;; mark item structure property `:running' to `t'.
500 (setf (youtube-dl-item-running item) t))))
501 (youtube-dl--redisplay)))
503 (defun youtube-dl--run-multiple-processes (&optional item)
504 "Start youtube-dl downloading in multiple processes."
505 (let* ((item (or item
506 youtube-dl--timer-item ; use item from `run-with-timer'.
507 (with-current-buffer (youtube-dl--list-buffer)
508 (nth (1- (line-number-at-pos)) youtube-dl-items))))
509 (current-item (youtube-dl--current)) ; `youtube-dl-process' currently actived process.
510 (proc (youtube-dl-item-process item)))
511 (unless (and item (youtube-dl-item-running item)) ; whether item structure property `:running' is `t'?
512 (let* ((url (substring-no-properties (youtube-dl-item-url item)))
513 (directory (youtube-dl-item-directory item))
514 (destination (youtube-dl-item-destination item))
515 (vid (youtube-dl-item-vid item))
516 (proc-buffer-name (youtube-dl--process-buffer-name vid))
517 (default-directory
518 (if directory
519 (concat (directory-file-name directory) "/")
520 (concat (directory-file-name youtube-dl-directory) "/")))
521 (_ (mkdir default-directory t))
522 (slow-p (youtube-dl-item-slow-p item))
523 (rate-limit (or (youtube-dl-item-rate-limit item) youtube-dl-slow-rate-limit))
524 (failures (youtube-dl-item-failures item))
525 (proc (when (<= failures 10) ; re-run process only when failures <= 10.
526 (make-process
527 :name proc-buffer-name
528 :command (let ((command (youtube-dl--construct-command item)))
529 ;; Insert complete command into process buffer for debugging.
530 (let ((inhibit-read-only t))
531 (with-current-buffer (get-buffer-create proc-buffer-name)
532 (insert (format "Command: %s" command))))
533 command)
534 :sentinel #'youtube-dl--sentinel
535 :filter #'youtube-dl--filter
536 :buffer proc-buffer-name))))
537 ;; clear temporary item variable `youtube-dl--timer-item'.
538 (setq youtube-dl--timer-item nil)
539 (when (processp proc)
540 ;; set process property list.
541 (set-process-plist proc (list :item item))
542 ;; assign `proc' object to item slot `:process'.
543 (setf (youtube-dl-item-process item) proc)
544 ;; set current youtube-dl process variable.
545 (setf youtube-dl-process proc)
546 ;; mark item structure property `:running' to `t'.
547 (setf (youtube-dl-item-running item) t))))
548 (youtube-dl--redisplay)))
550 (defun youtube-dl--run (&optional item)
551 "Start youtube-dl downloading."
552 ;; if single process model, then start download next item.
553 ;; if multiple processes model, then don't start next item `youtube-dl--run'.
554 (cl-case youtube-dl-process-model
555 (single-process (youtube-dl--run-single-process item))
556 (multiple-processes (youtube-dl--run-multiple-processes item))))
558 (defun youtube-dl--get-vid (url)
559 "Get video `URL' video vid with youtube-dl option `--get-id'."
560 (message "[youtube-dl] getting video vid.")
561 (let* ((parsed-url (url-generic-parse-url url))
562 (domain (url-domain parsed-url))
563 (parameters (url-filename parsed-url)))
564 ;; URL domain matching with regexp to extract vid.
565 (pcase domain
566 ("bilibili.com" ; "/video/BV1B8411L7te/", "/video/BV1uj411N7cp?spm_id_from=..0.0"
567 (when (string-match "/video/\\([^/?&]*\\)" parameters)
568 (match-string 1 parameters)))
569 ("pornhub.com" ; "/view_video.php?viewkey=ph6238f9c7cc9e2"
570 (when (string-match "/view_video\\.php\\?viewkey=\\([^/?&]*\\)" parameters)
571 (match-string 1 parameters)))
572 ("youtube.com" ; "/watch?v=q-iLEteyeUQ", "/watch?v=48JlgiBpw_I&t=311s"
573 (when (string-match "/watch\\?v=\\([^/?&]*\\)" parameters)
574 (match-string 1 parameters)))
575 (_ (progn
576 (let* ((output (with-temp-buffer
577 (apply #'call-process
578 youtube-dl-program
579 nil t nil
580 (youtube-dl--proxy-append url "--get-id" url))
581 (buffer-string)))
582 (output-lines-list (string-lines output))
583 (error-p (string-match-p "ERROR" output)))
584 (cond
585 ;; ("VrAfJvZGGXE" "")
586 ;; TEST: (youtube-dl--get-vid "https://www.youtube.com/watch?v=VROjLiq9LeQ")
587 ((length= output-lines-list 2) (car output-lines-list))
588 ;; TEST: (youtube-dl--get-vid "https://hanime1.me/watch?v=22454")
589 ;; ("WARNING: Falling back on generic information extractor." "watch?v=22454" "")
590 ((length= output-lines-list 3) (car (last output-lines-list 1)))
591 ;; TEST: (youtube-dl--get-vid "https://www.pornhub.com/view_video.php?viewkey=ph637ed6daf1795")
592 ;; 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.
593 ((string-match-p "WARNING" (car output-lines-list))
594 (car (last output-lines-list 1)))
595 ;; get vid failed, use URL string regex matching instead.
596 (t (progn
597 (when error-p
598 ;; WARNING: Could not send HEAD request to https://hanime1.me/watch?v=22709:
599 ;; HTTP Error 503: Service Temporarily Unavailable
600 ;; ERROR: Unable to download webpage: HTTP Error 503: Service Temporarily
601 ;; Unavailable (caused by <HTTPError 503: 'Service Temporarily
602 ;; Unavailable'>); please report this issue on https://yt-dl.org/bug . Make
603 ;; sure you are using the latest version; see https://yt-dl.org/update on
604 ;; how to update. Be sure to call youtube-dl with the --verbose flag and
605 ;; include its complete output.
607 ;; WARNING: unable to extract view count; please report this
608 ;; issue on https://yt-dl.org/bug . Make sure you are using
609 ;; the latest version; see https://yt-dl.org/update on how
610 ;; to update. Be sure to call youtube-dl with the --verbose
611 ;; flag and include its complete output.
612 (error (format "[youtube-dl] `youtube-dl--get-vid' retrive video vid error!\n%s" output)))
613 parameters)))))))))
615 ;;; TEST:
616 ;; (let ((parameters "/video/BV1B8411L7te/"))
617 ;; (when (string-match "/video/\\([^/]*\\)" parameters)
618 ;; (match-string 1 parameters)))
620 ;; (youtube-dl--get-vid "https://www.youtube.com/watch?v=VROjLiq9LeQ")
621 ;; (youtube-dl--get-vid "https://www.pornhub.com/view_video.php?viewkey=ph637ed6daf1795")
623 ;;;###autoload
624 (cl-defun youtube-dl
625 (url &key title (priority 0) directory destination paused slow)
626 "Queues URL for download using youtube-dl, returning the new item.
627 By default, it downloads to ~/Downloads/."
628 (interactive
629 (list (substring-no-properties
630 (read-from-minibuffer
631 "URL: " (or (thing-at-point 'url)
632 (when interprogram-paste-function
633 (funcall interprogram-paste-function)))))))
634 ;; remove this ID failure only on youtube.com, use URL as ID. or use youtube-dl extracted title, or hash on URL.
635 (let* ((vid (youtube-dl--get-vid url))
636 (destination (replace-regexp-in-string ; replace illegal filename characters in `destination'.
637 youtube-dl-destination-illegal-filename-chars-regex "-"
638 (or (youtube-dl--get-destination url)
639 destination)))
640 (title (or (replace-regexp-in-string (format "-%s.*" vid) "" destination)
641 (car (split-string destination "-"))))
642 (proc-buffer-name (youtube-dl--process-buffer-name vid))
643 (full-dir (expand-file-name (or directory "") youtube-dl-directory))
644 (item (youtube-dl-item--create :url url
645 :vid vid
646 :failures 0
647 :priority priority
648 :paused-p paused
649 :slow-p slow
650 :rate-limit nil
651 :directory full-dir
652 :destination destination
653 :title title
654 :buffer proc-buffer-name
655 :process nil)))
656 (prog1 item
657 (unless (youtube-dl-item-running item)
658 (youtube-dl--add item) ; Add ITEM to the queue.
659 (youtube-dl--run item) ; Start ITEM in the queue.
660 (when youtube-dl-auto-show-list
661 (youtube-dl-list))))))
663 (defalias 'youtube-dl-download-video 'youtube-dl)
665 (defun youtube-dl--playlist-list (playlist)
666 "For each video, return one plist with :index, :vid, and :title."
667 (with-temp-buffer
668 (when (zerop (call-process youtube-dl-program nil t nil
669 "--ignore-config"
670 "--dump-json"
671 "--flat-playlist"
672 playlist))
673 (goto-char (point-min))
674 (cl-loop with json-object-type = 'plist
675 for index upfrom 1
676 for video = (ignore-errors (json-read))
677 while video
678 collect (list :index index
679 :vid (plist-get video :vid)
680 :title (plist-get video :title))))))
682 (defun youtube-dl--playlist-reverse (list)
683 "Return a copy of LIST with the indexes reversed."
684 (let ((max (cl-loop for entry in list
685 maximize (plist-get entry :index))))
686 (cl-loop for entry in list
687 for index = (plist-get entry :index)
688 for copy = (copy-sequence entry)
689 collect (plist-put copy :index (- (1+ max) index)))))
691 (defun youtube-dl--playlist-cutoff (list n)
692 "Return a sorted copy of LIST with all items except where :index < N."
693 (let ((key (lambda (v) (plist-get v :index)))
694 (filter (lambda (v) (< (plist-get v :index) n)))
695 (copy (copy-sequence list)))
696 (cl-delete-if filter (cl-stable-sort copy #'< :key key))))
698 ;;;###autoload
699 (cl-defun youtube-dl-download-playlist
700 (url &key directory (first 1) paused (priority 0) reverse slow)
701 "Add entire playlist to download queue, with index prefixes.
703 :directory PATH -- Destination directory for all videos.
705 :first INDEX -- Start downloading from a given one-based index.
707 :paused BOOL -- Start all download entries as paused.
709 :priority PRIORITY -- Use this priority for all download entries.
711 :reverse BOOL -- Reverse the video numbering, solving the problem
712 of reversed playlists.
714 :slow BOOL -- Start all download entries in slow mode."
715 (interactive
716 (list (read-from-minibuffer
717 "URL: "
718 (when interprogram-paste-function
719 (funcall interprogram-paste-function)))))
720 (message "[youtube-dl] fetching playlist ...")
721 (let ((videos (youtube-dl--playlist-list url)))
722 (if (null videos)
723 (error "Failed to fetch playlist (%s)." url)
724 (let* ((max (cl-loop for entry in videos
725 maximize (plist-get entry :index)))
726 (width (1+ (floor (log max 10))))
727 (prefix-format (format "%%0%dd" width)))
728 (when reverse
729 (setf videos (youtube-dl--playlist-reverse videos)))
730 (dolist (video (youtube-dl--playlist-cutoff videos first))
731 (let* ((index (plist-get video :index))
732 (prefix (format prefix-format index))
733 (title (format "%s-%s" prefix (plist-get video :title)))
734 (dest (format "%s-%s" prefix "%(title)s-%(id)s.%(ext)s")))
735 (youtube-dl (plist-get video :url)
736 :title title
737 :priority priority
738 :directory directory
739 :destination dest
740 :paused paused
741 :slow slow)))))))
743 ;; List user interface:
745 ;;; refresh youtube-dl-list buffer (2).
746 (defun youtube-dl-list-redisplay ()
747 "Immediately redraw the queue list buffer."
748 (interactive)
749 (with-current-buffer (youtube-dl--list-buffer)
750 (let ((save-point (point))
751 (window (get-buffer-window (current-buffer))))
752 (youtube-dl--fill-listing)
753 (goto-char save-point)
754 (when window
755 (set-window-point window save-point))
756 (when hl-line-mode
757 (hl-line-highlight)))))
759 ;;; refresh youtube-dl-list buffer (1).
760 (defun youtube-dl--redisplay ()
761 "Redraw the queue list buffer only if visible."
762 (let ((log-buffer (youtube-dl--log-buffer)))
763 (when log-buffer
764 (with-current-buffer log-buffer
765 (let ((inhibit-read-only t)
766 (window (get-buffer-window log-buffer)))
767 (erase-buffer)
768 (mapc #'insert (youtube-dl-item-log youtube-dl--log-item))
769 (when window
770 (set-window-point window (point-max)))))))
771 (when (get-buffer-window (youtube-dl--list-buffer))
772 (youtube-dl-list-redisplay)))
774 (defun youtube-dl-list-log ()
775 "Display the log of the video under point."
776 (interactive)
777 (let* ((n (1- (line-number-at-pos)))
778 (item (nth n youtube-dl-items))
779 (buffer (youtube-dl--log-buffer item)))
780 (when item
781 (display-buffer buffer)
782 (select-window (get-buffer-window buffer))
783 (youtube-dl--redisplay))))
785 (defun youtube-dl-list-kill-log ()
786 "Kill the youtube-dl log buffer."
787 (interactive)
788 (let ((buffer (youtube-dl--log-buffer)))
789 (when buffer
790 (kill-buffer buffer))))
792 (defun youtube-dl-list-yank ()
793 "Copy the URL of the video under point to the clipboard."
794 (interactive)
795 (let* ((n (1- (line-number-at-pos)))
796 (item (nth n youtube-dl-items)))
797 (when item
798 (let ((url (concat "https://youtu.be/" (youtube-dl-item-vid item))))
799 (if (fboundp 'gui-set-selection)
800 (gui-set-selection nil url) ; >= Emacs 25
801 (with-no-warnings
802 (x-set-selection 'PRIMARY url))) ; <= Emacs 24
803 (message "[youtube-dl] yanked %s" url)))))
805 (defun youtube-dl-list-kill ()
806 "Remove the selected item from the queue."
807 (interactive)
808 (when youtube-dl-items ; avoid `youtube-dl-items' is `nil'.
809 (let* ((n (1- (line-number-at-pos)))
810 (item (nth n youtube-dl-items)))
811 (when item
812 (when (= n (1- (length youtube-dl-items)))
813 (forward-line -1))
814 (youtube-dl--remove item))))
815 (youtube-dl-list-redisplay))
817 (defun youtube-dl-list-pause ()
818 "Pause downloading of item under point."
819 (interactive)
820 (let* ((n (1- (line-number-at-pos)))
821 (item (nth n youtube-dl-items))
822 (proc (youtube-dl-item-process item))
823 (paused-p (youtube-dl-item-paused-p item)))
824 (if (and item paused-p)
825 ;; if paused, resume process.
826 (progn
827 (setf (youtube-dl-item-paused-p item) nil)
828 (youtube-dl--run))
829 ;; kill the process, but keep item on list buffer for pause status.
830 (when (process-live-p proc) (kill-process proc))
831 ;; after killed process, also setting item structure status properties.
832 (setf (youtube-dl-item-paused-p item) t)
833 (setf (youtube-dl-item-running item) nil))
834 (youtube-dl-list-redisplay)
835 (message "[youtube-dl] process %s is paused." proc)))
837 (defun youtube-dl-list-resume ()
838 "Resume failure/paused downloading item under point."
839 (interactive)
840 (when-let* ((n (1- (line-number-at-pos)))
841 (item (nth n youtube-dl-items))
842 (proc (youtube-dl-item-process item)))
843 (when item
844 (setf (youtube-dl-item-failures item) 0)
845 (cond
846 ((youtube-dl-item-paused-p item)
847 (setf (youtube-dl-item-paused-p item) nil)
848 (youtube-dl--run))
849 ((not (youtube-dl-item-running item))
850 (youtube-dl--run))
852 (setf (youtube-dl-item-running item) nil)
853 (youtube-dl--run)
854 ;; (user-error (format "[youtube-dl] Can't resume process %s correctly. Try resume again." proc))
855 ))))
856 (youtube-dl-list-redisplay))
858 (defun youtube-dl-list-toggle-slow (item &optional rate-limit)
859 "Set slow rate limit on item under point."
860 (interactive
861 (let* ((n (1- (line-number-at-pos))))
862 (list (nth n youtube-dl-items))))
863 (when item
864 (let ((proc (youtube-dl-item-process item))
865 (slow-p (youtube-dl-item-slow-p item))
866 (rate-limit (substring-no-properties
867 (or rate-limit
868 (completing-read "[youtube-dl] limit download rate (e.g. 100K): "
869 '("20K" "50K" "100K" "200K" "500K" "1M" "2M"))))))
870 ;; restart with a slower download rate.
871 (when (process-live-p proc) (kill-process proc))
872 (setf (youtube-dl-item-slow-p item) (not slow-p))
873 (setf (youtube-dl-item-rate-limit item) rate-limit)
874 (youtube-dl--run)))
875 (youtube-dl-list-redisplay))
877 (defun youtube-dl-list-toggle-slow-all ()
878 "Set slow rate on all items."
879 (interactive)
880 (let* ((count (length youtube-dl-items))
881 (slow-count (cl-count-if #'youtube-dl-item-slow-p youtube-dl-items))
882 (target (< slow-count (- count slow-count)))
883 (rate-limit (completing-read "[youtube-dl] limit download rate (e.g. 100K): "
884 '("20K" "50K" "100K" "200K" "500K" "1M" "2M"))))
885 (dolist (item youtube-dl-items)
886 (youtube-dl-list-toggle-slow item rate-limit)))
887 (youtube-dl--redisplay))
889 (defun youtube-dl-list-priority-modify (delta)
890 "Change priority of item under point by DELTA."
891 (let* ((n (1- (line-number-at-pos)))
892 (item (nth n youtube-dl-items)))
893 (when item
894 (cl-incf (youtube-dl-item-priority item) delta)
895 (youtube-dl--run))))
897 (defun youtube-dl-list-priority-up ()
898 "Decrease priority of item under point."
899 (interactive)
900 (youtube-dl-list-priority-modify 1))
902 (defun youtube-dl-list-priority-down ()
903 "Increase priority of item under point."
904 (interactive)
905 (youtube-dl-list-priority-modify -1))
907 (defvar youtube-dl-list-mode-map
908 (let ((map (make-sparse-keymap)))
909 (prog1 map
910 (define-key map "a" #'youtube-dl)
911 (define-key map "g" #'youtube-dl-list-redisplay)
912 (define-key map "l" #'youtube-dl-list-log)
913 (define-key map "L" #'youtube-dl-list-kill-log)
914 (define-key map "y" #'youtube-dl-list-yank)
915 (define-key map "k" #'youtube-dl-list-kill)
916 (define-key map "p" #'youtube-dl-list-pause)
917 (define-key map "r" #'youtube-dl-list-resume)
918 (define-key map "s" #'youtube-dl-list-toggle-slow)
919 (define-key map "S" #'youtube-dl-list-toggle-slow-all)
920 (define-key map "]" #'youtube-dl-list-priority-up)
921 (define-key map "[" #'youtube-dl-list-priority-down)))
922 "Keymap for `youtube-dl-list-mode'")
924 (defvar-local youtube-dl-list--auto-close-window-timer nil
925 "A timer to auto close youtube-dl list window.")
927 (defun youtube-dl-list--auto-close-window ()
928 "Auto close '*youtube-dl list*' buffer after finished all downloading."
929 (when (equal (length youtube-dl-items) 0)
930 (when-let ((buf (get-buffer-window (youtube-dl--list-buffer))))
931 (delete-window buf)
932 (when (timerp youtube-dl-list--auto-close-window-timer)
933 (cancel-timer youtube-dl-list--auto-close-window-timer)))))
935 (defvar youtube-dl-list--format
936 ;; (percentage download-rate)
937 ;; v
938 ;;space,vid,progress size eta fails
939 ;;| | | | | | status
940 ;;| | | | | | | title
941 ;;v v v v v v v v
942 "%s%-16s %-22.22s %-10.10s %-6.6s %-7.7s %-8.8s %s"
943 "Define the `youtube-dl-list-mode' `header-line-format' and list item format.")
945 (define-derived-mode youtube-dl-list-mode special-mode "youtube-dl"
946 "Major mode for listing the youtube-dl download queue."
947 :group 'youtube-dl
948 (use-local-map youtube-dl-list-mode-map)
949 (hl-line-mode)
950 (setq-local truncate-lines t) ; truncate long line text.
951 (setf header-line-format
952 (propertize
953 (format youtube-dl-list--format
954 (propertize " " 'display '((space :align-to 0))) ; space
955 " vid " "| progress" "| size" "| ETA" "| fails" "| status"
956 (concat "| title " (make-string 100 (string-to-char " "))))
957 'face '(:inverse-video t :extend t))))
959 (defvar youtube-dl--list-buffer-name " *youtube-dl list*"
960 "The buffer name of `youtube-dl-list-mode'.")
962 (defun youtube-dl--list-buffer ()
963 "Returns the queue listing buffer."
964 (if-let ((buf (get-buffer-create youtube-dl--list-buffer-name)))
965 (with-current-buffer buf
966 ;; TODO: use `tabulated-list-mode'.
967 ;; (tabulated-list-mode)
968 (youtube-dl-list-mode)
969 (current-buffer))))
971 (defun youtube-dl--log-buffer (&optional item)
972 "Returns a youtube-dl log buffer for ITEM."
973 (when item
974 (let* ((name (youtube-dl-item-buffer item))
975 (buffer (get-buffer-create name)))
976 (with-current-buffer buffer
977 (unless (eq major-mode 'special-mode)
978 (special-mode))
979 (setf youtube-dl--log-item item)
980 (setf (youtube-dl-item-log item) item)
981 (current-buffer)))))
983 ;;; refresh youtube-dl-list buffer (3).
984 (defun youtube-dl--fill-listing ()
985 "Erase and redraw the queue in the queue listing buffer."
986 (with-current-buffer (youtube-dl--list-buffer)
987 (let* ((inhibit-read-only t)
988 (active (youtube-dl--current)))
989 (erase-buffer)
990 (dolist (item youtube-dl-items)
991 (let ((vid (youtube-dl-item-vid item))
992 (running (youtube-dl-item-running item))
993 (failures (youtube-dl-item-failures item))
994 (priority (youtube-dl-item-priority item))
995 (percentage (youtube-dl-item-percentage item))
996 (download-rate (youtube-dl-item-download-rate item))
997 (paused-p (youtube-dl-item-paused-p item))
998 (slow-p (youtube-dl-item-slow-p item))
999 (rate-limit (youtube-dl-item-rate-limit item))
1000 (total-size (youtube-dl-item-total-size item))
1001 (eta (youtube-dl-item-eta item))
1002 (title (youtube-dl-item-title item))
1003 (url (youtube-dl-item-url item)))
1004 (insert
1005 (propertize
1006 (format (concat youtube-dl-list--format "\n")
1007 ;; space
1009 ;; (propertize " " 'display '((space :align-to 0)))
1010 ;; vid
1011 (if running ; update `:running' property every time process update.
1012 (propertize vid 'face 'default)
1013 (propertize vid 'face 'youtube-dl-pause))
1014 ;; progress (percentage download-rate)
1015 (if (and percentage download-rate)
1016 (propertize (format "⦼%-5.5s ⇩%-17.17s " percentage download-rate) 'face 'youtube-dl-active)
1017 (propertize (format "⦼%-5.5s ⇩%-17.17s " percentage download-rate) 'face 'youtube-dl-pause))
1018 ;; size
1019 (or (propertize (format "%s" total-size) 'face 'youtube-dl-pause) "-")
1020 ;; eta
1021 (or (propertize (format "%s" eta) 'face 'youtube-dl-pause) "?")
1022 ;; failure
1023 (if (= failures 0)
1025 (propertize (format " [%d] " failures) 'face 'youtube-dl-failure))
1026 ;; priority
1027 ;; (if (= priority 0)
1028 ;; ""
1029 ;; (propertize (format "%+d " priority) 'face 'youtube-dl-priority))
1030 ;; status
1031 (concat
1032 (if slow-p (propertize "SLOW" 'face 'youtube-dl-slow))
1033 (if rate-limit (propertize (format "≤ %s" rate-limit) 'face 'youtube-dl-slow))
1034 (if paused-p (propertize "PAUSE" 'face 'youtube-dl-pause)))
1035 ;; title
1036 (or title ""))
1037 'url url)))))))
1039 ;;;###autoload
1040 (defun youtube-dl-list ()
1041 "Display a list of all videos queued for download."
1042 (interactive)
1043 (youtube-dl--fill-listing)
1044 ;; set timer to auto close `youtube-dl--list-buffer' "*youtube-dl list*" buffer after finished all downloading.
1045 (with-current-buffer (youtube-dl--list-buffer)
1046 (unless (timerp youtube-dl-list--auto-close-window-timer)
1047 (setq-local youtube-dl-list--auto-close-window-timer
1048 (run-with-timer 0 (* 60 2) #'youtube-dl-list--auto-close-window)))
1049 ;; jump to beginning of buffer.
1050 (goto-char (point-min)))
1051 (display-buffer (youtube-dl--list-buffer)))
1053 ;;;###autoload
1054 (defun youtube-dl-download-audio (url)
1055 "Download audio format of URL."
1056 (interactive
1057 (list (read-from-minibuffer
1058 "URL: " (or (thing-at-point 'url)
1059 (when interprogram-paste-function
1060 (funcall interprogram-paste-function))))))
1061 (let ((youtube-dl-arguments (append youtube-dl-arguments '("-x" "--audio-format" "best"))))
1062 (youtube-dl url)))
1064 ;;;###autoload
1065 (defun youtube-dl-download-subtitle (url)
1066 "Download video subtitle of URL."
1067 (interactive
1068 (list (read-from-minibuffer
1069 "URL: " (or (thing-at-point 'url)
1070 (when interprogram-paste-function
1071 (funcall interprogram-paste-function))))))
1072 (let ((youtube-dl-arguments (append youtube-dl-arguments
1073 '("--skip-download" "--no-warnings"
1074 ;; "--write-auto-sub" ; YouTube only, for auto-generated subtitle.
1075 "--write-sub"
1076 "--sub-lang" "en,zh-Hans"
1077 "--sub-format" "ass/srt/best" ; ass/srt/best, srt, vtt, ttml, srv3, srv2, srv1
1078 ))))
1079 (youtube-dl url)))
1081 ;;;###autoload
1082 (defun youtube-dl-download-thumbnail (url)
1083 "Download video thumbnail of URL."
1084 (interactive
1085 (list (read-from-minibuffer
1086 "URL: " (or (thing-at-point 'url)
1087 (when interprogram-paste-function
1088 (funcall interprogram-paste-function))))))
1089 (let ((youtube-dl-arguments (append youtube-dl-arguments
1090 '("--skip-download" "--no-warnings"
1091 "--write-thumbnail"))))
1092 (youtube-dl url)))
1094 (provide 'youtube-dl)
1096 ;;; youtube-dl.el ends here