-
Notifications
You must be signed in to change notification settings - Fork 4
/
mpvi.el
1419 lines (1282 loc) · 64.3 KB
/
mpvi.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; mpvi.el --- Media tool based on EMMS and MPV -*- lexical-binding: t -*-
;; Copyright (C) 2023 lorniu <[email protected]>
;; Author: lorniu <[email protected]>
;; URL: https://github.com/lorniu/mpvi
;; Package-Requires: ((emacs "28.1") (emms "11"))
;; Keywords: convenience, docs, multimedia, application
;; SPDX-License-Identifier: MIT
;; Version: 1.1
;;; Commentary:
;;
;; Integrate MPV, EMMS, Org and others with Emacs, make watching/download/convert
;; video or audio conveniently and taking notes easily. Make EMMS support Windows.
;;
;; Installation:
;; - Install `emms' from elpa
;; - Install `mpvi' from melpa, then load it
;; - Install the dependencies: mpv (required), yt-dlp, ffmpeg, seam, danmaku2ass and tesseract
;;
;; Use `mpvi-open' to open a video/audio, then control the MPV with `mpvi-seek'.
;;
;; You can alse control MPV that is opened from `emms'.
;;; Code:
(require 'ffap)
(require 'emms)
(require 'emms-player-mpv)
(defgroup mpvi nil
"Integrate MPV with Emacs."
:group 'external
:prefix 'mpvi-)
(defcustom mpvi-cache-directory
(let ((dir (expand-file-name "mpvi/" (temporary-file-directory))))
(unless (file-exists-p dir) (make-directory dir))
dir)
"Used to save temporary files."
:type 'directory)
(defvar mpvi-last-save-directory nil)
(defvar mpvi-play-history nil)
(defvar mpvi-annotation-face '(:inherit completions-annotations))
(defvar mpvi-build-link-function #'mpvi-build-mpv-link)
(defvar mpvi-screenshot-function #'mpvi-screenshot)
(defvar mpvi-ocr-function #'mpvi-ocr-by-tesseract)
(defvar mpvi-local-video-handler #'mpvi-convert-by-ffmpeg)
(defvar mpvi-remote-video-handler #'mpvi-ytdlp-download)
;; Silence compiler
(defvar org-attach-method)
(defvar org-mouse-map)
(declare-function org-link-set-parameters "org.el" t t)
(declare-function org-open-at-point "org.el" t t)
(declare-function org-insert-item "org.el" t t)
(declare-function org-at-item-p "org.el" t t)
(declare-function org-display-inline-images "org.el" t t)
(declare-function org-attach-attach "org.el" t t)
(declare-function org-timer-secs-to-hms "org.el" t t)
(declare-function org-timer-fix-incomplete "org.el" t t)
(declare-function org-timer-hms-to-secs "org.el" t t)
(declare-function org-element-context "org.el" t t)
;; Helpers
(defun mpvi-log (fmt &rest args)
"Output log when `emms-player-mpv-debug' not nil.
FMT and ARGS are like arguments in `message'."
(when emms-player-mpv-debug
(apply #'message (concat "[mpvi] " fmt) args)))
(defun mpvi-call-process (program &rest args)
"Helper for `call-process', PROGRAM and ARGS are the same."
(mpvi-log ">>> %s %s" program
(mapconcat (lambda (a) (shell-quote-argument a)) args " "))
(apply #'call-process program nil t nil args))
(defun mpvi-url-p (url)
"Return if URL is an URL."
(member (url-type (url-generic-parse-url url)) '("http" "https")))
(defun mpvi-ffap-guesser ()
"Return proper url or file at current point."
(let* ((mark-active nil)
(guess (or (when (derived-mode-p 'org-mode)
(let ((elem (org-element-context)))
(when (equal 'link (car elem))
(setq elem (cadr elem))
(pcase (plist-get elem :type)
("mpv" (car (mpvi-parse-link (plist-get elem :path))))
((or "http" "https") (plist-get elem :raw-link))))))
(ffap-url-at-point)
(ffap-file-at-point))))
(when (and guess (not (mpvi-url-p guess)))
(if (file-exists-p guess)
(when (file-directory-p guess)
(setq guess (file-name-as-directory guess)))
(setq guess nil)))
guess))
(defun mpvi-read-file-name (prompt default-name)
"Read file name using a PROMPT minibuffer.
DEFAULT-NAME is used when only get a directory name."
(let* ((default-directory (or mpvi-last-save-directory default-directory))
(target (read-file-name prompt)))
(if (directory-name-p target)
(expand-file-name (file-name-nondirectory default-name) target)
(expand-file-name target))))
(defun mpvi-time-to-secs (time)
"Convert TIME to seconds format."
(require 'org-timer)
(cond ((or (null time) (numberp time)) time)
((or (not (stringp time)) (not (string-match-p "^-?[0-9:.]+$" time)))
(user-error "This is not a valid time: %s" time))
((cl-find ?: time)
(+ (org-timer-hms-to-secs (org-timer-fix-incomplete time))
(if-let (p (cl-search "." time)) (string-to-number (cl-subseq time p)) 0)))
(t (string-to-number time))))
(defun mpvi-secs-to-hms (secs &optional full truncate)
"Convert SECS to h:mm:ss.xx format.
If FULL is nil, remove '0:' prefix. If TRUNCATE is non-nil, remove frac suffix."
(require 'org-timer)
(let* ((frac (cadr (split-string (number-to-string secs) "\\.")))
(ts (concat (org-timer-secs-to-hms (truncate secs)) (if frac ".") frac)))
(when (and (not full) (string-prefix-p "0:" ts))
(setq ts (cl-subseq ts 2)))
(if truncate (car (split-string ts "\\.")) ts)))
(defun mpvi-secs-to-string (secs &optional groupp)
"Truncate SECS and format to string, keep at most 2 float digits.
When GROUPP not nil then try to insert commas to string for better reading."
(let ((ret (number-to-string
(if (integerp secs) secs
(/ (truncate (* 100 secs)) (float 100))))))
(when groupp
(while (string-match "\\(.*[0-9]\\)\\([0-9][0-9][0-9].*\\)" ret)
(setq ret (concat (match-string 1 ret) "," (match-string 2 ret)))))
ret))
;; MPV
(defvar mpvi-current-url-metadata nil)
(cl-defgeneric mpvi-extract-url (type url &rest _)
"Extract URL for different platforms.
Return a plist:
- :url/title/subfile for the real url, display media title and sub-file
- :opts/cmds for extra options for `loadfile', and commands executed after load
- :started for function executed after loaded
- :out-url-decorator for function to decorate url when open in external program
- others maybe used in anywhere else
TYPE should be keyword as :host format, for example :www.youtube.com,
if it's nil then this method will be a dispatcher."
(:method (type url &rest args)
(unless type ; the first call
(let* ((typefn (lambda (url) (intern (concat ":" (url-host (url-generic-parse-url url))))))
(playlist (mpvi-extract-playlist (funcall typefn url) url)))
(if (and playlist (null (car playlist))) ; when no selected-index, return all items in playlist
(list :playlist-url url :playlist-items (cdr playlist))
(let ((purl (if playlist (nth (car playlist) (cdr playlist)))) ret)
(if-let ((dest (apply #'mpvi-extract-url ; dispatch to method
(funcall typefn (or purl url))
(or purl url) args)))
(progn (setq ret dest)
(unless (plist-get ret :url)
(plist-put ret :url (or purl url))))
(setq ret (list :url (or purl url))))
(when playlist
(plist-put ret :playlist-url url)
(plist-put ret :playlist-index (car playlist))
(plist-put ret :playlist-items (cdr playlist)))
(unless (equal (plist-get ret :url) url)
(plist-put ret :origin-url url))
ret))))))
(cl-defgeneric mpvi-extract-playlist (type url &optional no-choose)
"Check if URL is a playlist link. If it is, return the selected playlist-item.
TYPE is platform as the same as in `mpvi-extract-url'.
Don't prompt user to choose When NO-CHOOSE is not nil.
Return list of (index-or-title playlist-items)."
(:method (_type url &optional no-choose)
(let ((meta (mpvi-ytdlp-url-metadata url)))
(when (assoc 'is_playlist meta)
(let ((urls (cl-loop for item across (alist-get 'entries meta)
collect (alist-get 'url item))))
(if no-choose
(cons (alist-get 'title meta) urls)
(let* ((items (cl-loop
for url in urls for i from 1
for item = (if (member url mpvi-play-history) (propertize url 'face mpvi-annotation-face) url)
collect (propertize item 'line-prefix (propertize (format "%2d. " i) 'face mpvi-annotation-face))))
(item (completing-read
(concat "Playlist" (if-let (title (alist-get 'title meta)) (format " (%s)" title)) ": ")
(lambda (input pred action)
(if (eq action 'metadata)
`(metadata (display-sort-function . ,#'identity))
(complete-with-action action items input pred)))
nil t nil nil (car items))))
(cons (cl-position item urls :test #'string=) urls))))))))
(defun mpvi-check-live ()
"Check if MPV is runing."
(unless (emms-player-mpv-proc-playing-p)
(user-error "No living MPV found"))
(with-temp-buffer
(unless (and (zerop (call-process emms-player-mpv-command-name nil '(t t) nil "--version"))
(progn (goto-char (point-min)) (re-search-forward "^mpv\\s-+v?\\(\\([0-9]+\\.?\\)+\\)" nil t 1))
(string> (mapconcat (lambda (n) (format "%03d" n))
(seq-map 'string-to-number (split-string (match-string-no-properties 1) "\\." t))
".")
"000.016.999"))
(user-error "You should update MPV to support ipc connect"))))
(defun mpvi-origin-path (&optional path)
"Reverse of `mpvi-extract-url', return the origin url for PATH.
When PATH is nil then return the path of current playing video."
(unless path
(mpvi-check-live)
(setq path (mpvi-cmd `(get_property path))))
(or (plist-get mpvi-current-url-metadata :origin-url) path))
(defun mpvi-cmd (cmd)
"Request MPV for CMD. This is sync version of `emms-player-mpv-cmd'."
(when (emms-player-mpv-proc-playing-p)
(catch 'mpvi-ret
(emms-player-mpv-cmd cmd (lambda (data _err)
(ignore-errors
(throw 'mpvi-ret data))))
(while (emms-player-mpv-proc-playing-p) (sleep-for 0.05))
(throw 'mpvi-ret nil))))
(defalias 'mpvi-async-cmd #'emms-player-mpv-cmd)
(cl-defun mpvi-prop (sym &optional (val nil supplied))
"Run command set_property SYM VAL in MPV.
Run get_property instead if VAL is absent."
(if supplied
(mpvi-async-cmd `(set_property ,sym ,val))
(mpvi-cmd `(get_property ,sym))))
(defun mpvi-pause (&optional how)
"Set or toggle pause state of MPV.
HOW is :json-false or t that returned by get-property.
Toggle pause if HOW is nil."
(interactive)
(mpvi-async-cmd
(if how
`(set pause ,(if (eq how :json-false) 'no 'yes))
`(cycle pause))))
(defun mpvi-seekable (&optional arg)
"Whether current video is seekable.
Alert user when not seekable when ARG not nil."
(let ((seekable (eq (mpvi-prop 'seekable) t)))
(if (and arg (not seekable))
(user-error "Current video is not seekable, do nothing")
seekable)))
(defun mpvi-speed (&optional n)
"Tune the speed base on N."
(mpvi-seekable 'assert)
(pcase n
('nil (mpvi-prop 'speed 1)) ; reset
((pred numberp)
(let ((factor (* 1.1 n)))
(mpvi-async-cmd `(multiply speed ,(if (>= n 0) factor (/ -1 factor))))))
(_ (mpvi-prop 'speed (read-from-minibuffer "Speed to: " n nil t)))))
(defcustom mpvi-post-play-cmds nil
"Command list let MPV process run after loading a file.
See `emms-player-mpv-cmd' for syntax."
:type 'list)
(cl-defun mpvi-play (path &optional (beg 0) end emms noseek)
"Play PATH from BEG to END.
EMMS is a flag that this is invoked from EMMS.
When NOSEEK is not nil then dont try to seek but open directly."
(if (mpvi-url-p path)
(unless (executable-find "yt-dlp")
(user-error "You should have 'yt-dlp' installed to play remote url"))
(setq path (expand-file-name path)))
(if (and (not noseek) (emms-player-mpv-proc-playing-p) (equal path (mpvi-origin-path)))
;; when path is current playing, just seek to position
(when (mpvi-seekable)
(mpvi-prop 'ab-loop-a (if end beg "no"))
(mpvi-prop 'ab-loop-b (or end "no"))
(mpvi-prop 'playback-time beg)
(mpvi-prop 'pause 'no))
;; start and loadfile
(message "Waiting %s..." path)
(if (emms-player-mpv-proc-playing-p) (ignore-errors (mpvi-pause t)))
;; If path is not the current playing, load it
(let (logo title subfile opts cmds started)
(when (mpvi-url-p path)
;; preprocessing url and extra mpv commands
(when-let ((ret (mpvi-extract-url nil path)))
(unless (plist-get ret :url) (user-error "Unknown url"))
(setq mpvi-current-url-metadata ret)
(setq path (or (plist-get ret :url) path))
(setq logo (plist-get ret :logo))
(setq title (plist-get ret :title))
(setq subfile (plist-get ret :subfile))
(setq opts (plist-get ret :opts))
(setq cmds (plist-get ret :cmds))
(setq started (plist-get ret :started))))
(setq opts
`((start . ,beg)
,@(when end
`((ab-loop-a . ,beg)
(ab-loop-b . ,end)))
,(when title
`(force-media-title . ,(format "\"%s\"" title)))
,(when subfile
`(sub-file . ,(format "\"%s\"" subfile)))
,@opts))
(mpvi-log "load opts: %S" opts)
(let* ((lst `(((set_property speed 1))
((set_property keep-open ,(if emms 'no 'yes)))
((loadfile ,path replace
,(mapconcat (lambda (x)
(format "%s=%s" (car x) (cdr x)))
(delq nil opts) ","))
. ,(lambda (_ err)
(if err
(message "Load video failed (%S)" err)
(if started
(funcall started)
(message "Started%s"
(if title
(concat (if logo (concat "/" logo)) ": "
(propertize title 'face 'font-lock-keyword-face))
".")))
(push path mpvi-play-history))))
((set_property pause no))
,@(cl-loop for c in `(,@cmds ,@mpvi-post-play-cmds)
if (car-safe (car c)) collect c
else collect (list c))))
(cmd (cons 'batch (delq nil lst))))
(mpvi-log "load-commands: %S" cmd)
(if (and emms (emms-player-mpv-proc-playing-p)) (emms-player-mpv-stop))
(mpvi-async-cmd cmd)))))
;; Timestamp Link
(defun mpvi-parse-link (link)
"Extract path, beg, end from LINK."
(if (string-match "^\\([^#]+\\)\\(?:#\\([0-9:.]+\\)?-?\\([0-9:.]+\\)?\\)?$" link)
(let ((path (match-string 1 link))
(beg (match-string 2 link))
(end (match-string 3 link)))
(list path (mpvi-time-to-secs beg) (mpvi-time-to-secs end)))
(user-error "Link is not valid")))
(defun mpvi-parse-link-at-point ()
"Return the mpv link object at point."
(unless (derived-mode-p 'org-mode)
(user-error "You must parse MPV link in org mode"))
(let ((node (cadr (org-element-context))))
(when (equal "mpv" (plist-get node :type))
(let ((meta (mpvi-parse-link (plist-get node :path)))
(end (save-excursion (goto-char (plist-get node :end)) (skip-chars-backward " \t") (point))))
`(:path ,(car meta) :vbeg ,(cadr meta) :vend ,(caddr meta) :end ,end ,@node)))))
(defun mpvi-build-mpv-link (path &optional beg end desc)
"Build mpv link with timestamp that used in org buffer.
PATH is local video file or remote url. BEG and END is the position number.
DESC is optional, used to describe the current timestamp link."
(concat "[[mpv:" path (if (or beg end) "#")
(if beg (number-to-string beg))
(if end "-")
(if end (number-to-string end))
"][▶ "
(if beg (mpvi-secs-to-hms beg nil t))
(if end " → ")
(if end (mpvi-secs-to-hms end nil t))
"]]"
(if desc (concat " " desc))))
(defcustom mpvi-attach-link-attrs "#+attr_html: :width 666"
"Attrs insert above a inserted attach image.
The :width can make image cannot display too large in org mode."
:type 'string)
(defun mpvi-insert-attach-link (file)
"Save image FILE to org file using `org-attach'."
(require 'org-attach)
;; attach it
(let ((org-attach-method 'mv)) (org-attach-attach file))
;; insert the attrs
(when mpvi-attach-link-attrs
(insert (string-trim mpvi-attach-link-attrs) "\n"))
;; insert the link
(insert "[[attachment:" (file-name-base file) "." (file-name-extension file) "]]")
;; show it
(org-display-inline-images))
(cl-defmacro mpvi-with-current-mpv-link ((var &optional path errmsg) &rest form)
"Run FORM when there is a mpv PATH at point that is playing.
Bind the link object to VAR for convenience. Alert user with ERRMSG when
there is a different path at point."
(declare (indent 1))
`(progn
(mpvi-check-live)
(let ((,var (mpvi-parse-link-at-point)))
(when (and ,var (not (equal (plist-get ,var :path)
,(or path `(mpvi-origin-path)))))
(user-error ,(or errmsg "Current link is not the actived one, do nothing")))
,@form)))
;; screenshot
(defvar mpvi-clipboard-command
(cond ((executable-find "xclip")
;; A hangs issue:
;; https://www.reddit.com/r/emacs/comments/da9h10/why_does_shellcommand_hang_using_xclip_filter_to/
"xclip -selection clipboard -t image/png -filter < \"%s\" &>/dev/null")
((and (executable-find "powershell") (memq system-type '(cygwin windows-nt)))
"powershell -Command \"Add-Type -AssemblyName System.Windows.Forms; [Windows.Forms.Clipboard]::SetImage($([System.Drawing.Image]::Fromfile(\\\"%s\\\")))\"")))
(defun mpvi-image-to-clipboard (image-file)
"Save IMAGE-FILE data to system clipboard.
I don't know whether better solutions exist."
(if (and mpvi-clipboard-command (file-exists-p image-file))
(let ((command (format mpvi-clipboard-command (shell-quote-argument image-file))))
(mpvi-log "Copy image to clipboard: %s" command)
(shell-command command))
(user-error "Nothing to do with copy image file")))
(defun mpvi-screenshot (path pos &optional target)
"Capture the screenshot of PATH at POS and save to TARGET."
(unless (mpvi-url-p path)
(setq path (expand-file-name path)))
(setq target
(if target (expand-file-name target)
(expand-file-name (format-time-string "IMG-%s.png") mpvi-cache-directory)))
(with-temp-buffer
(if (zerop (call-process "mpv" nil nil nil path
"--no-terminal" "--no-audio" "--vo=image" "--frames=1"
(format "--start=%s" (or pos 0))
"-o" target))
target
(user-error "Capture failed: %s" (string-trim (buffer-string))))))
(defun mpvi-screenshot-current-playing (&optional target flag)
"Capture screenshot from current playing mpv and save to TARGET.
If TARGET is nil save to temporary directory, if it is t save to clipboard.
If FLAG is string, pass directly to mpv as <flags> of screenshot-to-file, if
it is nil pass \"video\" as default, else prompt user to choose one."
(mpvi-check-live)
(let ((file (if (stringp target)
(expand-file-name target)
(expand-file-name (format-time-string "IMG-%s.png") mpvi-cache-directory)))
(flags (list "video" "subtitles" "window")))
(unless (or (null flag) (stringp flag))
(setq flag (completing-read "Flag of screenshot: " flags nil t)))
(unless (member flag flags) (setq flag "video"))
(mpvi-cmd `(screenshot-to-file ,file ,flag))
(if (eq target t) ; if filename is t save data to clipboard
(mpvi-image-to-clipboard file)
(prog1 file (kill-new file)))))
;; tesseract
(defcustom mpvi-tesseract-args "-l chi_sim"
"Extra options pass to `tesseract'."
:type 'string)
(defun mpvi-ocr-by-tesseract (file)
"Run tesseract OCR on the screenshot FILE."
(unless (executable-find "tesseract")
(user-error "Program `tesseract' not found"))
(with-temp-buffer
(if (zerop (apply #'mpvi-call-process "tesseract" file "stdout"
(if mpvi-tesseract-args (split-string-and-unquote mpvi-tesseract-args))))
(buffer-string)
(user-error "OCR tesseract failed: %s" (string-trim (buffer-string))))))
;; ffmpeg
(defcustom mpvi-ffmpeg-extra-args nil
"Extra options pass to `ffmpeg'."
:type 'string)
(defcustom mpvi-ffmpeg-gif-filter "fps=10,crop=iw:ih:0:0,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse"
"Filter used when use `ffmpeg' to convert to gif file."
:type 'string)
(defun mpvi-convert-by-ffmpeg (file &optional target beg end opts)
"Convert local video FILE from BEG to END using ffmpeg, output to TARGET.
This can be used to cut/resize/reformat and so on.
OPTS is a string, pass to `ffmpeg' when it is not nil."
(cl-assert (file-regular-p file))
(unless (executable-find "ffmpeg")
(user-error "Program `ffmpeg' not found"))
(let* ((beg (if (numberp beg) (format " -ss %s" beg) ""))
(end (if (numberp end) (format " -to %s" end) ""))
(target (expand-file-name
(or target (format-time-string "mpv-video-%s.mp4"))
mpvi-last-save-directory))
(extra (concat (if (member (file-name-extension target) '("gif" "webp"))
(format " -vf \"%s\" -loop 0" mpvi-ffmpeg-gif-filter)
" -c copy")
(if (or opts mpvi-ffmpeg-extra-args)
(concat " " (string-trim (or opts mpvi-ffmpeg-extra-args))))))
(command (string-trim
(minibuffer-with-setup-hook
(lambda ()
(use-local-map (make-composed-keymap nil (current-local-map)))
(local-set-key (kbd "C-x C-q")
(lambda ()
(interactive)
(let ((inhibit-read-only t))
(set-text-properties (minibuffer-prompt-end) (point-max) nil))))
(local-set-key (kbd "<return>")
(lambda ()
(interactive)
(let ((cmd (minibuffer-contents)))
(with-temp-buffer
(insert (string-trim cmd))
(let ((quote (if (member (char-before) '(?' ?\")) (char-before))))
(when (re-search-backward (if quote (format " +%c" quote) " +") nil t)
(setq target (buffer-substring (match-end 0) (if quote (- (point-max) 1) (point-max)))))))
(if (file-exists-p target)
(message
(propertize
(format "Output file %s is already exist!" target)
'face 'font-lock-warning-face))
(exit-minibuffer))))))
(read-string "Confirm: "
(concat (propertize
(concat "ffmpeg"
(propertize " -loglevel error" 'invisible t)
(format " -i %s" (expand-file-name file)))
'face 'font-lock-constant-face 'read-only t)
beg end extra (format " \"%s\"" target)))))))
(make-directory (file-name-directory target) t) ; ensure directory
(setq mpvi-last-save-directory (file-name-directory target)) ; record the dir
(with-temp-buffer
(mpvi-log "Convert file %s" file)
(apply #'mpvi-call-process (split-string-and-unquote command))
(if (file-exists-p target)
(prog1 target
(kill-new target)
(message "Save to %s done." (propertize target 'face 'font-lock-keyword-face)))
(user-error "Convert with ffmpeg failed: %s" (string-trim (buffer-string)))))))
;; yt-dlp
(defcustom mpvi-ytdlp-extra-args nil
"The default extra options pass to `yt-dlp'."
:type 'string)
(defvar mpvi-ytdlp-metadata-cache nil)
(defun mpvi-ytdlp-url-metadata (url &optional opts)
"Return metadata for URL, pass extra OPTS to `yt-dlp' for querying.
I just want to judge if current URL is a playlist link, but I can't find
better/faster solution. Maybe cache the results is one choice, but I don't think
it's good enough. Then I can not find good way to get all descriptions of
playlist item with light request. This should be improved someday."
(unless (executable-find "yt-dlp")
(user-error "Program `yt-dlp' should be installed"))
(or (cdr (assoc url mpvi-ytdlp-metadata-cache))
(with-temp-buffer
(condition-case nil
(progn
(mpvi-log "Request matadata for %s" url)
(apply #'mpvi-call-process
"yt-dlp" url "-J" "--flat-playlist" "--no-warnings"
(split-string-and-unquote (or opts mpvi-ytdlp-extra-args "")))
(goto-char (point-min))
(let* ((json (json-read))
(playlistp (equal "playlist" (alist-get '_type json))))
(if playlistp (nconc json (list '(is_playlist . t))))
(push (cons url json) mpvi-ytdlp-metadata-cache)
json))
(error (user-error "Error when get metadata for %s: %s" url (string-trim (buffer-string))))))))
(defun mpvi-ytdlp-pick-format (url)
"Completing read the formats for video with URL.
Return (suggestion-save-name . video-format)."
(unless (executable-find "yt-dlp")
(user-error "Program 'yt-dlp' should be installed"))
(with-temp-buffer
(mpvi-call-process "yt-dlp" "-F" url)
(goto-char (point-min))
(unless (re-search-forward "Available formats for \\(.+\\):" nil t)
(user-error "Nothing found: %s" (string-trim (buffer-string))))
(let* ((name (if (equal (mpvi-prop 'path) url)
(mpvi-prop 'media-title)
(match-string 1)))
(fmts (cl-loop with text = (string-trim (buffer-substring
(progn (search-forward "-\n" nil t) (point))
(point-max)))
for item in (split-string text "\n")
collect (cons (concat (propertize "> " 'face 'font-lock-keyword-face) item)
(split-string item " +"))))
(format (string-trim
(completing-read
"Format (choose directly for one, input like '1,4' for multiple. Default: 'bv,ba'): "
(lambda (input _pred action)
(pcase action
('metadata
`(metadata (display-sort-function . ,#'identity)))
(`(boundaries . _)
`(boundaries . ,(cons (length input) 0)))
(_ (complete-with-action action fmts "" nil))))
nil nil nil nil "bv,ba")))
(format (if (string-prefix-p ">" format)
(cadr (assoc format fmts))
(string-trim (cl-subseq format 0 (cl-position ?\> format)))))
(ext (if-let ((fmt (cl-find-if (lambda (c) (equal (cadr c) format)) fmts)))
(caddr fmt) "mp4")))
(setq format (string-replace " " "" (string-replace "," "+" format)))
(cons (concat name "_" format "." ext) format))))
(defun mpvi-ytdlp-output-field (url field &optional opts)
"Get FIELD information for video URL.
FIELD can be id/title/urls/description/format/thumbnail/formats_table and so on.
Pass extra OPTS to mpv if it is not nil."
(unless (executable-find "yt-dlp")
(user-error "Program 'yt-dlp' should be installed"))
(with-temp-buffer
(mpvi-log "yt-dlp output template for %s of %s" field url)
(apply #'mpvi-call-process
"yt-dlp" url "--print" field
(split-string-and-unquote (or opts mpvi-ytdlp-extra-args "")))
(goto-char (point-min))
(if (re-search-forward "^yt-dlp: error:.*$" nil t)
(user-error "Error to get `yt-dlp' template/%s: %s" field (match-string 0))
(string-trim (buffer-string)))))
(defun mpvi-ytdlp-download (url &optional target beg end opts)
"Download and clip video for URL to TARGET. Use BEG and END for range (trim).
OPTS is a string, pass to `yt-dlp' when it is not nil."
(cl-assert (mpvi-url-p url))
(unless (and (executable-find "yt-dlp") (executable-find "ffmpeg"))
(user-error "Programs `yt-dlp' and `ffmpeg' should be installed"))
(let* ((fmt (mpvi-ytdlp-pick-format url))
(beg (if (numberp beg) (format " -ss %s" beg)))
(end (if (numberp end) (format " -to %s" end)))
(extra (if (or opts mpvi-ytdlp-extra-args)
(concat " " (string-trim (or opts mpvi-ytdlp-extra-args)))
""))
(target (expand-file-name (or target (car fmt)) mpvi-last-save-directory))
(command (string-trim
(minibuffer-with-setup-hook
(lambda ()
(backward-char)
(use-local-map (make-composed-keymap nil (current-local-map)))
(local-set-key (kbd "<return>")
(lambda ()
(interactive)
(let ((cmd (minibuffer-contents)))
(with-temp-buffer
(insert cmd)
(goto-char (point-min))
(when (re-search-forward " -o +['\"]?\\([^'\"]+\\)" nil t)
(setq target (match-string 1)))
(if (file-exists-p target)
(message
(propertize
(format "Output file %s is already exist!" target)
'face 'font-lock-warning-face))
(exit-minibuffer)))))))
(read-string
"Confirm: "
(concat (propertize (concat "yt-dlp " url) 'face 'font-lock-constant-face 'read-only t)
" -f \"" (cdr fmt) "\""
(if (or beg end) " --downloader ffmpeg --downloader-args \"ffmpeg_i:")
beg end (if (or beg end) "\"") extra
" -o \"" target "\""))))))
(make-directory (file-name-directory target) t) ; ensure directory
(setq mpvi-last-save-directory (file-name-directory target)) ; record the dir
(with-temp-buffer
(mpvi-log "Download/Clip url %s" url)
(apply #'mpvi-call-process (split-string-and-unquote command))
(if (file-exists-p target)
(prog1 target
(kill-new target)
(message "Save to %s done." (propertize target 'face 'font-lock-keyword-face)))
(user-error "Download and clip with yt-dlp/ffmpeg failed: %s" (string-trim (buffer-string)))))))
(defun mpvi-ytdlp-download-subtitle (url &optional prefix opts)
"Download subtitle for URL and save as file named begin with PREFIX.
Pass OPTS to `yt-dlp' when it is not nil."
(unless (executable-find "yt-dlp")
(user-error "Program `yt-dlp' should be installed"))
(with-temp-buffer
(mpvi-log "Downloading subtitle for %s" url)
(apply #'mpvi-call-process
"yt-dlp" url "--write-subs" "--skip-download"
"-o" (or prefix (expand-file-name "SUB-%(fulltitle)s" mpvi-cache-directory))
(split-string-and-unquote (or opts mpvi-ytdlp-extra-args "")))
(goto-char (point-min))
(if (re-search-forward "Destination:\\(.*\\)$" nil t)
(string-trim (match-string 1))
(user-error "Error when download subtitle: %s" (string-trim (buffer-string))))))
;;; Patch `emms-player-mpv.el' for better integrated
;;
;; 1) Emacs don't have builtin way of connecting to Windows named pipe server
;;
;; - Should improve `make-network-process' to support this. Here solved by PowerShell
;;
;; 2) Some MPV events like 'end-file/playback-restart' not triggered as expected on Windows (BUG?),
;; So some logics in `emms-player-mpv-event-handler' are not working.
;;
;; - Maybe should improve MPV for Windows. Here workaround by adding some ugly patches in EMMS
;;
;; 3) The APIs in `emms-player-mpv.el' are too tightly tied to EMMS playlist
;;
;; - Maybe should refactor the APIs to make them can be used standalone, that is, can connect
;; to MPV and play videos without having to update EMMS playlist and so on
;;
;; Windows support, implement by PowerShell
(defun mpvi-emms-player-mpv-ipc-init (func)
"Advice for FUNC `emms-player-mpv-ipc-init', add Windows support."
(if (eq system-type 'windows-nt)
(mpvi-connect-to-win-named-pipe emms-player-mpv-ipc-socket)
(funcall func)))
(defun mpvi-emms-player-mpv-ipc-recv (json-string)
"Advice for `emms-player-mpv-ipc-recv', patch for output of PowerShell.
JSON-STRING is json format string return by ipc process."
(emms-player-mpv-debug-msg "json << %s" json-string)
(let (json)
(condition-case err
(setq json (json-read-from-string json-string))
;; PowerShell will output error message when something goes wrong to standard output,
;; It's not json format, so catch it here
(error (erase-buffer) (user-error "ERR in IPC-RECV: %s\n------\n%s" err json-string)))
(let ((rid (alist-get 'request_id json)))
(when (and rid (not (alist-get 'command json))) ; skip the echoed 'command' for Windows
(emms-player-mpv-ipc-req-resolve
rid (alist-get 'data json) (alist-get 'error json)))
(when (alist-get 'event json)
(emms-player-mpv-event-handler json)
;; Only call the hook when video is played from EMMS
(when (emms-playlist-current-selected-track)
(run-hook-with-args 'emms-player-mpv-event-functions json))))))
(defun mpvi-emms-player-mpv-event-handler (func json-data)
"Advice for FUNC `mpvi-emms-player-mpv-event-handler', workaround for Windows.
JSON-DATA is argument."
(when (eq system-type 'windows-nt)
(pcase (alist-get 'event json-data)
("start-file" ; playback-restart event not working in Windows
(unless (emms-player-mpv-proc-playing-p)
(emms-player-mpv-proc-playing t)
(emms-player-started emms-player-mpv))
(emms-player-mpv-event-playing-time-sync))))
(funcall func json-data))
(defun mpvi-emms-player-mpv-force-stop (&rest _)
"Advice for `emms-player-mpv-proc-sentinel' and `emms-player-mpv-stop'."
;; Event 'end-file' is not working correctly on Windows! So have a try like this..
(when (eq system-type 'windows-nt)
(emms-player-mpv-proc-stop)
(emms-player-mpv-ipc-stop)
(emms-player-mpv-proc-playing nil)))
(defun mpvi-connect-to-win-named-pipe (pipename)
"Connect to MPV by PIPENAME via `PowerShell'."
(emms-player-mpv-ipc-stop)
(emms-player-mpv-debug-msg "ipc: init for windows")
(with-current-buffer (get-buffer-create emms-player-mpv-ipc-buffer)
(erase-buffer))
(setq emms-player-mpv-ipc-id 1
emms-player-mpv-ipc-req-table nil)
(setq pipename (string-replace "/" "\\" pipename)) ; path seperator on Windows is different
(with-timeout (5 (emms-player-mpv-ipc-stop)
(user-error "No MPV process found"))
(while (not (mpvi-win-named-pipe-exists-p pipename))
(sleep-for 0.05)))
(let* ((ps1 " $conn = [System.IO.Pipes.NamedPipeClientStream]::new('.', '%s');
try {
$reader = [System.IO.StreamReader]::new($conn);
$writer = [System.IO.StreamWriter]::new($conn);
$conn.Connect(5000);
while (1) {
$msg = Read-Host;
$writer.WriteLine($msg);
$writer.Flush();
$conn.WaitForPipeDrain();
do {
$ret = $reader.ReadLine();
Write-Host $ret;
} while ($ret -match '\"event\":');
}
}
catch [System.TimeoutException], [System.InvalidOperationException] { Write-Host 'Connect to MPV failed'; }
catch { Write-Host $_; }
finally { $conn.Dispose(); } ")
(cmd (format "& {%s}" (replace-regexp-in-string
"[ \n\r\t]+" " " (format ps1 pipename))))
(proc (make-process :name "emms-player-mpv-ipc"
:connection-type 'pipe
:buffer (get-buffer-create emms-player-mpv-ipc-buffer)
:noquery t
:filter #'emms-player-mpv-ipc-filter
:sentinel #'emms-player-mpv-ipc-sentinel
:command (list "powershell" "-NoProfile" "-Command" cmd))))
(with-timeout (5 (setq emms-player-mpv-ipc-proc nil)
(user-error "Connect to MPV failed"))
(while (not (eq (process-status emms-player-mpv-proc) 'run))
(sleep-for 0.05)))
(setq emms-player-mpv-ipc-proc proc)))
(defun mpvi-win-named-pipe-exists-p (pipename)
"Check if named pipe with name of PIPENAME exists on Windows."
(unless (executable-find "powershell")
(user-error "Cannot find PowerShell"))
(with-temp-buffer
(call-process "powershell" nil t nil
"-Command"
(format "& {Get-ChildItem \\\\.\\pipe\\ | Where-Object {$_.Name -eq '%s'}}"
pipename))
(> (length (buffer-string)) 0)))
;; Only update track when videos are played from EMMS buffer
(defun mpvi-emms-player-started (player)
"Advice for `emms-player-started', PLAYER is the current player."
(setq emms-player-playing-p player
emms-player-paused-p nil)
(when (emms-playlist-current-selected-track) ; add this
(run-hooks 'emms-player-started-hook)))
(defun mpvi-emms-player-stopped ()
"Advice for `emms-player-stopped'."
(setq emms-player-playing-p nil)
(when (emms-playlist-current-selected-track) ; add this
(if emms-player-stopped-p
(run-hooks 'emms-player-stopped-hook)
(sleep-for emms-player-delay)
(run-hooks 'emms-player-finished-hook)
(funcall emms-player-next-function))))
;; Integrate `emms-player-start' with `mpvi-play'
(defun mpvi-emms-player-mpv-start (track)
"Play TRACK in EMMS. Integrate with `mpvi-play'."
(setq emms-player-mpv-stopped nil)
(emms-player-mpv-proc-playing nil)
(let ((track-name (emms-track-get track 'name))
(start-func (lambda () (mpvi-play track-name nil nil t)))) ; <- change this
(if (and (not (eq system-type 'windows-nt)) ; pity, auto switch next not working on Windows
emms-player-mpv-ipc-stop-command)
(setq emms-player-mpv-ipc-stop-command start-func)
(funcall start-func))))
;; Minor mode
;;;###autoload
(define-minor-mode mpvi-emms-integrated-mode
"Global minor mode to toggle EMMS integration."
:global t
(if mpvi-emms-integrated-mode
(progn
(advice-add #'emms-player-mpv-ipc-init :around #'mpvi-emms-player-mpv-ipc-init)
(advice-add #'emms-player-mpv-ipc-recv :override #'mpvi-emms-player-mpv-ipc-recv)
(advice-add #'emms-player-mpv-event-handler :around #'mpvi-emms-player-mpv-event-handler)
(advice-add #'emms-player-mpv-proc-sentinel :after #'mpvi-emms-player-mpv-force-stop)
;;
(advice-add #'emms-player-started :override #'mpvi-emms-player-started)
(advice-add #'emms-player-stopped :override #'mpvi-emms-player-stopped)
;;
(advice-add #'emms-player-mpv-start :override #'mpvi-emms-player-mpv-start)
(advice-add #'emms-player-mpv-stop :after #'mpvi-emms-player-mpv-force-stop))
(advice-remove #'emms-player-mpv-ipc-init #'mpvi-emms-player-mpv-ipc-init)
(advice-remove #'emms-player-mpv-ipc-recv #'mpvi-emms-player-mpv-ipc-recv)
(advice-remove #'emms-player-mpv-event-handler #'mpvi-emms-player-mpv-event-handler)
(advice-remove #'emms-player-mpv-proc-sentinel #'mpvi-emms-player-mpv-force-stop)
(advice-remove #'emms-player-started #'mpvi-emms-player-started)
(advice-remove #'emms-player-stopped #'mpvi-emms-player-stopped)
(advice-remove #'emms-player-mpv-start #'mpvi-emms-player-mpv-start)
(advice-remove #'emms-player-mpv-stop #'mpvi-emms-player-mpv-force-stop)))
(mpvi-emms-integrated-mode 1)
;;; Interactive Commands
;; [open]
(defcustom mpvi-favor-paths nil
"Your favor video path list.
Item should be a path string or a cons.
For example:
\\='(\"~/video/aaa.mp4\"
\"https://www.youtube.com/watch?v=NQXA\"
(\"https://www.douyu.com/110\" . \"some description\"))
This can be used by `mpvi-open-from-favors' to quick open video."
:type 'list)
(defvar mpvi-open-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map (kbd "C-x b") #'mpvi-open-from-favors)
(define-key map (kbd "C-x <return>") (lambda () (interactive) (throw 'mpvi-open (list (minibuffer-contents) 'add))))
(define-key map (kbd "C-x C-w") (lambda () (interactive) (throw 'mpvi-open (list (minibuffer-contents) 'dup))))
map))
;;;###autoload
(defun mpvi-open (path &optional act)
"Deal with PATH, which is a local video or remote url.
Play the video if ACT is nil or play, add to EMMS if ACT is add,
clip the video if ACT is dup.
Keybind `C-x RET' to add to playlist.
Keybind `C-x b' to choose video path from `mpvi-favor-paths'."
(interactive (catch 'mpvi-open
(minibuffer-with-setup-hook
(lambda ()
(use-local-map (make-composed-keymap (list (current-local-map) mpvi-open-map))))
(list (unwind-protect
(catch 'ffap-prompter
(ffap-read-file-or-url
"Playing video (file or url): "
(prog1 (mpvi-ffap-guesser) (ffap-highlight))))
(ffap-highlight t))))))
(unless (and (> (length path) 0) (or (mpvi-url-p path) (file-exists-p path)))
(user-error "Not correct file or url"))
(prog1 (setq path (if (mpvi-url-p path) path (expand-file-name path)))
(cond
((or (null act) (equal act 'play))
(setq mpvi-current-url-metadata nil)
(with-current-emms-playlist (setq emms-playlist-selected-marker nil))
(mpvi-play path))
((equal act 'add)
(mpvi-emms-add path))
((equal act 'dup)
(if (mpvi-url-p path)
(mpvi-ytdlp-download path)
(mpvi-convert-by-ffmpeg path))))))
;;;###autoload
(defun mpvi-open-from-favors ()
"Choose video from `mpvi-favor-paths' and play it."
(interactive)
(unless (consp mpvi-favor-paths)
(user-error "You should add your favor paths into `mpvi-favor-paths' first"))
(let* ((annfn (lambda (it)
(when-let (s (alist-get it mpvi-favor-paths))
(format " (%s)" s))))
(path (completing-read "Choose video to play: "
(lambda (input pred action)
(if (eq action 'metadata)
`(metadata (display-sort-function . ,#'identity)
(annotation-function . ,annfn))
(complete-with-action action mpvi-favor-paths input pred)))
nil t)))
;; called directly vs called from minibuffer
(if (= (recursion-depth) 0)
(mpvi-open path)
(throw 'mpvi-open (list path 'play)))))
;; [seek]
(defvar mpvi-seek-paused nil)
(defvar mpvi-seek-overlay nil)
(defvar mpvi-seek-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map minibuffer-local-map)
(define-key map (kbd "i") #'mpvi-seeking-insert)
(define-key map (kbd "g") #'mpvi-seeking-revert)
(define-key map (kbd "n") (lambda () (interactive) (mpvi-seeking-walk 1)))
(define-key map (kbd "p") (lambda () (interactive) (mpvi-seeking-walk -1)))
(define-key map (kbd "N") (lambda () (interactive) (mpvi-seeking-walk "1%")))
(define-key map (kbd "P") (lambda () (interactive) (mpvi-seeking-walk "-1%")))
(define-key map (kbd "M-n") (lambda () (interactive) (mpvi-seeking-walk :ff)))
(define-key map (kbd "M-p") (lambda () (interactive) (mpvi-seeking-walk :fb)))
(define-key map (kbd "C-l") (lambda () (interactive) (mpvi-seeking-walk 0)))
(define-key map (kbd "C-n") (lambda () (interactive) (mpvi-seeking-walk 1)))
(define-key map (kbd "C-p") (lambda () (interactive) (mpvi-seeking-walk -1)))
(define-key map (kbd "M-<") (lambda () (interactive) (mpvi-seeking-revert 0)))
(define-key map (kbd "k") (lambda () (interactive) (mpvi-speed 1)))