Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] Define functions for making mpv play the curent subtitle audio at point 25%, 50% and 75% of the audio #69

Open
rodrigomorales1 opened this issue Jun 21, 2024 · 6 comments

Comments

@rodrigomorales1
Copy link

rodrigomorales1 commented Jun 21, 2024

Let me explain my use case: When I am editing a subtitle in a *.srt file, I sometimes want to check if the audio for that segment is consistent with the subtitle, to check this I am willing to listen to the entire audio for that subtitle. When editing the end time of a subtitle, I frequently want to check if the last part of the audio is consistent with the subtitle. Currently, when the end time of a subtitle changes, the audio is played from the start time, so I unwillingly need to listen to the audio starting from the beginning. I believe it would be more convenient for some users (such as me) if there were a function that would allow the user to play the audio starting at position 75%, so that the user don't need to listen to the 0%-75% segment, which the user might not be interested in listening.

In my use case, there are some subtitles which are 15 seconds long. Sometimes when I change the end time, I want to listen the last part of the audio (i.e. the segment 75%-100% in the current subtitle audio). Having to listen to a 15 seconds audio even though I'm interested in the last part is bothersome. I wish there were a function that I could map to a key to a function that makes mpv play the current subtitle audio starting at 75%.

I believe having functions that play the current subtitle audio at positions 25%, 50% and 75% would make subed more convenient to other users, because users can map those keys to a key and instruct mpv to play starting at a point that is closer to the part they are interested in listening.

@rodrigomorales1
Copy link
Author

rodrigomorales1 commented Jun 21, 2024

I have written these functions to accomplish the behavior I described:

(defun my-subed-mpv-jump-to-25% ()
  (interactive)
  (let ((cur-sub-start (subed-subtitle-msecs-start))
        (cur-sub-stop (subed-subtitle-msecs-stop)))
    (subed-mpv-jump
     (+ cur-sub-start
        (* (- cur-sub-stop cur-sub-start) 0.25)))))

(defun my-subed-mpv-jump-to-50% ()
  (interactive)
  (let ((cur-sub-start (subed-subtitle-msecs-start))
        (cur-sub-stop (subed-subtitle-msecs-stop)))
    (subed-mpv-jump
     (+ cur-sub-start
        (* (- cur-sub-stop cur-sub-start) 0.5)))))

(defun my-subed-mpv-jump-to-75% ()
  (interactive)
  (let ((cur-sub-start (subed-subtitle-msecs-start))
        (cur-sub-stop (subed-subtitle-msecs-stop)))
    (subed-mpv-jump
     (+ cur-sub-start
        (* (- cur-sub-stop cur-sub-start) 0.75)))))

(defun my-subed-mpv-jump-to-90% ()
  (interactive)
  (let ((cur-sub-start (subed-subtitle-msecs-start))
        (cur-sub-stop (subed-subtitle-msecs-stop)))
    (subed-mpv-jump
     (+ cur-sub-start
        (* (- cur-sub-stop cur-sub-start) 0.90)))))

I mapped those functions to these keys:

