summaryrefslogtreecommitdiff
path: root/srt2vtt
blob: a4cb576042aabd05915205eb495a5116608c1dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/guile
-*- scheme -*-
!#

;;; srt2vtt --- SRT to WebVTT converter
;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;;
;;; srt2vtt is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; srt2vtt is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with srt2vtt.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Convert SRT formatted subtitles to WebVTT format.
;;
;;; Code:

(use-modules (ice-9 format)
             (ice-9 match)
             (ice-9 rdelim)
             (ice-9 regex)
             (srfi srfi-1)
             (srfi srfi-9)
             (srfi srfi-11)
             (srfi srfi-26)
             (srfi srfi-37))

(define-record-type <subtitle>
  (make-subtitle id start end text)
  subtitle?
  (id subtitle-id)
  (start subtitle-start)
  (end subtitle-end)
  (text subtitle-text))

(define parse-time
  (let ((regexp (make-regexp "([0-9]+):([0-9]+):([0-9]+),([0-9]+)")))
    (lambda (s)
      "Parse the SubRip formatted timestamp in the string S into a 4
element list.  Valid input looks like '00:00:03.417'."
      (let ((match (regexp-exec regexp s)))
        (map (cut match:substring match <>) '(1 2 3 4))))))

(define parse-time-span
  (let ((regexp (make-regexp "([0-9:,]+) --> ([0-9:,]+)")))
    (lambda (s)
      "Parse the SubRip formatted time span in the string S and return
two values: the start time and the end time.  Valid input looks like
'00:00:03.417 --> 00:00:04.936'."
      (let ((match (regexp-exec regexp s)))
        (values (parse-time (match:substring match 1))
                (parse-time (match:substring match 2)))))))

(define (read-sub-rip port)
  "Read a SubRip formatted subtitle from PORT."
  (let-values (((id) (string->number (read-line port)))
               ((start end) (parse-time-span (read-line port)))
               ((lines) (let loop ((lines '()))
                          (let ((line (read-line port)))
                            (if (or (eof-object? line)
                                    (and (string-null? line)
                                         ;; A subtitle may be a blank line!
                                         (not (null? lines))))
                                lines
                                (loop (cons line lines)))))))
    (make-subtitle id start end lines)))

(define (read-sub-rips port)
  "Read all SubRip formatted subtitles from PORT."
  (reverse
   (let loop ((subs '()))
     (if (eof-object? (peek-char port))
         subs
         (loop (cons (read-sub-rip port) subs))))))

(define (write-time time port)
  "Write TIME as a WebVTT formatted timestamp to PORT."
  (match time
   ((h m s ms)
    (format port "~a:~a:~a.~a" h m s ms))))

(define (write-web-vtt subtitle port)
  "Write SUBTITLE as a WebVTT formatted subtitle to PORT."
  (match subtitle
    (($ <subtitle> id start end text)
     (format port "~a~%" id)
     (write-time start port)
     (display " --> " port)
     (write-time end port)
     (newline port)
     (format port "~a~%" (string-join text "\n"))
     (newline port))))

(define (write-web-vtts subtitles port)
  "Write all SUBTITLES as WebVTT formatted subtitles to PORT."
  (format port "WEBVTT~%~%")
  (for-each (cut write-web-vtt <> port) subtitles))

(define (convert input-port output-port)
  "Read the SubRip formatted subtitles from INPUT-PORT and write the
WebVTT equivalents to OUTPUT-PORT."
  (write-web-vtts (read-sub-rips input-port) output-port))

(define (show-help-and-exit)
  (format #t "Usage: srt2vtt [OPTIONS]
Convert SubRip formatted subtitles to WebVTT format.~%")
  (display "
  -h, --help             display this help and exit")
  (display "
  -v, --version          display version and exit")
  (display "
  -i, --input=FILE-NAME  read input from FILE-NAME")
  (display "
  -o, --output=FILE-NAME write output to FILE-NAME")
  (newline)
  (exit 0))

(define (show-version-and-exit)
  (format #t "srt2vtt 0.1~%")
  (exit 0))

(define (show-usage-and-exit)
  (format #t "Try `srt2vtt --help' for more information.~%")
  (exit 1))

(define (show-wrong-args-and-exit)
  (format #t "Invalid arguments~%")
  (show-usage-and-exit))

(define %default-args
  `((input . ,(current-input-port))
    (output . ,(current-output-port))))

(define %options
  (list (option '(#\h "help") #f #f
                (lambda (opt name arg args)
                  (show-help-and-exit)))
        (option '(#\v "version") #f #f
                (lambda (opt name arg args)
                  (show-version-and-exit)))
        (option '(#\i "input") #t #f
                (lambda (opt name arg args)
                  (alist-cons 'input arg args)))
        (option '(#\o "output") #t #f
                (lambda (opt name arg args)
                  (alist-cons 'output arg args)))))

(define (make-call-with-port-or-file file-proc)
  "Create a new procedure that accepts two arguments: a port or file,
and a procedure.  When the returned procedure is called with a port as
the first argument, the second argument is simply applied with that
port.  If the first argument is a string, the second argument is
applied with the port corresponding to the file name contained in the
string as opened by FILE-PROC."
  (lambda (port-or-file proc)
    (if (port? port-or-file)
        (proc port-or-file)
        (file-proc port-or-file proc))))

(define call-with-port-or-input-file
  (make-call-with-port-or-file call-with-input-file))

(define call-with-port-or-output-file
  (make-call-with-port-or-file call-with-output-file))

(define (parse-opts args)
  "Parse the list of ARGS and return a list of option/value pairs.
When an option isn't specified in ARGS, it defaults to the value in
'%default-args'."
  (args-fold args
             %options
             (lambda (opt name arg args)
               (error "Unrecognized option '~a'" name))
             (lambda (arg args)
               (error "Extraneous argument '~a'" arg))
             %default-args))

(define (main args)
  "srt2vtt entry point."
  (let ((opts (parse-opts args)))
    (call-with-port-or-input-file (assoc-ref opts 'input)
      (lambda (input-port)
        (call-with-port-or-output-file (assoc-ref opts 'output)
          (lambda (output-port)
            (convert input-port output-port)))))))

(match (command-line)
  ((arg0 args ...)
   (main args)))