#!/usr/bin/guile \ --debug -e main -s !# (use-modules (ice-9 regex) (ice-9 getopt-long)) ;; start-inline: (grumbel helper) (use-modules (ice-9 optargs)) (define (file:for-each-line filename thunk) (with-input-from-file filename (lambda () (let loop ((line (read-line))) (cond ((not (eof-object? line)) (thunk line) (loop (read-line)))))))) (define* (println #:key (port #f) . rest) (cond (port (for-each (lambda (el) (display el port)) rest)) (else (for-each display rest))) (newline)) ;; stop-inline: (grumbel helper) (define *subtitle-line-regex* (make-regexp "^\\{([0-9]+)\\}\\{([0-9]+)\\}(.*)$")) (define (scale-subtitle factor offset line) (let ((ret (regexp-exec *subtitle-line-regex* line))) (cond ((not ret) (error 'parse-error "Parse error with line: " line)) (else (let ((start (string->number (match:substring ret 1))) (end (string->number (match:substring ret 2))) (text (match:substring ret 3))) (println "{" (inexact->exact (+ (* factor start) offset)) "}" "{" (inexact->exact (+ (* factor end) offset)) "}" text)))))) (define (scale-subtitle-file filename scale offset) (file:for-each-line filename (lambda (line) (scale-subtitle scale offset line)))) (define (print-help) (display "\ subscale.scm [options] FILENAME -h, --help Display this help -s, --scale NUMBER Scale the numbers by NUMBER -o, --offset NUMBER Offset the frame references by NUMBER --output FILENAME Write result to filename, instead of stdout ")) (define (main args) (let* ((option-spec '((scale (single-char #\s) (value #t)) (offset (single-char #\o) (value #t)) (output (value #t)) (help (single-char #\h) (value #f)))) (options (getopt-long args option-spec)) (scale (string->number (option-ref options 'scale "1"))) (offset (string->number (option-ref options 'offset "0"))) (output-file (option-ref options 'output #f)) (help-wanted (option-ref options 'help #f)) (rest (option-ref options '() #f))) (cond ((or help-wanted (not (= (length rest) 1))) (print-help)) (else (cond (output-file (with-output-to-file output-file (lambda () (scale-subtitle-file (car rest) scale offset)))) (else (scale-subtitle-file (car rest) scale offset))))))) ;; Avalon: --offset 220 ;; EOF ;;