Skip to content

Commit

Permalink
feat: customize seek step
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchen-lea committed Mar 27, 2024
1 parent 95dede8 commit cab95d2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
33 changes: 33 additions & 0 deletions org-media-note-mpv.el
Expand Up @@ -7,6 +7,16 @@
(require 'org-media-note-core)

;;;; Customization

(defcustom org-media-note-seek-method 'seconds
"Method used for seeking. Possible values are 'seconds, 'percentage, 'frames."
:type 'symbol
:options '(seconds percentage frames))

(defcustom org-media-note-seek-value 5
"Value used for seeking, interpretation depends on `org-media-note-seek-method`."
:type 'number)

;;;; Variables

(defvar org-media-note-last-play-speed 1.0
Expand Down Expand Up @@ -59,6 +69,29 @@
(let ((video-url (read-string "Url to play: ")))
(org-media-note--follow-link video-url 0)))

(defun org-media-note-seek (direction)
"Seek in the given DIRECTION according to the configured method and value."
(interactive)
(let ((was-pause (mpv-get-property "pause"))
(backward? (eq direction 'backward)))
(cl-case org-media-note-seek-method
(seconds (mpv-run-command "seek"
(if backward?
(- org-media-note-seek-value)
org-media-note-seek-value)
"relative"))
(percentage (mpv-run-command "seek"
(if backward?
(- org-media-note-seek-value)
org-media-note-seek-value)
"relative-percent"))
(frames (progn
(dotimes (_ org-media-note-seek-value)
(mpv-run-command (if backward? "frame-back-step" "frame-step"))))
(sleep-for 0.3) ;; without this, frame seek cannot resume play
(when (not (eq was-pause t))
(mpv-run-command "set_property" "pause" "no"))))))

(defun org-media-note-change-speed-by (speed-step)
"Modify playing media's speed by SPEED-STEP."
(let ((current-speed (mpv-get-property "speed")))
Expand Down
35 changes: 32 additions & 3 deletions org-media-note.el
Expand Up @@ -88,8 +88,8 @@
"Set A of AB-loop")))
:width 31)
("g" org-media-note-goto-timestamp "Jump to timestamp")
("<left>" mpv-seek-backward "Backward 5s")
("<right>" mpv-seek-forward "Forward 5s")
("<left>" (org-media-note-seek 'backward) (format "Backward %s" (org-media-note--seek-step t)))
("<right>" (org-media-note-seek 'forward) (format "Forward %s" (org-media-note--seek-step t)))
("C-<left>"
(mpv-run-command "sub-seek" -1)
"Previous subtitle")
Expand Down Expand Up @@ -158,7 +158,9 @@
((eq org-media-note-timestamp-pattern 'hmsf) "hh:mm:ss.fff")))
:width 29)
("t M" org-media-note-set-separator
(format "Separator when merge: %s" org-media-note-separator-when-merge)))))
(format "Separator when merge: %s" org-media-note-separator-when-merge))
("t <right>" org-media-note-set-seek-method
(format "Seek step: %s" (org-media-note--seek-step t))))))


(defun org-media-note--hydra-title ()
Expand Down Expand Up @@ -196,6 +198,19 @@
;; Title when no media is playing
(concat icon " org-media-note")))))

(defun org-media-note--seek-step (&optional in-macro?)
"Return a formatted string based on the current seek method and value."
(format "%s%s"
org-media-note-seek-value
(cond
((eq org-media-note-seek-method 'seconds) "s")
((eq org-media-note-seek-method 'percentage)
(if in-macro? "%%" "%"))
((eq org-media-note-seek-method 'frames)
(if (= org-media-note-seek-value 1)
" frame"
" frames")))))

;;;;; Customize Org link
(defun org-media-note--default-desc-fn (base timestamp seconds desc)
"Default function to generate link description.
Expand Down Expand Up @@ -298,6 +313,20 @@
(setq org-media-note-separator-when-merge new-separator)
(message "org-media-note-media-note-separator set to: %s" org-media-note-separator-when-merge))

(defun org-media-note-set-seek-method ()
"Config the seek method and value for playback."
(interactive)
(let* ((method (org-media-note--select "Select seek method: "
'("seconds" "percentage" "frames")))
(value (read-number (format "Enter value for %s: " method))))
(setq org-media-note-seek-method (cond
((string-equal method "seconds") 'seconds)
((string-equal method "percentage") 'percentage)
((string-equal method "frames") 'frames))
org-media-note-seek-value value))
(message "Seek method set to: %s."
(org-media-note--seek-step)))

;;;;; Minor Mode

(defun org-media-note--update-auto-insert-advice (add-advice)
Expand Down

0 comments on commit cab95d2

Please sign in to comment.