15c812f8ae52dd578636fb632c07d2359755bb57
[srt2vtt.git] / srt2vtt
1 #!/usr/bin/guile --no-auto-compile
2 -*- scheme -*-
3 !#
4
5 ;;; srt2vtt --- SRT to WebVTT converter
6 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
7 ;;;
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.
12 ;;;
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.
17 ;;;
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/>.
20
21 ;;; Commentary:
22 ;;
23 ;; Convert SRT formatted subtitles to WebVTT format.
24 ;;
25 ;;; Code:
26
27 (use-modules (ice-9 format)
28              (ice-9 match)
29              (ice-9 rdelim)
30              (ice-9 regex)
31              (srfi srfi-1)
32              (srfi srfi-9)
33              (srfi srfi-11)
34              (srfi srfi-26)
35              (srfi srfi-37))
36
37 (define-record-type <subtitle>
38   (make-subtitle id start end text)
39   subtitle?
40   (id subtitle-id)
41   (start subtitle-start)
42   (end subtitle-end)
43   (text subtitle-text))
44
45 (define parse-time
46   (let ((regexp (make-regexp "([0-9]+):([0-9]+):([0-9]+),([0-9]+)")))
47     (lambda (s)
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))))))
52
53 (define parse-time-span
54   (let ((regexp (make-regexp "([0-9:,]+) --> ([0-9:,]+)")))
55     (lambda (s)
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)))))))
62
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 (and (string-null? line)
70                                      ;; A subtitle may be a blank line!
71                                      (not (null? lines)))
72                                 lines
73                                 (loop (cons line lines)))))))
74     (make-subtitle id start end lines)))
75
76 (define (read-sub-rips port)
77   "Read all SubRip formatted subtitles from PORT."
78   (reverse
79    (let loop ((subs '()))
80      (if (eof-object? (peek-char port))
81          subs
82          (loop (cons (read-sub-rip port) subs))))))
83
84 (define (write-time time port)
85   "Write TIME as a WebVTT formatted timestamp to PORT."
86   (match time
87    ((h m s ms)
88     (format port "~a:~a:~a.~a" h m s ms))))
89
90 (define (write-web-vtt subtitle port)
91   "Write SUBTITLE as a WebVTT formatted subtitle to PORT."
92   (match subtitle
93     (($ <subtitle> id start end text)
94      (format port "~a~%" id)
95      (write-time start port)
96      (display " --> " port)
97      (write-time end port)
98      (newline port)
99      (format port "~a~%" (string-join text "\n"))
100      (newline port))))
101
102 (define (write-web-vtts subtitles port)
103   "Write all SUBTITLES as WebVTT formatted subtitles to PORT."
104   (format port "WEBVTT~%~%")
105   (for-each (cut write-web-vtt <> port) subtitles))
106
107 (define (convert input-port output-port)
108   "Read the SubRip formatted subtitles from INPUT-PORT and write the
109 WebVTT equivalents to OUTPUT-PORT."
110   (write-web-vtts (read-sub-rips input-port) output-port))
111
112 (define (show-help-and-exit)
113   (format #t "Usage: srt2vtt [OPTIONS]
114 Convert SubRip formatted subtitles to WebVTT format.~%")
115   (display "
116   -h, --help             display this help and exit")
117   (display "
118   -v, --version          display version and exit")
119   (display "
120   -i, --input=FILE-NAME  read input from FILE-NAME")
121   (display "
122   -o, --output=FILE-NAME write output to FILE-NAME")
123   (newline)
124   (exit 0))
125
126 (define (show-version-and-exit)
127   (format #t "srt2vtt 0.1~%")
128   (exit 0))
129
130 (define (show-usage-and-exit)
131   (format #t "Try `srt2vtt --help' for more information.~%")
132   (exit 1))
133
134 (define (show-wrong-args-and-exit)
135   (format #t "Invalid arguments~%")
136   (show-usage-and-exit))
137
138 (define %default-args
139   `((input . ,(current-input-port))
140     (output . ,(current-output-port))))
141
142 (define %options
143   (list (option '(#\h "help") #f #f
144                 (lambda (opt name arg args)
145                   (show-help-and-exit)))
146         (option '(#\v "version") #f #f
147                 (lambda (opt name arg args)
148                   (show-version-and-exit)))
149         (option '(#\i "input") #t #f
150                 (lambda (opt name arg args)
151                   (alist-cons 'input arg args)))
152         (option '(#\o "output") #t #f
153                 (lambda (opt name arg args)
154                   (alist-cons 'output arg args)))))
155
156 (define (make-call-with-port-or-file file-proc)
157   "Create a new procedure that accepts two arguments: a port or file,
158 and a procedure.  When the returned procedure is called with a port as
159 the first argument, the second argument is simply applied with that
160 port.  If the first argument is a string, the second argument is
161 applied with the port corresponding to the file name contained in the
162 string as opened by FILE-PROC."
163   (lambda (port-or-file proc)
164     (if (port? port-or-file)
165         (proc port-or-file)
166         (file-proc port-or-file proc))))
167
168 (define call-with-port-or-input-file
169   (make-call-with-port-or-file call-with-input-file))
170
171 (define call-with-port-or-output-file
172   (make-call-with-port-or-file call-with-output-file))
173
174 (define (parse-opts args)
175   "Parse the list of ARGS and return a list of option/value pairs.
176 When an option isn't specified in ARGS, it defaults to the value in
177 '%default-args'."
178   (args-fold args
179              %options
180              (lambda (opt name arg args)
181                (error "Unrecognized option '~a'" name))
182              (lambda (arg args)
183                (error "Extraneous argument '~a'" arg))
184              %default-args))
185
186 (define (main args)
187   "srt2vtt entry point."
188   (let ((opts (parse-opts args)))
189     (call-with-port-or-input-file (assoc-ref opts 'input)
190       (lambda (input-port)
191         (call-with-port-or-output-file (assoc-ref opts 'output)
192           (lambda (output-port)
193             (convert input-port output-port)))))))
194
195 (match (command-line)
196   ((arg0 args ...)
197    (main args)))