5 ;;; srt2vtt --- SRT to WebVTT converter
6 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
8 ;;; srt2vtt is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or
11 ;;; (at your option) any later version.
13 ;;; srt2vtt is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;;; General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with srt2vtt. If not, see <http://www.gnu.org/licenses/>.
23 ;; Convert SRT formatted subtitles to WebVTT format.
27 (use-modules (ice-9 format)
37 (define-record-type <subtitle>
38 (make-subtitle id start end text)
41 (start subtitle-start)
46 (let ((regexp (make-regexp "([0-9]+):([0-9]+):([0-9]+),([0-9]+)")))
48 "Parse the SubRip formatted timestamp in the string S into a 4
49 element list. Valid input looks like '00:00:03.417'."
50 (let ((match (regexp-exec regexp s)))
51 (map (cut match:substring match <>) '(1 2 3 4))))))
53 (define parse-time-span
54 (let ((regexp (make-regexp "([0-9:,]+) --> ([0-9:,]+)")))
56 "Parse the SubRip formatted time span in the string S and return
57 two values: the start time and the end time. Valid input looks like
58 '00:00:03.417 --> 00:00:04.936'."
59 (let ((match (regexp-exec regexp s)))
60 (values (parse-time (match:substring match 1))
61 (parse-time (match:substring match 2)))))))
63 (define (read-sub-rip port)
64 "Read a SubRip formatted subtitle from PORT."
65 (let-values (((id) (string->number (read-line port)))
66 ((start end) (parse-time-span (read-line port)))
67 ((lines) (let loop ((lines '()))
68 (let ((line (read-line port)))
69 (if (or (eof-object? line)
70 (and (string-null? line)
71 ;; A subtitle may be a blank line!
74 (loop (cons line lines)))))))
75 (make-subtitle id start end lines)))
77 (define (read-sub-rips port)
78 "Read all SubRip formatted subtitles from PORT."
80 (let loop ((subs '()))
81 (if (eof-object? (peek-char port))
83 (loop (cons (read-sub-rip port) subs))))))
85 (define (write-time time port)
86 "Write TIME as a WebVTT formatted timestamp to PORT."
89 (format port "~a:~a:~a.~a" h m s ms))))
91 (define (write-web-vtt subtitle port)
92 "Write SUBTITLE as a WebVTT formatted subtitle to PORT."
94 (($ <subtitle> id start end text)
95 (format port "~a~%" id)
96 (write-time start port)
97 (display " --> " port)
100 (format port "~a~%" (string-join text "\n"))
103 (define (write-web-vtts subtitles port)
104 "Write all SUBTITLES as WebVTT formatted subtitles to PORT."
105 (format port "WEBVTT~%~%")
106 (for-each (cut write-web-vtt <> port) subtitles))
108 (define (convert input-port output-port)
109 "Read the SubRip formatted subtitles from INPUT-PORT and write the
110 WebVTT equivalents to OUTPUT-PORT."
111 (write-web-vtts (read-sub-rips input-port) output-port))
113 (define (show-help-and-exit)
114 (format #t "Usage: srt2vtt [OPTIONS]
115 Convert SubRip formatted subtitles to WebVTT format.~%")
117 -h, --help display this help and exit")
119 -v, --version display version and exit")
121 -i, --input=FILE-NAME read input from FILE-NAME")
123 -o, --output=FILE-NAME write output to FILE-NAME")
127 (define (show-version-and-exit)
128 (format #t "srt2vtt 0.1~%")
131 (define (show-usage-and-exit)
132 (format #t "Try `srt2vtt --help' for more information.~%")
135 (define (show-wrong-args-and-exit)
136 (format #t "Invalid arguments~%")
137 (show-usage-and-exit))
139 (define %default-args
140 `((input . ,(current-input-port))
141 (output . ,(current-output-port))))
144 (list (option '(#\h "help") #f #f
145 (lambda (opt name arg args)
146 (show-help-and-exit)))
147 (option '(#\v "version") #f #f
148 (lambda (opt name arg args)
149 (show-version-and-exit)))
150 (option '(#\i "input") #t #f
151 (lambda (opt name arg args)
152 (alist-cons 'input arg args)))
153 (option '(#\o "output") #t #f
154 (lambda (opt name arg args)
155 (alist-cons 'output arg args)))))
157 (define (make-call-with-port-or-file file-proc)
158 "Create a new procedure that accepts two arguments: a port or file,
159 and a procedure. When the returned procedure is called with a port as
160 the first argument, the second argument is simply applied with that
161 port. If the first argument is a string, the second argument is
162 applied with the port corresponding to the file name contained in the
163 string as opened by FILE-PROC."
164 (lambda (port-or-file proc)
165 (if (port? port-or-file)
167 (file-proc port-or-file proc))))
169 (define call-with-port-or-input-file
170 (make-call-with-port-or-file call-with-input-file))
172 (define call-with-port-or-output-file
173 (make-call-with-port-or-file call-with-output-file))
175 (define (parse-opts args)
176 "Parse the list of ARGS and return a list of option/value pairs.
177 When an option isn't specified in ARGS, it defaults to the value in
181 (lambda (opt name arg args)
182 (error "Unrecognized option '~a'" name))
184 (error "Extraneous argument '~a'" arg))
188 "srt2vtt entry point."
189 (let ((opts (parse-opts args)))
190 (call-with-port-or-input-file (assoc-ref opts 'input)
192 (call-with-port-or-output-file (assoc-ref opts 'output)
193 (lambda (output-port)
194 (convert input-port output-port)))))))
196 (match (command-line)