emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/subed 3eca1828e6 3/3: subed-word-data-fix-subtitle-timing:


From: ELPA Syncer
Subject: [nongnu] elpa/subed 3eca1828e6 3/3: subed-word-data-fix-subtitle-timing: New command
Date: Tue, 17 Dec 2024 16:02:55 -0500 (EST)

branch: elpa/subed
commit 3eca1828e6fdbb99cf0705cd12c03e3fd37748c1
Author: Sacha Chua <sacha@sachachua.com>
Commit: Sacha Chua <sacha@sachachua.com>

    subed-word-data-fix-subtitle-timing: New command
    
    * subed/subed-word-data.el (subed-word-data-fix-subtitle-timing): New.
---
 subed/subed-word-data.el | 63 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/subed/subed-word-data.el b/subed/subed-word-data.el
index 20cfeecaf0..fbc7cb20ff 100644
--- a/subed/subed-word-data.el
+++ b/subed/subed-word-data.el
@@ -367,6 +367,69 @@ Requires the text properties to be set."
         (setq last-start-time nil)))
     (goto-char max-pos)))
 
+(defun subed-word-data-fix-subtitle-timing (beg end)
+  "Sets subtitle starts and stops based on the word data.
+Assumes words haven't been edited."
+  (interactive (list (if (region-active-p) (min (point) (mark)))
+                     (if (region-active-p) (max (point) (mark)))))
+  (setq beg (or beg (point-min)))
+  (setq end (if end (save-excursion
+                      (goto-char end)
+                      (subed-jump-to-subtitle-end)
+                      (point))
+              (point-max)))
+  (goto-char beg)
+  (if (subed-subtitle-msecs-start)
+      (subed-jump-to-subtitle-text)
+    (subed-forward-subtitle-text))
+  (let* ((start-ms (save-excursion
+                     (goto-char beg)
+                     (or (subed-subtitle-msecs-start)
+                         (progn
+                           (subed-forward-subtitle-time-start)
+                           (subed-subtitle-msecs-start)))))
+         (data (seq-drop-while
+                (lambda (o)
+                  (< (or (alist-get 'start o) 0)
+                     start-ms))
+                subed-word-data--cache))
+                                min-distance
+                                current-distance
+                                current-sub
+                                num-words
+                                candidate
+                                test-distance)
+    (while (and (not (> (point) end)) data)
+      (setq current-sub (replace-regexp-in-string "\n" " " 
(subed-subtitle-text)))
+      (setq num-words (- (length (split-string current-sub " ")) 2))
+      (setq candidate (mapconcat (lambda (o) (alist-get 'text o))
+                                 (seq-take data num-words) " "))
+      (setq current-distance (string-distance current-sub candidate))
+      ;; add another word to see if the distance goes down
+      (setq test-distance (string-distance current-sub
+                                           (mapconcat (lambda (o) (alist-get 
'text o))
+                                                      (seq-take data (1+ 
num-words)) " ")))
+      (while (< test-distance current-distance)
+        (setq current-distance test-distance
+              num-words (1+ num-words)
+              test-distance (string-distance current-sub
+                                             (mapconcat (lambda (o) (alist-get 
'text o))
+                                                        (seq-take data (1+ 
num-words)) " "))))
+      (subed-set-subtitle-time-start
+       (alist-get
+        'start
+        (seq-find (lambda (o) (alist-get 'start o))
+                  (seq-take data num-words))))
+      (subed-set-subtitle-time-stop
+       (alist-get
+        'end
+        (seq-find (lambda (o) (alist-get 'end o))
+                  (reverse (seq-take data num-words)))))
+      (subed-word-data-refresh-text-properties-for-subtitle)
+      (setq data (seq-drop data num-words))
+      (unless (subed-forward-subtitle-text)
+        (goto-char (point-max))))))
+
 (with-eval-after-load 'subed-align
   (add-hook 'subed-align-region-hook #'subed-word-data-refresh-region))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]