Last active
December 27, 2015 01:28
-
-
Save gempesaw/7244723 to your computer and use it in GitHub Desktop.
Convenience functions for shifting between milliseconds and 00:00:00,000 format along with an interactive function to rewrite each of the timers in a .srt file. Invoking `convert-next-time` will run through the whole file and try to avoid getting stopped by `max-specpdl-size`. That's probably bad, for bigger .srt files...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defvar time-multipliers `(,(* 60 60 1000) ,(* 60 1000) 1000 1) | |
| "Multipliers for going to and from milliseconds and | |
| 00:00:00,000 format") | |
| (defvar subtitle-offset "00:00:04,764" | |
| "Amount in 00:00:00,000 format of offset to shift the values | |
| by ") | |
| (ert-deftest subtitle-shift-tests () | |
| (let ((max-lisp-eval-depth 1500) | |
| (max-specpdl-size 2200)) | |
| (should (eq 40271111 (time-to-ms "11:11:11,111"))) | |
| (should (string= "75:30:28,182" (ms-to-time 271828182))) | |
| (with-temp-buffer | |
| (let ((subtitle-offset (ms-to-time 1000))) | |
| (insert "01:08:15,830 --> 01:08:20,670") | |
| (goto-char (point-min)) | |
| (convert-next-time) | |
| (should (string= "01:08:14,830 --> 01:08:19,670" | |
| (buffer-substring-no-properties (point-min) (point-max)))))))) | |
| (defun time-to-ms (time) | |
| (apply '+ | |
| (mapcar* '* | |
| (mapcar 'string-to-number | |
| (s-split "[:,]" time)) | |
| time-multipliers))) | |
| (defun ms-to-time (time) | |
| (defun ms-to-time-iter (time h m s ms) | |
| (let ((multiplier time-multipliers) | |
| (threshold)) | |
| (cond | |
| ((> time (setq threshold (pop multiplier))) | |
| (ms-to-time-iter (- time threshold) (+ h 1) m s ms)) | |
| ((> time (setq threshold (pop multiplier))) | |
| (ms-to-time-iter (- time threshold) h (+ m 1) s ms)) | |
| ((> time (setq threshold (pop multiplier))) | |
| (ms-to-time-iter (- time threshold) h m (+ s 1) ms)) | |
| (t | |
| (format "%02d:%02d:%02d,%03d" h m s time))))) | |
| (ms-to-time-iter time 0 0 0 0)) | |
| (defun convert-next-time (&optional recurse) | |
| (interactive "p") | |
| (search-forward-regexp "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]" nil) | |
| (let* ((times (s-split " --> " | |
| (buffer-substring-no-properties | |
| (line-beginning-position) | |
| (line-end-position)))) | |
| (offset (time-to-ms subtitle-offset))) | |
| (delete-region (line-beginning-position) | |
| (line-end-position)) | |
| (move-beginning-of-line 1) | |
| (insert (format "%s --> %s" | |
| (ms-to-time (- (time-to-ms (car times)) offset)) | |
| (ms-to-time (- (time-to-ms (cadr times)) offset)))) | |
| (unless (eq nil recurse) | |
| (setq max-lisp-eval-depth (+ 1 max-lisp-eval-depth)) | |
| (setq max-specpdl-size (+ 1 max-specpdl-size)) | |
| (convert-next-time)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment