X-Git-Url: https://jxself.org/git/?p=srt2vtt.git;a=blobdiff_plain;f=srt2vtt%2Fsubrip.scm;fp=srt2vtt%2Fsubrip.scm;h=167aff804a4b1d8c8b24f3902ce425c1e4f2f350;hp=0000000000000000000000000000000000000000;hb=b965a6d6326e67df0b0909065119c59563da8db2;hpb=3d5619c63693a196652e3b9e545995ec4919152c diff --git a/srt2vtt/subrip.scm b/srt2vtt/subrip.scm new file mode 100644 index 0000000..167aff8 --- /dev/null +++ b/srt2vtt/subrip.scm @@ -0,0 +1,66 @@ +;;; srt2vtt --- SRT to WebVTT converter +;;; Copyright © 2015 David Thompson +;;; +;;; 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 . + +(define-module (srt2vtt subrip) + #:use-module (ice-9 match) + #:use-module (ice-9 rdelim) + #:use-module (ice-9 regex) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-11) + #:use-module (srfi srfi-26) + #:use-module (srt2vtt) + #:export (read-subrip + read-subrips)) + +(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-subrip 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-subrips port) + "Read all SubRip formatted subtitles from PORT." + (reverse + (let loop ((subs '())) + (if (eof-object? (peek-char port)) + subs + (loop (cons (read-subrip port) subs))))))