1 #!/usr/bin/guile --no-auto-compile
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 (let ((match (regexp-exec regexp s)))
49 (map (cut match:substring match <>) '(1 2 3 4))))))
51 (define parse-time-span
52 (let ((regexp (make-regexp "([0-9:,]+) --> ([0-9:,]+)")))
54 (let ((match (regexp-exec regexp s)))
55 (values (parse-time (match:substring match 1))
56 (parse-time (match:substring match 2)))))))
58 (define (read-sub-rip port)
59 (let-values (((id) (string->number (read-line port)))
60 ((start end) (parse-time-span (read-line port)))
61 ((lines) (let loop ((lines '()))
62 (let ((line (read-line port)))
63 (if (string-null? line)
65 (loop (cons line lines)))))))
66 (make-subtitle id start end lines)))
68 (define (read-sub-rips port)
70 (let loop ((subs '()))
71 (if (eof-object? (peek-char port))
73 (loop (cons (read-sub-rip port) subs))))))
75 (define (write-time time port)
78 (format port "~a:~a:~a.~a" h m s ms))))
80 (define (write-web-vtt subtitle port)
82 (($ <subtitle> id start end text)
83 (format port "~a~%" id)
84 (write-time start port)
85 (display " --> " port)
88 (format port "~a~%" (string-join text "\n"))
91 (define (write-web-vtts subtitles port)
92 (format port "WEBVTT~%~%")
93 (for-each (cut write-web-vtt <> port) subtitles))
95 (define (convert input-port output-port)
96 (write-web-vtts (read-sub-rips input-port) output-port))
98 (define (show-help-and-exit)
99 (format #t "Usage: srt2vtt [OPTIONS]
100 Convert SubRip formatted subtitles to WebVTT format.~%")
102 -h, --help display this help and exit")
104 -v, --version display version and exit")
106 -i, --input=FILE-NAME read input from FILE-NAME")
108 -o, --output=FILE-NAME write output to FILE-NAME")
112 (define (show-version-and-exit)
113 (format #t "srt2vtt 0.1~%")
116 (define (show-usage-and-exit)
117 (format #t "Try `srt2vtt --help' for more information.~%")
120 (define (show-wrong-args-and-exit)
121 (format #t "Invalid arguments~%")
122 (show-usage-and-exit))
124 (define %default-args
125 `((input . ,(current-input-port))
126 (output . ,(current-output-port))))
129 (list (option '(#\h "help") #f #f
130 (lambda (opt name arg args)
131 (show-help-and-exit)))
132 (option '(#\v "version") #f #f
133 (lambda (opt name arg args)
134 (show-version-and-exit)))
135 (option '(#\i "input") #t #f
136 (lambda (opt name arg args)
137 (alist-cons 'input arg args)))
138 (option '(#\o "output") #t #f
139 (lambda (opt name arg args)
140 (alist-cons 'output arg args)))))
142 (define (make-call-with-port-or-file file-proc)
143 (lambda (port-or-file proc)
144 (if (port? port-or-file)
146 (file-proc port-or-file proc))))
148 (define call-with-port-or-input-file
149 (make-call-with-port-or-file call-with-input-file))
151 (define call-with-port-or-output-file
152 (make-call-with-port-or-file call-with-output-file))
154 (define (parse-opts args)
157 (lambda (opt name arg args)
158 (error "Unrecognized option '~a'" name))
160 (error "Extraneous argument '~a'" arg))
164 (let ((opts (parse-opts args)))
165 (call-with-port-or-input-file (assoc-ref opts 'input)
167 (call-with-port-or-output-file (assoc-ref opts 'output)
168 (lambda (output-port)
169 (convert input-port output-port)))))))
171 (match (command-line)