(define-key subed-mode-map (kbd "C-c C-1") 'my-subed-mpv-jump-to-25%)
(define-key subed-mode-map (kbd "C-c C-2") 'my-subed-mpv-jump-to-50%)
(define-key subed-mode-map (kbd "C-c C-3") 'my-subed-mpv-jump-to-75%)
(define-key subed-mode-map (kbd "C-c C-4") 'my-subed-mpv-jump-to-90%)

I release the code included in this comment under Creative Commons Zero License

@rodrigomorales1
Copy link
Author

rodrigomorales1 commented Jun 29, 2024

I just found out that subed defines two functions for jumping to specific parts of the audio:

subed-mpv-jump-to-current-subtitle

subed/subed/subed-mpv.el

Lines 326 to 332 in 07567d5

(defun subed-mpv-jump-to-current-subtitle ()
"Move playback position to start of currently focused subtitle if possible."
(interactive)
(let ((cur-sub-start (subed-subtitle-msecs-start)))
(when cur-sub-start
(subed-debug "Seeking player to focused subtitle: %S" cur-sub-start)
(subed-mpv-jump cur-sub-start))))

subed-mpv-jump-to-current-subtitle-near-end

subed/subed/subed-mpv.el

Lines 334 to 341 in 07567d5

(defun subed-mpv-jump-to-current-subtitle-near-end ()
"Move playback position to near the end of the current subtitle."
(interactive)
(let ((cur-sub-end (subed-subtitle-msecs-stop)))
(when cur-sub-end
(setq cur-sub-end (- cur-sub-end subed-sample-msecs))
(subed-debug "Seeking player to end of focused subtitle: %S" cur-sub-end)
(subed-mpv-jump cur-sub-end))))

Both functions are mapped by default.

subed/subed/subed.el

Lines 73 to 74 in 07567d5

(define-key subed-mode-map (kbd "M-j") #'subed-mpv-jump-to-current-subtitle)
(define-key subed-mode-map (kbd "M-J") #'subed-mpv-jump-to-current-subtitle-near-end)

I think these already defined functions cover most use cases. In order to cover more use cases, we could provide a macro that helps users define functions, which would be similar to the functions I defined in #69 (comment), for jumping to specific parts (25%, 30%, 50%, 75%).

@rodrigomorales1
Copy link
Author

rodrigomorales1 commented Jul 2, 2024

I had another idea that has to do with jumping to specific parts in mpv: We could define functions for seeking backward or seeking forward (5s, 10s) from the current playback position.

My use case: I sometimes use programs for automatically generating subtitles for audio files, those subtitles contain some errors, so I need to manually fix them (e.g. split long subtitles, correct time span). While I'm splitting subtitles, I need to listen the part that comes after the point and then go back to the point I was in to make sure that I'm splitting a subtitle in the right position, in this case I wish I could seek backward.

@sachac
Copy link
Owner

sachac commented Oct 18, 2024

That could be fun. =) I think I've mostly been using middle-click in subed-waveform to sample sound without affecting the playback position, but something that's more keyboard-focused might work for your workflow. Have you figured out keyboard shortcuts and functions that make sense to you? (No rush, just curious!)

@sachac
Copy link
Owner

sachac commented Oct 18, 2024

Or maybe if there was the idea of some kind of register, like the way you can save Emacs positions to a register and then jump back to them...

@rodrigomorales1
Copy link
Author

rodrigomorales1 commented Oct 27, 2024

Have you figured out keyboard shortcuts and functions that make sense to you? (No rush, just curious!)

@sachac In GNU Emacs 29.4, M-; is mapped to comment-dwim and M-' is mapped to abbrev-previx-mark, both in global-map. I have never used those functions while editing subtitles so I ended up using M-; for seeking backward and M-' for seeking forward. Below I share a minimal reproducible example for the functions that I defined.

In the video below, I show how seeking helps me listen specific parts of a subtitle. Note that the benefit of seeking is that I don't have to listen the entire subtitle again in order to listen a specific part. The following list explains what happens in the video:

  1. The point is on the first subtitle
  2. I move the point to the second subtitle
  3. I play the audio
  4. I pause mpv
  5. I search the string a to move the point where I want to split the subtitle
  6. I press M-; (bound to my-subed-mpv-seek-backward) repeatedly to seek backward so that I can listen one more time the part where I think I should split the subtitle. When seeking backward, the vertical line which is shown in the waveform is moved.
  7. I pause mpv
  8. I press M-; (bound to my-subed-mpv-seek-backward) to seek backward again to move the vertical line to the specific part where I want to split the subtitle.
  9. I press M-. to split the subtitle (by default, M-. is bound to subed-split-subtitle in subed-srt-mode-map)
output.webm

I used the command in the first code block below to download the sample video and the sample subtitle file shown in the video. I created the file /tmp/subtitles/init.el with the contents of the second code block. I started Emacs using the command in the third code block below.

rm -rf /tmp/subtitles \
  && mkdir /tmp/subtitles \
  && curl \
       --output /tmp/subtitles/video.ogv \
       'https://upload.wikimedia.org/wikipedia/commons/4/41/Creative_Commons_and_Commerce.ogv' \
  && curl \
       --data-urlencode 'format=json' \
       --data-urlencode 'action=query' \
       --data-urlencode 'prop=revisions' \
       --data-urlencode 'rvprop=content' \
       --data-urlencode 'rvslots=*' \
       --data-urlencode 'formatversion=2' \
       --data-urlencode 'titles=TimedText:Creative_Commons_and_Commerce.ogv.en.srt' \
       'https://commons.wikimedia.org/w/api.php' | jq --raw-output '.query.pages.[].revisions.[].slots.main.content' > /tmp/subtitles/video.en.srt
(add-to-list 'load-path "/tmp/subed/subed")

(require 'subed-srt)
(require 'subed-waveform)

(add-hook 'subed-mode-hook 'subed-enable-sync-player-to-point)
(add-hook 'subed-mode-hook 'subed-enable-loop-over-current-subtitle)
(add-hook 'subed-mode-hook 'subed-enable-replay-adjusted-subtitle)
(add-hook 'subed-mode-hook 'subed-waveform-minor-mode)
(add-hook 'subed-mode-hook 'my-subed-custom-keys-minor-mode)

(defun my-subed-mpv-seek-backward ()
  (interactive)
  (subed-mpv-seek -100))

(defun my-subed-mpv-seek-forward ()
  (interactive)
  (subed-mpv-seek 100))

(define-minor-mode
  my-subed-custom-keys-minor-mode
  "Custom keys for `subed-mode'."
  :keymap (let ((map (make-sparse-keymap)))
            (define-key map (kbd "M-;") 'my-subed-mpv-seek-backward)
            (define-key map (kbd "M-'") 'my-subed-mpv-seek-forward)
            map))
emacs \
  --quick \
  --load /tmp/subtitles/init.el \
  /tmp/subtitles/video.en.srt

Sidenote: I initially created the issue for functions that would allow to jump to the 25%, 50% and 75% of the audio, but now I believe that seeking is more needed, so we should focus our attention on seeking and then we can focus on those functions for jumping to 25%, 50% and 75%.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants