Skip to content

automatically create subtitles with ImageMagick


bashscript

file generateSubs.sh:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/bash
c=0
chap=0
echo Chapter $chap
while read p; do
    if [ -n  "$p" ]; then
        # white sub:
        convert -size 1920x1080 -pointsize 48 -gravity South -background transparent -font Arial -fill black -stroke black label:"$p" -blur 0x2 +repage -fill white -stroke none label:"$p" -composite $(printf %02d $chap)\_$(printf %02d $c)\_white.png
        # black sub:
        convert -size 1920x1080 -pointsize 48 -gravity South -background transparent -font Arial -fill white -stroke white label:"$p" -blur 0x2 +repage -fill black -stroke none label:"$p" -composite $(printf %02d $chap)\_$(printf %02d $c)\_black.png
        c=$((c+1))
    else
        chap=$((chap+1))
        c=0
        echo Chapter $chap
    fi
done

run script:

every line will produce a new subtitle image, an empty line begins a new chapter

  • run it interactive:
    1
    ./generateSubs.sh
    
  • or pipe a given textfile:
    1
    cat subtitles.txt | ./generateSubs.sh