From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: joakim@verona.se Newsgroups: gmane.emacs.devel Subject: Re: rescale srt files Date: Wed, 08 Sep 2010 10:57:55 +0200 Message-ID: References: <13770423.659811283935719877.JavaMail.www@wwinf4633> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1283936298 27456 80.91.229.12 (8 Sep 2010 08:58:18 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Sep 2010 08:58:18 +0000 (UTC) Cc: "Emacs Dev \[emacs-devel\]" To: alinsoar@voila.fr Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 08 10:58:16 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OtGTv-0004O5-IQ for ged-emacs-devel@m.gmane.org; Wed, 08 Sep 2010 10:58:16 +0200 Original-Received: from localhost ([127.0.0.1]:50023 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OtGTu-0003gx-Ss for ged-emacs-devel@m.gmane.org; Wed, 08 Sep 2010 04:58:14 -0400 Original-Received: from [140.186.70.92] (port=33674 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OtGTn-0003gs-PY for emacs-devel@gnu.org; Wed, 08 Sep 2010 04:58:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OtGTm-00077b-2z for emacs-devel@gnu.org; Wed, 08 Sep 2010 04:58:07 -0400 Original-Received: from smtprelay-b12.telenor.se ([62.127.194.21]:43074) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OtGTl-00075f-Mg for emacs-devel@gnu.org; Wed, 08 Sep 2010 04:58:06 -0400 Original-Received: from ipb4.telenor.se (ipb4.telenor.se [195.54.127.167]) by smtprelay-b12.telenor.se (Postfix) with ESMTP id 664B2C317 for ; Wed, 8 Sep 2010 10:57:58 +0200 (CEST) X-SENDER-IP: [83.227.138.150] X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmoxAInshkxT44qWPGdsb2JhbAChDAwBAQEBNS29doU9BIoXgw4 X-IronPort-AV: E=Sophos;i="4.56,333,1280700000"; d="scan'208";a="1668583361" Original-Received: from ua-83-227-138-150.cust.bredbandsbolaget.se (HELO www.verona.se) ([83.227.138.150]) by ipb4.telenor.se with ESMTP; 08 Sep 2010 10:57:57 +0200 Original-Received: from localhost.localdomain (unknown [192.168.201.6]) by www.verona.se (Postfix) with ESMTP id 0323472EF40; Wed, 8 Sep 2010 10:57:55 +0200 (CEST) In-Reply-To: <13770423.659811283935719877.JavaMail.www@wwinf4633> (A. Soare's message of "Wed, 8 Sep 2010 10:48:39 +0200 (CEST)") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:129771 Archived-At: "A. Soare" 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=E9couvrez les tendances de la rentr=E9e : coiffure, mode, sports ou cu= isine sur http://actu.voila.fr/evenementiel/rentree2010/=20 > > > (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) (len= gth 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) >=20=20=20=20 > ) --=20 Joakim Verona