; ----------------------------------------------------------------------------- ; ; srt2subtitle 0.15 ; ================= ; Script to create subtitles from "SubRip" files (*.srt). ; ; Copyright (c) 2006 Martin Zuther (http://www.mzuther.de/) ; ; This program 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 2 of the License, or ; (at your option) any later version. ; ; This program 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 this program; if not, write to the Free Software ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ; ; ----------------------------------------------------------------------------- (script-fu-register "script-fu-srt2subtitle-batch" "/Xtns/Script-Fu/mzuther/srt2subtitle (batch mode)" "Creates subtitles for PAL and NTSC by reading a \"SubRip\" file (*.srt). This file has to use Unix newline characters." "Martin Zuther" "2006, Martin Zuther" "26.12.2006" "" SF-FILENAME _"SubRip file" "/home/mzuther/subtitles/subtitles.srt" SF-TEXT _"Output path" "/home/mzuther/subtitles/output" SF-TEXT _"Filename prefix" "subtitle_" SF-OPTION _"Format" '("PAL (720 x 576)" "NTSC (720 x 486)") SF-COLOR _"Text color" '(255 255 0) SF-COLOR _"Shadow color" '(0 0 0) SF-ADJUSTMENT _"Shadow size (pixels)" '(2 0 5 1 10 0 0) SF-ADJUSTMENT _"Safe area (percent)" '(10 0 25 1 10 0 0) SF-FONT _"Font" "Sans" SF-ADJUSTMENT _"Font size (pixels)" '(20 8 40 1 10 0 0) SF-TOGGLE _"Antialias" TRUE ) (define (script-fu-srt2subtitle-batch strInputFilename strOutputPath strOutputFilenamePrefix nFormat colText colShadow nShadowSize nSafeArea fontName nFontSize bAntiAlias) ; who really needs so many different line endings? (set! strNewlineCharacterUnix "\n") (set! strNewlineCharacterDOS "\r\n") (set! strNewlineCharacterMac "\r") ; open subrip file (*.srt) (set! file (fopen strInputFilename "r") ) (set! strSubtitles "") (while (set! strInput (fread 1 file) ) (set! strSubtitles (string-append strSubtitles strInput) ) ) (fclose file) ; make sure the last subtitle doesn't get too long (set! strSubtitles (string-append strSubtitles strNewlineCharacterUnix strNewlineCharacterUnix) ) ; double newlines signal next subtitle (set! listSubtitles (strbreakup strSubtitles (string-append strNewlineCharacterUnix strNewlineCharacterUnix) ) ) ; initialize loop variable (set! script-fu-srt2subtitle-batch-looper 0) ; loop through list with text lines (while (< script-fu-srt2subtitle-batch-looper (length listSubtitles) ) ; get indexed subtitle information (set! strSubtitleInformation (lref-default listSubtitles script-fu-srt2subtitle-batch-looper "") ) (set! pDisplay (car (cadr (script-fu-srt2subtitle-create-image strOutputPath strOutputFilenamePrefix nFormat colText colShadow nShadowSize nSafeArea fontName nFontSize bAntiAlias strSubtitleInformation) ) ) ) ; close image (if (> pDisplay -1) (gimp-display-delete pDisplay) ) ; update loop variable (set! script-fu-srt2subtitle-batch-looper (+ script-fu-srt2subtitle-batch-looper 1) ) ) ) (script-fu-register "script-fu-srt2subtitle-create-image" "/Xtns/Script-Fu/mzuther/srt2subtitle (single subtitle)" "Creates a single subtitle for PAL and NTSC from the section of a \"SubRip\" file (*.srt).\n\nMostly called from batch mode or used for debugging..." "Martin Zuther" "2006, Martin Zuther" "26.12.2006" "" SF-TEXT _"Output path" "/home/mzuther/subtitles" SF-TEXT _"Filename prefix" "subtitle_" SF-OPTION _"Format" '("PAL (720 x 576)" "NTSC (720 x 486)") SF-COLOR _"Text color" '(255 255 0) SF-COLOR _"Shadow color" '(0 0 0) SF-ADJUSTMENT _"Shadow size (pixels)" '(2 0 5 1 10 0 0) SF-ADJUSTMENT _"Safe area (percent)" '(10 0 25 1 10 0 0) SF-FONT _"Font" "Sans" SF-ADJUSTMENT _"Font size (pixels)" '(20 8 40 1 10 0 0) SF-TOGGLE _"Antialias" TRUE SF-TEXT _"Text" "1\n00:01:00,000 --> 00:05:00,000\nShort line\n\"A long line with quotes and stuff...\"\nAnother line!" ) (define (script-fu-srt2subtitle-create-image strOutputPath strOutputFilenamePrefix nFormat colText colShadow nShadowSize nSafeArea fontName nFontSize bAntiAlias strSubtitleInformation) ; who really needs so many different line endings? (set! strNewlineCharacterUnix "\n") (set! strNewlineCharacterDOS "\r\n") (set! strNewlineCharacterMac "\r") ; create custom palette (set! pPalette (car (gimp-palette-new "srt2subtitle-temp") ) ) (gimp-palette-add-entry pPalette "Text color" colText) ; mix between text and shadow color (set! colRedPart (/ (+ (car colText) (car colShadow) ) 2) ) (set! colGreenPart (/ (+ (cadr colText) (cadr colShadow) ) 2) ) (set! colBluePart (/ (+ (caddr colText) (caddr colShadow) ) 2) ) (gimp-palette-add-entry pPalette "AntiAlias color" (list colRedPart colGreenPart colBluePart) ) (gimp-palette-add-entry pPalette "Shadow color" colShadow) ; "decode" subtitle information (set! listSubtitleInformation (strbreakup strSubtitleInformation strNewlineCharacterUnix) ) ; check that at least two newlines were detected (if (> (length listSubtitleInformation) 2) (begin (set! strSubtitleNumber (car listSubtitleInformation) ) (set! strSubtitleStartTime (car (strbreakup (cadr listSubtitleInformation) " --> ") ) ) (set! listText (cddr listSubtitleInformation) ) ; initialize image format (PAL or NTSC) (cond ( (= nFormat 0) (set! nImageWidth 720) (set! nImageHeight 576) ) ( (= nFormat 1) (set! nImageWidth 720) (set! nImageHeight 486) ) ) ; create image and background layer (set! pImage (car (gimp-image-new nImageWidth nImageHeight RGB) ) ) (set! pLayerBackground (car (gimp-layer-new pImage nImageWidth nImageHeight RGB-IMAGE "Background" 100 NORMAL-MODE) ) ) ; add background with alpha channel to image (gimp-layer-add-alpha pLayerBackground) (gimp-image-add-layer pImage pLayerBackground -1) ; empty background layer (gimp-selection-all pImage) (gimp-edit-clear pLayerBackground) (gimp-selection-none pImage) ; initialize vertical start position (set! nVerticalPosition (- nImageHeight (* (length listText) nFontSize 1.25) ) ) (set! nVerticalPosition (- nVerticalPosition (* nImageHeight (/ nSafeArea 100) ) ) ) ; initialize loop variable (set! script-fu-srt2subtitle-create-image-looper 0) ; loop through list with text lines (while (< script-fu-srt2subtitle-create-image-looper (length listText) ) ; get indexed text line (set! strOutput (lref-default listText script-fu-srt2subtitle-create-image-looper "") ) ; output text line (if not empty) (if (not (= 0 (strcmp strOutput "") ) ) (begin (set! nVerticalPosition (+ nVerticalPosition (* nFontSize 1.25) ) ) (script-fu-srt2subtitle-create-layer pImage nVerticalPosition nShadowSize colText colShadow fontName nFontSize bAntiAlias strOutput) ) ) ; update loop variable (set! script-fu-srt2subtitle-create-image-looper (+ script-fu-srt2subtitle-create-image-looper 1) ) ) ; merge all layers with background (gimp-image-merge-visible-layers pImage CLIP-TO-IMAGE) ; convert to maximum number of allowed colors (minus one for alpha channel) (gimp-image-convert-indexed pImage FS-DITHER CUSTOM-PALETTE 3 0 1 "srt2subtitle-temp") ; display new image (set! pDisplay (gimp-display-new pImage) ) ; hand the filename over to GIMP ... (set! strSubtitleStartTime (string-append (substring strSubtitleStartTime 0 2) (substring strSubtitleStartTime 3 5) (substring strSubtitleStartTime 6 8) "_" (substring strSubtitleStartTime 9 12) ) ) (set! strOutputFilename (string-append strOutputPath "/" strOutputFilenamePrefix strSubtitleStartTime ".png") ) (gimp-image-set-filename pImage strOutputFilename) ; ... and save the image (set! pDrawable (car (gimp-image-active-drawable pImage) ) ) (file-png-save2 1 pImage pDrawable strOutputFilename strOutputFilename 1 9 0 0 0 1 1 0 0) ; clear dirty flag (gimp-image-clean-all pImage) ) (begin (set! pImage -1) (set! pDisplay (list -1) ) ) ) ; delete custom palette (gimp-palette-delete pPalette) ; return image (or invalid pointers) (list pImage pDisplay) ) (define (script-fu-srt2subtitle-create-layer pImage nVerticalPosition nShadowSize colText colShadow fontName nFontSize bAntiAlias strText) ; initialize colors (gimp-palette-set-foreground colText) (gimp-palette-set-background colShadow) ; initialize image and text sizes (set! nImageWidth (car (gimp-image-width pImage) ) ) (set! nImageHeight (car (gimp-image-height pImage) ) ) ; create and center text layer (set! pLayerText (car (gimp-text-fontname pImage -1 0 nVerticalPosition strText -1 bAntiAlias nFontSize PIXELS fontName) ) ) (set! nTextWidth (car (gimp-drawable-width pLayerText) ) ) (set! nHorizontalPosition (/ (- nImageWidth nTextWidth) 2) ) (gimp-layer-set-offsets pLayerText nHorizontalPosition nVerticalPosition) ; create shadow #1 (if (> nShadowSize 0) (begin ; initialize colors (gimp-palette-set-foreground colText) (gimp-palette-set-background colShadow) ; create new layer for shadow (set! pLayerShadow (car (gimp-layer-new pImage nImageWidth nImageHeight RGB-IMAGE "Shadow" 100 NORMAL-MODE) ) ) (gimp-image-add-layer pImage pLayerShadow -1) (gimp-image-lower-layer pImage pLayerShadow) ; empty shadow layer (gimp-selection-all pImage) (gimp-edit-clear pLayerShadow) (gimp-selection-none pImage) ; add alpha channel to shadow layer (gimp-layer-add-alpha pLayerShadow) ; create shadow (gimp-selection-layer-alpha pLayerText) (gimp-selection-grow pImage nShadowSize) (gimp-selection-invert pImage) (gimp-edit-clear pLayerShadow) (gimp-selection-none pImage) ; merge both layers (set! pLayerText (car (gimp-image-merge-down pImage pLayerText CLIP-TO-IMAGE) ) ) (gimp-drawable-set-name pLayerText strText) ) ) ; return both image and layer (list pImage pLayerText) )