Add blank line after WebVTT signature.
[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       (let ((match (regexp-exec regexp s)))
49         (map (cut match:substring match <>) '(1 2 3 4))))))
50
51 (define parse-time-span
52   (let ((regexp (make-regexp "([0-9:,]+) --> ([0-9:,]+)")))
53     (lambda (s)
54       (let ((match (regexp-exec regexp s)))
55         (values (parse-time (match:substring match 1))
56                 (parse-time (match:substring match 2)))))))
57
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)
64                                 lines
65                                 (loop (cons line lines)))))))
66     (make-subtitle id start end lines)))
67
68 (define (read-sub-rips port)
69   (reverse
70    (let loop ((subs '()))
71      (if (eof-object? (peek-char port))
72          subs
73          (loop (cons (read-sub-rip port) subs))))))
74
75 (define (write-time time port)
76   (match time
77    ((h m s ms)
78     (format port "~a:~a:~a.~a" h m s ms))))
79
80 (define (write-web-vtt subtitle port)
81   (match subtitle
82     (($ <subtitle> id start end text)
83      (format port "~a~%" id)
84      (write-time start port)
85      (display " --> " port)
86      (write-time end port)
87      (newline port)
88      (format port "~a~%" (string-join text "\n"))
89      (newline port))))
90
91 (define (write-web-vtts subtitles port)
92   (format port "WEBVTT~%~%")
93   (for-each (cut write-web-vtt <> port) subtitles))
94
95 (define (convert input-port output-port)
96   (write-web-vtts (read-sub-rips input-port) output-port))
97
98 (define (show-help-and-exit)
99   (format #t "Usage: srt2vtt [OPTIONS]
100 Convert SubRip formatted subtitles to WebVTT format.~%")
101   (display "
102   -h, --help             display this help and exit")
103   (display "
104   -v, --version          display version and exit")
105   (display "
106   -i, --input=FILE-NAME  read input from FILE-NAME")
107   (display "
108   -o, --output=FILE-NAME write output to FILE-NAME")
109   (newline)
110   (exit 0))
111
112 (define (show-version-and-exit)
113   (format #t "srt2vtt 0.1~%")
114   (exit 0))
115
116 (define (show-usage-and-exit)
117   (format #t "Try `srt2vtt --help' for more information.~%")
118   (exit 1))
119
120 (define (show-wrong-args-and-exit)
121   (format #t "Invalid arguments~%")
122   (show-usage-and-exit))
123
124 (define %default-args
125   `((input . ,(current-input-port))
126     (output . ,(current-output-port))))
127
128 (define %options
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)))))
141
142 (define (make-call-with-port-or-file file-proc)
143   (lambda (port-or-file proc)
144     (if (port? port-or-file)
145         (proc port-or-file)
146         (file-proc port-or-file proc))))
147
148 (define call-with-port-or-input-file
149   (make-call-with-port-or-file call-with-input-file))
150
151 (define call-with-port-or-output-file
152   (make-call-with-port-or-file call-with-output-file))
153
154 (define (parse-opts args)
155   (args-fold args
156              %options
157              (lambda (opt name arg args)
158                (error "Unrecognized option '~a'" name))
159              (lambda (arg args)
160                (error "Extraneous argument '~a'" arg))
161              %default-args))
162
163 (define (main args)
164   (let ((opts (parse-opts args)))
165     (call-with-port-or-input-file (assoc-ref opts 'input)
166       (lambda (input-port)
167         (call-with-port-or-output-file (assoc-ref opts 'output)
168           (lambda (output-port)
169             (convert input-port output-port)))))))
170
171 (match (command-line)
172   ((arg0 args ...)
173    (main args)))