aboutsummaryrefslogtreecommitdiffstats
path: root/yeetube-mpv.el
blob: 2c925a403d9efeea49db84555cbe22d5b4b4360f (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
;;; yeetube-mpv.el --- Provide yeetube mpv functionality  -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Thanos Apollo

;; Author: Thanos Apollo <[email protected]>
;; Keywords: extensions youtube videos
;; URL: https://git.thanosapollo.org/yeetube

;; 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 3 of the License, 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, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This package is a yeetube extension, to start an mpv process &
;; remotely control it.

;;; Code:

(defcustom yeetube-mpv-disable-video nil
  "Add no-video flag when using mpv."
  :type 'boolean
  :group 'yeetube)

(defcustom yeetube-mpv-enable-torsocks nil
  "Enable torsocks.")

(defvar yeetube-mpv-path (executable-find "mpv")
  "Path for mpv executable.")

(defvar yeetube-mpv-torsocks (executable-find "torsocks")
  "Path to torsocks executable.")

(defvar yeetube-mpv-video-quality "720"
  "Video resolution/quality

Accepted values include: 1080, 720, 480, 360, 240, 144")

(defun yeetube-mpv-change-video-quality ()
  (interactive)
  (let ((new-value (completing-read (format "Set video quality (current value %s):" yeetube-mpv-video-quality)
				    '("1080" "720" "480" "360" "240" "144") nil t)))
    (setf yeetube-mpv-video-quality new-value)))

(defun yeetube-mpv-toggle-torsocks ()
  "Toggle torsocks."
  (interactive)
  (pcase yeetube-mpv-torsocks
    ('t (setf yeetube-mpv-torsocks nil)
       (message "yeetube: Torsocks disabled"))
    ('nil (setf yeetube-mpv-torsocks t)
	   (message "yeetube: Torsocks enabled"))))

(defun yeetube-mpv-check ()
  "Check if mpv and yt-dlp is installed."
  (unless (and (executable-find "mpv")
	       (executable-find "yt-dlp"))
    (error "Unable to play video.  Please install `yt-dlp' and `mpv'")))

(defun yeetube-mpv-process (command)
  "Start yeetube process for shell COMMAND."
  (yeetube-mpv-check)
  (let ((yeetube-mpv-process "yeetube"))
    (dolist (process (process-list))
      (when (string-match yeetube-mpv-process (process-name process))
	(kill-process process)))
    (sit-for 0.1)
    (unless (get-process yeetube-mpv-process)
      (start-process-shell-command
       "yeetube" nil command))))

(defun yeetube-mpv-ytdl-format-video-quality (resolution)
  "Return shell quoted argument for ytdlp with RESOLUTION."
  (shell-quote-argument (format "bestvideo[height<=?%s]+bestaudio/best" resolution)))

(defun yeetube-mpv-play (input)
  "Start yeetube process to play INPUT using mpv.

This function is not specific to just playing urls.  Feel free to use
to play local files."
  (yeetube-mpv-process
   (concat (when yeetube-mpv-enable-torsocks
	     (concat yeetube-mpv-torsocks " "))
	   yeetube-mpv-path " --ytdl-format="
	   (yeetube-mpv-ytdl-format-video-quality yeetube-mpv-video-quality)
	   " "
	   (shell-quote-argument input)
	   (when yeetube-mpv-disable-video " --no-video")))
  (message (if yeetube-mpv-enable-torsocks
	       "yeetube: Starting mpv process (using torsocks)"
	     "yeetube: Starting mpv process")))

(defun yeetube-mpv-toggle-no-video-flag ()
  "Toggle no video flag for mpv player."
  (interactive)
  (if yeetube-mpv-disable-video
      (progn (setf yeetube-mpv-disable-video nil)
	     (message "yeetube: mpv enabled video"))
    (setf yeetube-mpv-disable-video t)
    (message "yeetube: mpv disabled video")))

(defun yeetube-mpv-send-keypress (key)
  "Send KEY to yeetube-mpv-process."
  (interactive "sKey: ")
  (process-send-string "yeetube" key))

(defun yeetube-mpv-toggle-pause ()
  "Toggle pause mpv."
  (interactive)
  (yeetube-mpv-send-keypress "p")
  (message "yeetube: toggle pause"))

(defun yeetube-mpv-toggle-fullscreen ()
  "Toggle fullscreen."
  (interactive)
  (yeetube-mpv-send-keypress "f")
  (message "toggle fullscreen"))

(defun yeetube-mpv-toggle-video ()
  "Toggle video mpv."
  (interactive)
  (yeetube-mpv-send-keypress "_")
  (message "yeetube: toggle video"))

(provide 'yeetube-mpv)
;;; yeetube-mpv.el ends here