unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* rescale srt files
@ 2010-09-08  8:48 A. Soare
  2010-09-08  8:57 ` joakim
  2010-09-09 19:49 ` A Soare
  0 siblings, 2 replies; 4+ messages in thread
From: A. Soare @ 2010-09-08  8:48 UTC (permalink / raw)
  To: Emacs   Dev  [emacs-devel]

[-- Attachment #1: Type: text/plain, Size: 371 bytes --]

I attach here a procedure to rescale the srt files. I use it all the time to change the speed and the offset of a subtitle file. It can also join 2 files in only one.








____________________________________________________

 Découvrez les tendances de la rentrée : coiffure, mode, sports ou cuisine  sur http://actu.voila.fr/evenementiel/rentree2010/ 

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: re-titrage2.el --]
[-- Type: text/x-emacs-lisp; name=re-titrage2.el, Size: 4990 bytes --]


(defun dialog-create (INDEX START-TIME END-TIME TEXT)
  (list INDEX START-TIME END-TIME TEXT) )

(defun dialog-index (what)
  (let ((vector '( INDEX START-TIME END-TIME TEXT ) )
	(r nil))
    (dotimes (x 4 r ) (if (eq (nth x vector) what) (setq r x) ) ) ) )

(defun dialog-get (what dialog)
  (let ((pos (dialog-index what) ) )
    (nth pos dialog) ) )

(defun dialog-set (what new dialog)
  (let ((pos (dialog-index what) ) )
    (setcar (nthcdr pos dialog) new) )
  dialog )

(defun timestamp-to-miliseconds (H M S MS)
  "convert a list (HOUR MINUTE SECONDS MILISECONDS) to
 TIMESTAMP (MILISECONDS)"
  (+ MS (* 1000 (+ S (* M 60) (* H 3600) ) ) ) )

(defun miliseconds-to-timestamp (TIMESTAMP)
  "convert TIMESTAMP (MILISECONDS) to the list
 (HOUR MINUTE SECONDS MILISECONDS)"
  (let (MS S M H)
    (setq
     MS (% TIMESTAMP 1000)		; miliseconds
     TIMESTAMP (/ (- TIMESTAMP MS) 1000)
     S (% TIMESTAMP 60)			; seconds
     TIMESTAMP (/ (- TIMESTAMP S) 60)
     M (% TIMESTAMP 60)			; minutes
     H (/ (- TIMESTAMP M) 60) )		; hours
    (list H M S MS) ) )

(defun read-text (limit &optional skip-first type)
  (and skip-first
       (re-search-forward skip-first) )
  (let ((data (buffer-substring (point) (- (re-search-forward limit) (length limit) ) ) ) )
    (if (eq type 'S)
	data
      (string-to-number data ) ) ) )

;; 608
;; 1:0:27,650 --> 1:0:34,849
;; One hunk starts at `608', and ends before `609'.
;; This is the text of the current hunk.
;;
;; 609
;; 1:0:38,723 --> 1:0:41,839
;; TEXT-HUNK `609'
(defun read-one-hunk nil
  (setq index (+ (read-text "\n") last-index-first-file))
  (let ((HHs (read-text ":"))
	(MMs (read-text ":"))
	(SSs (read-text ","))
	(MSs (read-text " "))
	(HHe (read-text ":" "-->") )
	(MMe (read-text ":"))
	(SSe (read-text ","))
	(MSe (read-text "\n"))
	(text (read-text "\n\n" nil 'S) ) )
    ;; jumps to the beginning of the next hunk
    (skip-chars-forward "[:blank:]\n")
    (dialog-create
     index
     (+ (timestamp-to-miliseconds HHs MMs SSs MSs) offset )
     (+ (timestamp-to-miliseconds HHe MMe SSe MSe) offset )
     text ) ) )

(defun read-file-srt (file offset)
  (let (index prev-index)
    (with-current-buffer (find-file-noselect file)
      (goto-char 1)
      (while (> (buffer-size) (point) )
	(push (read-one-hunk) titrage)
	(if (and prev-index (> 1 (- index prev-index ) ) )
	    (error "srt file contains errrors [ at the index %d ]" prev-index) )
	(setq prev-index index) )
      (kill-buffer (current-buffer) ) ) ) )

(defun re-titrage
  (File1
   first-required-dialog-time
   first-index
   second-required-dialog-time
   second-index
   &optional File2
   length-first-part
   )
  (let ((last-index-first-file 0)
	titrage
	first-orginal-dialog-time
	second-orginal-dialog-time)
    (read-file-srt File1 0)
    (setq last-index-first-file (dialog-get 'INDEX (car titrage) ) )
    (and File2 (read-file-srt File2 length-first-part) )
    (setq titrage (nreverse titrage))

    (setq
     second-orginal-dialog-time
     (dialog-get 'START-TIME (nth (1- second-index) titrage) )
     first-orginal-dialog-time
     (dialog-get 'START-TIME (nth (1- first-index) titrage) ) )

    (and
     first-required-dialog-time
     (progn
       (let ((speed
	      (/ (float (- second-required-dialog-time
			   first-required-dialog-time) )
		 (float (- second-orginal-dialog-time
			   first-orginal-dialog-time) ) ) ) )
       (dolist (x titrage)
	 (dialog-set
	  'START-TIME
	  (+ first-required-dialog-time
	     (truncate (* (- (dialog-get 'START-TIME x)
			     first-orginal-dialog-time)
			  speed) ) )
	  x )
	 (dialog-set
	  'END-TIME
	  (+ first-required-dialog-time
	     (truncate (* (- (dialog-get 'END-TIME x)
			     first-orginal-dialog-time)
			  speed) ) )
	  x ) ) ) ) )
    titrage) )

(defun make-new-file (titrage)
  (setq default-enable-multibyte-characters nil)
  (set-buffer-multibyte nil)
  (set-buffer (find-file "new") )
  (dolist (x titrage)
    (set 'stime (miliseconds-to-timestamp (dialog-get 'START-TIME x) ) )
    (set 'etime (miliseconds-to-timestamp (dialog-get 'END-TIME x) ) )
    (insert (format "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n%s\n\n"
                    (dialog-get 'INDEX x)
                    (car stime) (cadr stime) (nth 2 stime) (nth 3 stime)
                    (car etime) (cadr etime) (nth 2 etime) (nth 3 etime)
                    (dialog-get 'TEXT x) ) ) ) )

(make-new-file
 (re-titrage
  "/1"
  ;; NIL lorsqu'on veut enchainer 2 fichiers
  ;; la vitesse demeure toujours constante a 1
  (timestamp-to-miliseconds 0 3 34 0)
  ;; nil
  ;; INDEX 1 ou NIL
  16
  ;; le deuxieme dialogue
  (timestamp-to-miliseconds 1 37 51 0 )
  ;; INDEX 2
  1467
  ;; le deuxieme fichier (quand existant) va avoir la meme vitesse que
  ;; le premier.
  ;;
  ;; "/2"
  ;;
  ;; lorsqu'on a la deuxieme partie, on est cense d'y pourvoir le
  ;; moment de la fin de la premiere partie
  ;;
  ;; (timestamp-to-miliseconds 1 7 6 0 )
  ) )


'( (eval-current-buffer)
   
   )

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: rescale srt files
  2010-09-08  8:48 rescale srt files A. Soare
@ 2010-09-08  8:57 ` joakim
  2010-09-08  9:04   ` Leo
  2010-09-09 19:49 ` A Soare
  1 sibling, 1 reply; 4+ messages in thread
From: joakim @ 2010-09-08  8:57 UTC (permalink / raw)
  To: alinsoar; +Cc: Emacs Dev [emacs-devel]

"A. Soare" <alinsoar@voila.fr> writes:

> I attach here a procedure to rescale the srt files. I use it all the time to change the speed and the offset of a subtitle file. It can also join 2 files in only one.

Interesting! I will probably have use for your code.

However, I believe you will get better exposure for this type of progam
on the emacs-sources list, and the emacswiki.

>
>
>
>
>
>
>
> ____________________________________________________
>
>  Découvrez les tendances de la rentrée : coiffure, mode, sports ou cuisine  sur http://actu.voila.fr/evenementiel/rentree2010/ 
>
>
> (defun dialog-create (INDEX START-TIME END-TIME TEXT)
>   (list INDEX START-TIME END-TIME TEXT) )
>
> (defun dialog-index (what)
>   (let ((vector '( INDEX START-TIME END-TIME TEXT ) )
> 	(r nil))
>     (dotimes (x 4 r ) (if (eq (nth x vector) what) (setq r x) ) ) ) )
>
> (defun dialog-get (what dialog)
>   (let ((pos (dialog-index what) ) )
>     (nth pos dialog) ) )
>
> (defun dialog-set (what new dialog)
>   (let ((pos (dialog-index what) ) )
>     (setcar (nthcdr pos dialog) new) )
>   dialog )
>
> (defun timestamp-to-miliseconds (H M S MS)
>   "convert a list (HOUR MINUTE SECONDS MILISECONDS) to
>  TIMESTAMP (MILISECONDS)"
>   (+ MS (* 1000 (+ S (* M 60) (* H 3600) ) ) ) )
>
> (defun miliseconds-to-timestamp (TIMESTAMP)
>   "convert TIMESTAMP (MILISECONDS) to the list
>  (HOUR MINUTE SECONDS MILISECONDS)"
>   (let (MS S M H)
>     (setq
>      MS (% TIMESTAMP 1000)		; miliseconds
>      TIMESTAMP (/ (- TIMESTAMP MS) 1000)
>      S (% TIMESTAMP 60)			; seconds
>      TIMESTAMP (/ (- TIMESTAMP S) 60)
>      M (% TIMESTAMP 60)			; minutes
>      H (/ (- TIMESTAMP M) 60) )		; hours
>     (list H M S MS) ) )
>
> (defun read-text (limit &optional skip-first type)
>   (and skip-first
>        (re-search-forward skip-first) )
>   (let ((data (buffer-substring (point) (- (re-search-forward limit) (length limit) ) ) ) )
>     (if (eq type 'S)
> 	data
>       (string-to-number data ) ) ) )
>
> ;; 608
> ;; 1:0:27,650 --> 1:0:34,849
> ;; One hunk starts at `608', and ends before `609'.
> ;; This is the text of the current hunk.
> ;;
> ;; 609
> ;; 1:0:38,723 --> 1:0:41,839
> ;; TEXT-HUNK `609'
> (defun read-one-hunk nil
>   (setq index (+ (read-text "\n") last-index-first-file))
>   (let ((HHs (read-text ":"))
> 	(MMs (read-text ":"))
> 	(SSs (read-text ","))
> 	(MSs (read-text " "))
> 	(HHe (read-text ":" "-->") )
> 	(MMe (read-text ":"))
> 	(SSe (read-text ","))
> 	(MSe (read-text "\n"))
> 	(text (read-text "\n\n" nil 'S) ) )
>     ;; jumps to the beginning of the next hunk
>     (skip-chars-forward "[:blank:]\n")
>     (dialog-create
>      index
>      (+ (timestamp-to-miliseconds HHs MMs SSs MSs) offset )
>      (+ (timestamp-to-miliseconds HHe MMe SSe MSe) offset )
>      text ) ) )
>
> (defun read-file-srt (file offset)
>   (let (index prev-index)
>     (with-current-buffer (find-file-noselect file)
>       (goto-char 1)
>       (while (> (buffer-size) (point) )
> 	(push (read-one-hunk) titrage)
> 	(if (and prev-index (> 1 (- index prev-index ) ) )
> 	    (error "srt file contains errrors [ at the index %d ]" prev-index) )
> 	(setq prev-index index) )
>       (kill-buffer (current-buffer) ) ) ) )
>
> (defun re-titrage
>   (File1
>    first-required-dialog-time
>    first-index
>    second-required-dialog-time
>    second-index
>    &optional File2
>    length-first-part
>    )
>   (let ((last-index-first-file 0)
> 	titrage
> 	first-orginal-dialog-time
> 	second-orginal-dialog-time)
>     (read-file-srt File1 0)
>     (setq last-index-first-file (dialog-get 'INDEX (car titrage) ) )
>     (and File2 (read-file-srt File2 length-first-part) )
>     (setq titrage (nreverse titrage))
>
>     (setq
>      second-orginal-dialog-time
>      (dialog-get 'START-TIME (nth (1- second-index) titrage) )
>      first-orginal-dialog-time
>      (dialog-get 'START-TIME (nth (1- first-index) titrage) ) )
>
>     (and
>      first-required-dialog-time
>      (progn
>        (let ((speed
> 	      (/ (float (- second-required-dialog-time
> 			   first-required-dialog-time) )
> 		 (float (- second-orginal-dialog-time
> 			   first-orginal-dialog-time) ) ) ) )
>        (dolist (x titrage)
> 	 (dialog-set
> 	  'START-TIME
> 	  (+ first-required-dialog-time
> 	     (truncate (* (- (dialog-get 'START-TIME x)
> 			     first-orginal-dialog-time)
> 			  speed) ) )
> 	  x )
> 	 (dialog-set
> 	  'END-TIME
> 	  (+ first-required-dialog-time
> 	     (truncate (* (- (dialog-get 'END-TIME x)
> 			     first-orginal-dialog-time)
> 			  speed) ) )
> 	  x ) ) ) ) )
>     titrage) )
>
> (defun make-new-file (titrage)
>   (setq default-enable-multibyte-characters nil)
>   (set-buffer-multibyte nil)
>   (set-buffer (find-file "new") )
>   (dolist (x titrage)
>     (set 'stime (miliseconds-to-timestamp (dialog-get 'START-TIME x) ) )
>     (set 'etime (miliseconds-to-timestamp (dialog-get 'END-TIME x) ) )
>     (insert (format "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n%s\n\n"
>                     (dialog-get 'INDEX x)
>                     (car stime) (cadr stime) (nth 2 stime) (nth 3 stime)
>                     (car etime) (cadr etime) (nth 2 etime) (nth 3 etime)
>                     (dialog-get 'TEXT x) ) ) ) )
>
> (make-new-file
>  (re-titrage
>   "/1"
>   ;; NIL lorsqu'on veut enchainer 2 fichiers
>   ;; la vitesse demeure toujours constante a 1
>   (timestamp-to-miliseconds 0 3 34 0)
>   ;; nil
>   ;; INDEX 1 ou NIL
>   16
>   ;; le deuxieme dialogue
>   (timestamp-to-miliseconds 1 37 51 0 )
>   ;; INDEX 2
>   1467
>   ;; le deuxieme fichier (quand existant) va avoir la meme vitesse que
>   ;; le premier.
>   ;;
>   ;; "/2"
>   ;;
>   ;; lorsqu'on a la deuxieme partie, on est cense d'y pourvoir le
>   ;; moment de la fin de la premiere partie
>   ;;
>   ;; (timestamp-to-miliseconds 1 7 6 0 )
>   ) )
>
>
> '( (eval-current-buffer)
>    
>    )

-- 
Joakim Verona



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: rescale srt files
  2010-09-08  8:57 ` joakim
@ 2010-09-08  9:04   ` Leo
  0 siblings, 0 replies; 4+ messages in thread
From: Leo @ 2010-09-08  9:04 UTC (permalink / raw)
  To: emacs-devel

On 2010-09-08 09:57 +0100, joakim@verona.se wrote:
> Interesting! I will probably have use for your code.
>
> However, I believe you will get better exposure for this type of progam
> on the emacs-sources list, and the emacswiki.

Maybe make it an extension to emms.

Leo




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: rescale srt files
  2010-09-08  8:48 rescale srt files A. Soare
  2010-09-08  8:57 ` joakim
@ 2010-09-09 19:49 ` A Soare
  1 sibling, 0 replies; 4+ messages in thread
From: A Soare @ 2010-09-09 19:49 UTC (permalink / raw)
  To: emacs-devel


I wrote that script in 2006, and used it all the time since. I re wrote it
almost completely before yesterday.

Today I did a few minor changes.

I attach here again the last versin. If you need it, use and modify it as you
wish. The same if you want to install it in emacs. You are free to modify it to
conform to the emacs' writinf rules.





------------------------------------------ ***







(defun dialog-create (INDEX START-TIME END-TIME TEXT)
  (list INDEX START-TIME END-TIME TEXT) )

(defun dialog-index (what)
  (let ((vector '( INDEX START-TIME END-TIME TEXT ) )
	(r nil))
    (dotimes (x 4 r ) (if (eq (nth x vector) what) (setq r x) ) ) ) )

(defun dialog-get (what dialog)
  (let ((pos (dialog-index what) ) )
    (nth pos dialog) ) )

(defun dialog-set (what new dialog)
  (let ((pos (dialog-index what) ) )
    (setcar (nthcdr pos dialog) new) )
  dialog )

(defun timestamp-to-miliseconds (H M S MS)
  "convert a group (HOUR MINUTE SECONDS MILISECONDS) to
 TIMESTAMP (MILISECONDS)"
  (+ MS (* 1000 (+ S (* M 60) (* H 3600) ) ) ) )

(defun miliseconds-to-timestamp (TIMESTAMP)
  "convert TIMESTAMP (MILISECONDS) to the list
 (HOUR MINUTE SECONDS MILISECONDS)"
  (let (MS S M H)
    (setq
     MS (% TIMESTAMP 1000)		; miliseconds
     TIMESTAMP (/ (- TIMESTAMP MS) 1000)
     S (% TIMESTAMP 60)			; seconds
     TIMESTAMP (/ (- TIMESTAMP S) 60)
     M (% TIMESTAMP 60)			; minutes
     H (/ (- TIMESTAMP M) 60) )		; hours
    (list H M S MS) ) )

(defun read-text (limit &optional skip-first type)
  (and skip-first
       (re-search-forward skip-first) )
  (let ((data (buffer-substring
	       (point)
	       (- (re-search-forward limit)
		  (length limit) ) ) ) )
    (if (eq type 'S)
	data
      (string-to-number data ) ) ) )

;; 608
;; 1:0:27,650 --> 1:0:34,849
;; One hunk starts at `608', and ends before `609'.
;; This is the text of the current hunk.
;;
;; 609
;; 1:0:38,723 --> 1:0:41,839
;; TEXT-HUNK `609'
(defun read-one-hunk nil
  (setq index (+ (read-text "\n") last-index-first-file) )
  (let ((HHs (read-text ":") )
	(MMs (read-text ":") )
	(SSs (read-text ",") )
	(MSs (read-text " ") )
	(HHe (read-text ":" "-->") )
	(MMe (read-text ":") )
	(SSe (read-text ",") )
	(MSe (read-text "\n") )
	(text (read-text "\n\n" nil 'S) ) )
    ;; jumps to the beginning of the next hunk
    (skip-chars-forward "[:blank:]\n")
    (dialog-create
     index
     (+ (timestamp-to-miliseconds HHs MMs SSs MSs) offset )
     (+ (timestamp-to-miliseconds HHe MMe SSe MSe) offset )
     text ) ) )

(defun write-one-hunk (hunk)
    (set 'stime (dialog-get 'START-TIME hunk))
    (set 'etime (dialog-get 'END-TIME hunk))
    (insert (format "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n%s\n\n"
                    (dialog-get 'INDEX hunk)
                    (car stime) (cadr stime) (nth 2 stime) (nth 3 stime)
                    (car etime) (cadr etime) (nth 2 etime) (nth 3 etime)
                    (dialog-get 'TEXT hunk) ) ) )

(defun read-file-srt (file offset)
  (let (index prev-index)
    (with-current-buffer (find-file-noselect file)
      (goto-char 1)
      (while (> (buffer-size) (point) )
	(push (read-one-hunk) titrage)
	(if (and prev-index (> 1 (- index prev-index ) ) )
	    (error "srt file contains errrors [ at the index %d ]" prev-index) )
	(setq prev-index index) )
      (kill-buffer (current-buffer) ) ) ) )

(defun rescale (what)
  (+ first-required-dialog-time
     (truncate (* (- (dialog-get what x)
		     first-orginal-dialog-time)
		  speed) ) ) )

(defun re-titrage
  (File1
   first-required-dialog-time
   first-index
   second-required-dialog-time
   second-index
   &optional File2
   length-first-part
   )
  (let ((last-index-first-file 0)
	titrage
	first-orginal-dialog-time
	second-orginal-dialog-time)
    (read-file-srt File1 0)
    (setq last-index-first-file (dialog-get 'INDEX (car titrage) ) )
    (and File2 (read-file-srt File2 length-first-part) )
    (setq titrage (nreverse titrage))

    (setq
     second-orginal-dialog-time
     (dialog-get 'START-TIME (nth (1- second-index) titrage) )
     first-orginal-dialog-time
     (dialog-get 'START-TIME (nth (1- first-index) titrage) ) )

     (progn
       (let ((speed
	      (/ (float (- second-required-dialog-time
			   first-required-dialog-time) )
		 (float (- second-orginal-dialog-time
			   first-orginal-dialog-time) ) ) ) )
       (dolist (x titrage)
	(let ((new-start-time
	       (miliseconds-to-timestamp 
		(if first-required-dialog-time
		    (rescale 'START-TIME)
		  (dialog-get 'START-TIME x) ) ) )
	      (new-end-time
	       (miliseconds-to-timestamp
		(if first-required-dialog-time
		    (rescale 'END-TIME)
		(dialog-get 'END-TIME x) ) ) ) )
	  (dialog-set 'START-TIME new-start-time x )
	  (dialog-set 'END-TIME new-end-time x ) ) ) ) )
     titrage ) )

(defun make-new-file (titrage)
  (setq default-enable-multibyte-characters nil)
  (set-buffer-multibyte nil)
  (set-buffer (find-file "new") )
  (dolist (x titrage)
	(write-one-hunk x) ) )

(make-new-file
 (re-titrage
  "/1"
  ;; first-required-dialog-time = NIL lorsqu'on veut enchainer 2 fichiers
  ;; (la vitesse demeure toujours constante a 1 en ce cas)
  (timestamp-to-miliseconds 0 3 34 0)
  ;; nil
  ;; INDEX 1
  16
  ;; le deuxieme dialogue
  (timestamp-to-miliseconds 1 37 51 0 )
  ;; INDEX 2
  1467
  ;; le deuxieme fichier (quand existant) va avoir la meme vitesse que
  ;; le premier ( 1 lorsque first-required-dialog-time est NIL. )
  ;;
  ;; "/2"
  ;;
  ;; lorsqu'on a la deuxieme partie, on est cense d'y pourvoir la
  ;; longueur de la premiere partie
  ;;
  ;; (timestamp-to-miliseconds 1 7 6 0 )
  ) )


'( (eval-current-buffer)
   
   )






^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-09-09 19:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-08  8:48 rescale srt files A. Soare
2010-09-08  8:57 ` joakim
2010-09-08  9:04   ` Leo
2010-09-09 19:49 ` A Soare

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).