forked from skeeto/youtube-dl-emacs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-dl-play.el
119 lines (98 loc) · 4.53 KB
/
youtube-dl-play.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
;;; youtube-dl-play.el --- plays video clips with mpv player and youtube-dl -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Igor B. Poretsky <[email protected]>
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This module plays video clips with mpv player and youtube-dl
;; command line program, which serves as its back end.
;; The `youtube-dl-play' command starts playback of an item
;; under point in the youtube-dl download list or from specified URL.
;;; Code:
(require 'custom)
(require 'youtube-dl)
;;;###autoload
(defgroup youtube-dl-play ()
"YouTube video playback control options."
:group 'youtube-dl)
(defcustom youtube-dl-play-program "mpv"
"The name of the program invoked for playing YouTube videos."
:group 'youtube-dl-play
:type 'string)
(defcustom youtube-dl-play-fullscreen "--fs"
"Fullscreen playback mode option."
:group 'youtube-dl-play
:type '(choice (const :tag "according to player configuration" nil)
(const :tag "yes" "--fs")
(const :tag "no" "--no-fs")))
(defcustom youtube-dl-play-format nil
"Playback format. It can be specified explicitly when default does not fit.
Some of the formats my actually be unavailable for a particular clip."
:group 'youtube-dl-play
:type '(choice (const :tag "default" nil)
(const :tag "best" "bestvideo+bestaudio")
(const :tag "worst" "worstvideo+worstaudio")
(const "mp4")
(const "flv")
(const "3gp")
(const "webm")
(const :tag "aac (audio only" "aac")
(const :tag "m4a (audio only)" "m4a")
(const :tag "mp3 (audio only)" "mp3")
(const :tag "ogg (audio only)" "ogg")
(const :tag "wav (audio only)" "wav")))
(defvar youtube-dl-play-process nil
"Youtube video playback process.")
(defun youtube-dl-play--sentinel (process event)
"YouTube video playback process events handler."
(when (and (eq process youtube-dl-play-process)
(not (process-live-p process)))
(setf youtube-dl-play-process nil))
(message "Process %s %s" (process-name process) event))
;;;###autoload
(defun youtube-dl-play-stop (&optional force)
"Stops currently playing youtube video if any.
When optional argument is `non-nil' don't ask a confirmation.
Being called interactively never asks a confirmation."
(interactive "p")
(when (and (processp youtube-dl-play-process)
(process-live-p youtube-dl-play-process)
(or force
(y-or-n-p "Stop current playback? ")))
(kill-process youtube-dl-play-process)))
;;;###autoload
(defun youtube-dl-play (thing &optional start)
"Plays video from specified URL or, being invoked interactively
in the download list, from an item under point. Optional second
argument, if non-nil, is treated as start time specification string."
(interactive (youtube-dl-thing))
(youtube-dl-play-stop t)
(let* ((url
(if (youtube-dl-item-p thing)
(youtube-dl-item-url thing)
thing))
(proc
(apply #'start-process "mpv" nil youtube-dl-play-program
"--profile=pseudo-gui" "--ytdl"
(nconc (list youtube-dl-play-fullscreen)
(when youtube-dl-play-format
(list (concat "--ytdl-format=" youtube-dl-play-format)))
(when youtube-dl-proxy
(list (format "--ytdl-raw-options=proxy=\"%s\"" youtube-dl-proxy)))
(when start
(list (concat "--start=" start)))
`(,url)))))
(setf youtube-dl-play-process proc)
(set-process-sentinel proc #'youtube-dl-play--sentinel)))
(provide 'youtube-dl-play)
;;; youtube-dl-play.el ends here