~ubuntu-branches/ubuntu/karmic/tovid/karmic

« back to all changes in this revision

Viewing changes to src/makemenu

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-01-24 22:04:40 UTC
  • Revision ID: james.westby@ubuntu.com-20080124220440-x7cheljduf1rdgnq
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
ME="[makemenu]:"
 
3
. tovid-init
 
4
 
 
5
# version $Id: makemenu 2220 2007-07-27 02:21:46Z wapcaplet88 $
 
6
# makemenu
 
7
# Part of the tovid suite
 
8
# =======================
 
9
# A bash script for generating menus for use with DVD, VCD, or SVCD.
 
10
# Given a background image and a list of titles, and an optional audio
 
11
# file to use as background music, this script produces an MPEG video
 
12
# displaying the specified text, with subtitle-multiplexed menu
 
13
# highlights (for DVD) or enumerated titles (for (S)VCD).
 
14
#
 
15
# Project homepage: http://www.tovid.org
 
16
#
 
17
#
 
18
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
 
19
 
20
# This program is free software; you can redistribute it and/or 
 
21
# modify it under the terms of the GNU General Public License 
 
22
# as published by the Free Software Foundation; either 
 
23
# version 2 of the License, or (at your option) any later 
 
24
# version.
 
25
 
26
# This program is distributed in the hope that it will be useful,
 
27
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
28
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
29
# GNU General Public License for more details.
 
30
 
31
# You should have received a copy of the GNU General Public License
 
32
# along with this program; if not, write to the Free Software
 
33
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
 
34
#
 
35
#           http://www.gnu.org/licenses/gpl.txt
 
36
 
 
37
# Options to consider
 
38
# font selection
 
39
# font color, highlight color, selection color
 
40
# font shadow, border
 
41
# icons, graphic overlays
 
42
# Support for full-motion video menus
 
43
 
 
44
SCRIPT_NAME=`cat << EOF
 
45
--------------------------------
 
46
makemenu
 
47
A script to generate DVD/(S)VCD menus
 
48
Part of the tovid suite, version $TOVID_VERSION
 
49
$TOVID_HOME_PAGE
 
50
--------------------------------
 
51
EOF`
 
52
 
 
53
USAGE=`cat << EOF
 
54
Usage: makemenu [OPTIONS] "Title1" "Title2" ... -out {output prefix}
 
55
 
 
56
Common options:
 
57
 
 
58
    -pal | -ntsc | -ntscfilm            Set TV standard
 
59
    -dvd | -vcd | -svcd                 Encode to standard format
 
60
    -background IMAGE                   ex: -background star_field.png
 
61
    -audio AUDIOFILE                    ex: -audio foo_fighters.mp3
 
62
    -font "FONTNAME"                    ex: -font Courier
 
63
 
 
64
Example: 
 
65
 
 
66
    makemenu -background ocean.jpg "First" "Second" "Third" -out mymenu
 
67
        Create 'mymenu.mpg' with three titles and a custom background image.
 
68
 
 
69
See the makemenu manual page ('man makemenu') for additional documentation.
 
70
 
 
71
EOF`
 
72
 
 
73
# Defaults and function definitions
 
74
 
 
75
# Print script name, usage notes, and optional error message, then exit.
 
76
# Args: $@ == text string containing error message
 
77
usage_error ()
 
78
{
 
79
    printf "%s\n" "$USAGE"
 
80
    printf "%s\n" "$SEPARATOR"
 
81
    printf "*** %s\n" "$@"
 
82
    exit 1
 
83
}
 
84
 
 
85
# Clean up the temporary files
 
86
function cleanup()
 
87
{
 
88
    if $DEBUG; then
 
89
        echo "Leaving temporary files in $TMP_DIR"
 
90
        echo "Debugging log saved to $LOG_FILE"
 
91
    else
 
92
        echo "Cleaning up...   "
 
93
        rm -rf "$TMP_DIR"
 
94
    fi
 
95
}
 
96
 
 
97
 
 
98
# Initializations
 
99
 
 
100
# No background image or audio
 
101
BG_IMAGE=""
 
102
BG_AUDIO=""
 
103
MENU_LENGTH=""
 
104
# NTSC DVD
 
105
TVSYS="ntsc"
 
106
RES="dvd"
 
107
# Use a safe area
 
108
SAFE_AREA=:
 
109
# Number of titles
 
110
NUM_TITLES=0
 
111
MAX_VCD_TITLES=9
 
112
MAX_DVD_TITLES=36
 
113
OVER_MAX_TITLES=false
 
114
# Scale up and crop non-4:3 images by default
 
115
SCALECROP="crop"
 
116
# Colors for menu items (RGB)
 
117
TEXT_COLOR="white"
 
118
HIGHLIGHT_COLOR="yellow"
 
119
SELECT_COLOR="red"
 
120
# Default title font and point size
 
121
TITLE_FONT="Helvetica"
 
122
TITLE_SIZE="24"
 
123
FONT_DECO="-stroke black -strokewidth 1"
 
124
TEXT_ALIGN="northwest"
 
125
# Default menu title settings
 
126
MAKE_TITLE=false
 
127
# Button char
 
128
BUTTON_FONT=""
 
129
BUTTON_CHAR=">"
 
130
BTN_STROKE="none"
 
131
# NTSC DVD
 
132
SAMPRATE="48000"
 
133
ASUF="ac3"
 
134
VSUF="m2v"
 
135
FPS="2997"
 
136
MPEG2ENC_FMT="-f 8"
 
137
MPLEX_OPTS="-V"
 
138
MPEG2ENC_SYS="-F 4 -n n"
 
139
# Figure out what version of ppmtoy4m is in use;
 
140
# mjpegtools 1.6.2 seems to require 420_mpeg2
 
141
if mplex 2>&1 | grep -q "version 1.6.2"; then
 
142
    CHROMA_MODE="420_mpeg2"
 
143
else
 
144
    CHROMA_MODE="420mpeg2"
 
145
fi
 
146
PPM_OPTS="-S $CHROMA_MODE -A 10:11 -F 30000:1001"
 
147
# Direct stuff to /dev/null
 
148
REDIR="/dev/null"
 
149
# Not doing debugging
 
150
DEBUG=false
 
151
QUIET=false
 
152
# Don't overwrite output files
 
153
OVERWRITE=false
 
154
OUT_PREFIX=""
 
155
LOG_FILE="makemenu.log"
 
156
NOASK=false
 
157
# How far to space titles and buttons
 
158
TITLE_BOTTOM_SPACE=12
 
159
TITLE_LEFT_SPACE=10
 
160
 
 
161
 
 
162
# =========================================================================\
 
163
# E X E C U T I O N   B E G I N S   H E R E
 
164
 
165
#
 
166
 
 
167
echo "$SCRIPT_NAME"
 
168
 
 
169
assert_dep "$magick" "You are missing dependencies required for making menus!"
 
170
 
 
171
# Process all options
 
172
while test $# -gt 0; do
 
173
    case "$1" in
 
174
        "-ntsc" | "-ntscfilm" )
 
175
            TVSYS="ntsc"
 
176
            MPEG2ENC_SYS="-F 4 -n n"
 
177
            PPM_OPTS="-S $CHROMA_MODE -A 10:11 -F 30000:1001"
 
178
            FPS="2997"
 
179
            ;;
 
180
        "-pal" )
 
181
            TVSYS="pal"
 
182
            MPEG2ENC_SYS="-F 3 -n p"
 
183
            PPM_OPTS="-S $CHROMA_MODE -A 59:54 -F 25:1"
 
184
            FPS="2500"
 
185
            ;;
 
186
        "-dvd" )
 
187
            RES="dvd"
 
188
            MPEG2ENC_FMT="-f 8"
 
189
            # Variable bitrate
 
190
            MPLEX_OPTS="-V"
 
191
            VSUF="m2v"
 
192
            ;;
 
193
        "-vcd" )
 
194
            RES="vcd"
 
195
            MPEG2ENC_FMT="-f 1"
 
196
            # Constant bitrate
 
197
            MPLEX_OPTS=""
 
198
            VSUF="m1v"
 
199
            ;;
 
200
        "-svcd" )
 
201
            RES="vcd"
 
202
            MPEG2ENC_FMT="-f 4"
 
203
            # Variable bitrate
 
204
            MPLEX_OPTS="-V"
 
205
            VSUF="m2v"
 
206
            ;;
 
207
        "-audio" )
 
208
            shift
 
209
            # Make sure file exists
 
210
            if test -f "$1"; then
 
211
                BG_AUDIO=$1
 
212
            else
 
213
                usage_error "Can't find background audio file: $1"
 
214
            fi
 
215
            ;;
 
216
        "-background" )
 
217
            shift
 
218
            # Make sure file exists
 
219
            if test -f "$1"; then
 
220
                BG_IMAGE=$1
 
221
            else
 
222
                usage_error "Can't find background image file: $1"
 
223
            fi
 
224
            ;;
 
225
        "-overwrite" ) OVERWRITE=: ;;
 
226
        "-crop" ) SCALECROP="crop" ;;
 
227
        "-scale" ) SCALECROP="scale" ;;
 
228
        "-nosafearea" ) SAFE_AREA=false ;;
 
229
        "-font" )
 
230
            shift
 
231
            TITLE_FONT="$1"
 
232
            ;;
 
233
        "-fontsize" )
 
234
            shift
 
235
            TITLE_SIZE="$1"
 
236
            ;;
 
237
        "-fontdeco" )
 
238
            shift
 
239
            FONT_DECO="$1"
 
240
            ;;
 
241
        "-textcolor" )
 
242
            shift
 
243
            TEXT_COLOR="$1"
 
244
            ;;
 
245
        "-highlightcolor" )
 
246
            shift
 
247
            HIGHLIGHT_COLOR="$1"
 
248
            ;;
 
249
        "-selectcolor" )
 
250
            shift
 
251
            SELECT_COLOR="$1"
 
252
            ;;
 
253
        "-align" )
 
254
            shift
 
255
            case "$1" in
 
256
                "left" ) TEXT_ALIGN="northwest" ;;
 
257
                "right" ) TEXT_ALIGN="northeast" ;;
 
258
                "center" ) TEXT_ALIGN="north" ;;
 
259
                "middle" ) TEXT_ALIGN="center" ;;
 
260
                * ) TEXT_ALIGN=$(echo "$1" | tr A-Z a-z)
 
261
            esac
 
262
            ;;
 
263
        "-button" )
 
264
            shift
 
265
            case "$1" in
 
266
                "play" ) 
 
267
                    BUTTON_FONT="Webdings"
 
268
                    BUTTON_CHAR=4
 
269
                    ;;
 
270
                "movie" )
 
271
                    BUTTON_FONT="Webdings"
 
272
                    OFFICIAL_PRINTF=$(which printf)
 
273
                    BUTTON_CHAR=$($OFFICIAL_PRINTF '\u00b8')
 
274
                    ;;
 
275
                "utf8" )
 
276
                    shift
 
277
                    OFFICIAL_PRINTF=$(which printf)
 
278
                    BUTTON_CHAR=$($OFFICIAL_PRINTF "\u$1")
 
279
                    ;;
 
280
                * ) 
 
281
                    BUTTON_CHAR="$1"
 
282
                    if test $(echo -n "$BUTTON_CHAR" | wc -c ) -ne 1; then
 
283
                        usage_error "Buttons must have only _one_ characater."
 
284
                    fi
 
285
                    ;;
 
286
            esac
 
287
            ;;
 
288
        "-debug" )
 
289
            DEBUG=:
 
290
            REDIR="$LOG_FILE"
 
291
            ;;
 
292
        "-quiet" )
 
293
            QUIET=:
 
294
            ;;
 
295
        "-out" )
 
296
            shift
 
297
            OUT_PREFIX="$1"
 
298
            ;;
 
299
        "-noask" ) NOASK=: ;;
 
300
        "-button-outline" )
 
301
            shift
 
302
            BTN_STROKE="$1"
 
303
            ;;
 
304
        "-button-font" )
 
305
            shift
 
306
            BUTTON_FONT="$1"
 
307
            ;;
 
308
        "-menu-title" )
 
309
            shift
 
310
            MENU_TITLE="$1"
 
311
            MAKE_TITLE=:
 
312
            ;;
 
313
        "-menu-title-fontsize" )
 
314
            shift
 
315
            MENU_TITLE_SIZE="$1"
 
316
            ;;
 
317
        "-length" )
 
318
            shift
 
319
            MENU_LENGTH="-t $1"
 
320
            ;;
 
321
        # Assume anything in quotes is a video title
 
322
        ""*"" )
 
323
            TITLES=("${TITLES[@]}" "$1")
 
324
            ;;
 
325
        * )
 
326
            test -n "$1" && usage_error "Unrecognized command-line option: $1"
 
327
            ;;
 
328
    esac
 
329
 
 
330
    # Get the next argument
 
331
    shift
 
332
done
 
333
 
 
334
NUM_TITLES=${#TITLES[@]}
 
335
# If no titles were provided, exit with error
 
336
if test $NUM_TITLES -eq 0; then
 
337
    usage_error "Please provide at least one title string, enclosed in double-quotes."
 
338
fi
 
339
 
 
340
# If output prefix was not provided, exit with error
 
341
if test -z "$OUT_PREFIX"; then
 
342
    usage_error "Please provide an output prefix with the -out option."
 
343
fi
 
344
 
 
345
# Make sure we're not overwriting an existing menu
 
346
if test -f "$OUT_PREFIX.mpg"; then
 
347
    if $OVERWRITE; then
 
348
        rm -f "$OUT_PREFIX.mpg"
 
349
    else
 
350
        echo "There is already a file named \"$OUT_PREFIX.mpg\" in this directory!"
 
351
        echo "Use '-overwrite' to replace this file. (makemenu -overwrite ...)"
 
352
        echo "Exiting..."
 
353
        exit 1
 
354
    fi
 
355
fi
 
356
 
 
357
 
 
358
OUT_FILENAME="$OUT_PREFIX.mpg"
 
359
# Temporary directory named after the given -out prefix
 
360
OUTNAME=$(basename "$OUT_PREFIX")
 
361
TMP_DIR=$(tempdir "$WORKING_DIR/$OUTNAME")
 
362
$DEBUG && echo "Storing temporary files in $TMP_DIR" 
 
363
BG_CANVAS="$TMP_DIR/bg_canvas.png"
 
364
MENU_PPM="$TMP_DIR/menu.ppm"
 
365
MENU_MPEG="$TMP_DIR/menu.mpg"
 
366
SPUMUX_XML="$TMP_DIR/menu_buttons.xml"
 
367
 
 
368
SELECT_PNG="$TMP_DIR/select.png"
 
369
HIGHLIGHT_PNG="$TMP_DIR/highlight.png"
 
370
TILE_CANVAS="$TMP_DIR/00_tile_canvas.png"
 
371
BUTTON_PAD="$TMP_DIR/00_button_pad.png"
 
372
BUTTON_PLACEHOLDER="$TMP_DIR/00_button-placeholder.png"
 
373
HIGHLIGHT_BUTTON="$TMP_DIR/00_highlight-button.png"
 
374
SELECT_BUTTON="$TMP_DIR/00_select-button.png"
 
375
HIGHLIGHT_TEXT="$TMP_DIR/02_highlight-text.png"
 
376
MENU_TEXT="$TMP_DIR/02_menu-text.png"
 
377
SAFETY_CANVAS="$TMP_DIR/02_safety-canvas.png"
 
378
SELECT_TEXT="$TMP_DIR/02_select-text.png"
 
379
ACTIVE_TEXT="$TMP_DIR/03_active-text.png"
 
380
ACTIVE_HIGHLIGHT="$TMP_DIR/03_active-highlight.png"
 
381
ACTIVE_SELECT="$TMP_DIR/03_active-select.png"
 
382
MENU_TITLE_TEXT="$TMP_DIR/02_menu-title.png"
 
383
MENU_TITLE_PLACEHOLDER="$TMP_DIR/02_menu-title-placeholder.png"
 
384
 
 
385
# Use appropriate settings for DVD
 
386
if test $RES = "dvd"; then
 
387
    assert_dep "$dvd" "You are missing dependencies required for making DVD menus!"
 
388
    WIDTH="720"
 
389
    SAMPRATE="48000"
 
390
    test $TVSYS = "ntsc" && HEIGHT="480"
 
391
    test $TVSYS = "pal" && HEIGHT="576"
 
392
# And for (S)VCD
 
393
else
 
394
    WIDTH="352"
 
395
    SAMPRATE="44100"
 
396
    test $TVSYS = "ntsc" && HEIGHT="240"
 
397
    test $TVSYS = "pal" && HEIGHT="288"
 
398
    # Reduce font size and spacing
 
399
    TITLE_SIZE=$(expr $TITLE_SIZE \/ 2)  
 
400
fi
 
401
 
 
402
# Set audio stream format
 
403
if test "$RES" = "dvd"; then
 
404
    ASUF="ac3"
 
405
else
 
406
    ASUF="mp2"
 
407
fi
 
408
VIDEO_STREAM="$TMP_DIR/video.$VSUF"
 
409
AUDIO_STREAM="$TMP_DIR/audio.$ASUF"
 
410
 
 
411
# Make sure there are 9 or fewer titles for VCD and 36 or fewere for DVD
 
412
if test "$RES" = "vcd" && test "$NUM_TITLES" -gt $MAX_VCD_TITLES; then
 
413
    OVER_MAX_TITLES=:
 
414
    DISC_TYPE="(S)VCD"
 
415
    MAX_TITLES=$MAX_VCD_TITLES
 
416
elif test "$RES" = "dvd" && test "$NUM_TITLES" -gt $MAX_DVD_TITLES; then
 
417
    OVER_MAX_TITLES=:
 
418
    DISC_TYPE="DVD"
 
419
    MAX_TITLES=$MAX_DVD_TITLES
 
420
fi
 
421
if $OVER_MAX_TITLES; then
 
422
    echo $SEPARATOR
 
423
    echo "I read $NUM_TITLES titles for your $DISC_TYPE menu, which is more than the maximum number"
 
424
    echo "of titles allowed on one menu for ${DISC_TYPE}s ($MAX_TITLES is the limit)."
 
425
    echo "Please try using fewer titles on this menu."
 
426
    cleanup
 
427
    exit 1
 
428
fi
 
429
 
 
430
# Use a foreground canvas the size of the background minus safe area
 
431
if $SAFE_AREA; then
 
432
    FG_WIDTH=$(expr $WIDTH \* 4 \/ 5)
 
433
    FG_HEIGHT=$(expr $HEIGHT \* 4 \/ 5)
 
434
else
 
435
    FG_WIDTH=$(expr $WIDTH \- 10)
 
436
    FG_HEIGHT=$(expr $HEIGHT \- 10)
 
437
fi
 
438
 
 
439
# If no button font was given with -button-font, default to the title font
 
440
if test -z "$BUTTON_FONT"; then
 
441
    BUTTON_FONT=$TITLE_FONT
 
442
fi
 
443
 
 
444
# Confirm the title font, or use a similar one
 
445
if test -s "$TITLE_FONT"; then
 
446
    # Using ttf font file for ImageMagick
 
447
    USE_TITLE_FONT="$TITLE_FONT"
 
448
else
 
449
    # Check to see if the given font name is available in ImageMagick
 
450
    # (only return the first exact match)
 
451
    USE_TITLE_FONT=$( convert -list type | \
 
452
        grep -m 1 "$TITLE_FONT" | awk '{print $1}' )
 
453
 
 
454
   # If not available, try to use something similar
 
455
    if test -z $USE_TITLE_FONT ; then
 
456
            echo $SEPARATOR
 
457
            echo "Font: \"$TITLE_FONT\" does not appear to be either a font file or registered with ImageMagick."
 
458
            USE_TITLE_FONT=$( convert -list type | \
 
459
            grep -i -m 1 "${TITLE_FONT:0:20}" | awk '{print $1}' )
 
460
 
 
461
        # If a similarly-named one does't exist, default to Helvetica
 
462
            if test -z "$USE_TITLE_FONT"; then
 
463
                echo "A similarly-named font was not found. Sorry!"
 
464
                USE_TITLE_FONT="Helvetica"
 
465
            fi
 
466
            echo "The font \"$USE_TITLE_FONT\" will be used instead."
 
467
            echo $SEPARATOR
 
468
    fi
 
469
fi
 
470
 
 
471
# Set the title font with the one that was found/compatible
 
472
TITLE_FONT="$USE_TITLE_FONT"
 
473
 
 
474
# Confirm the button font and button's existance in the font
 
475
if test $RES = 'dvd'; then
 
476
    # Confirm the button font, or use a similar one
 
477
    if test -s "$BUTTON_FONT"; then
 
478
        # Using ttf font file for ImageMagick
 
479
        USE_BUTTON_FONT="$BUTTON_FONT"
 
480
    else
 
481
        # Check to see if the given button font is available in ImageMagick
 
482
        # (only return the first exact match)
 
483
        USE_BUTTON_FONT=$( convert -list type | \
 
484
        grep -m 1 "$BUTTON_FONT" | awk '{print $1}' )
 
485
 
 
486
        # If not available, try to use something similar
 
487
        if test -z $USE_BUTTON_FONT ; then
 
488
                echo $SEPARATOR
 
489
                echo "Button: \"$BUTTON_FONT\" does not appear to be either a font file or registered with ImageMagick."
 
490
            USE_BUTTON_FONT=$( convert -list type | \
 
491
                grep -i -m 1 "${BUTTON_FONT:0:20}" | awk '{print $1}' )
 
492
 
 
493
            # If a similarly-named one does't exist, default to Helvetica
 
494
            if test -z "$USE_BUTTON_FONT"; then
 
495
                    echo "A similarly-named font was not found. Sorry!"
 
496
                    USE_BUTTON_FONT="$TITLE_FONT"
 
497
            fi
 
498
                echo "The font \"$USE_BUTTON_FONT\" will be used instead."
 
499
        echo $SEPARATOR
 
500
        fi
 
501
    fi
 
502
 
 
503
# Confirm that the button character exists in the font
 
504
    if convert -size 720x200 xc:none -fill white \
 
505
        -font $USE_BUTTON_FONT -pointsize $TITLE_SIZE -weight bold \
 
506
        +antialias -annotate +20+80 "$BUTTON_CHAR" -trim +repage \
 
507
        -bordercolor none -border 10 info: >> /dev/null 2>&1; then :
 
508
    else
 
509
        echo $SEPARATOR
 
510
        echo "The button \"$BUTTON_CHAR\" doesn't exist in \"$USE_BUTTON_FONT\""
 
511
        USE_BUTTON_FONT="$TITLE_FONT"
 
512
        echo "The font \"$USE_BUTTON_FONT\" will be used instead."
 
513
        echo $SEPARATOR
 
514
    fi
 
515
fi
 
516
 
 
517
# Set the button font with the one that was found/compatible
 
518
BUTTON_FONT="$USE_BUTTON_FONT"
 
519
 
 
520
# ==========================================================================\
 
521
# M E N U   D R A W I N G
 
522
#
 
523
#
 
524
 
 
525
# Measure the font's vertical height
 
526
TILE_HEIGHT=$(convert -size 720x200 xc:none -fill white -pointsize $TITLE_SIZE -font $TITLE_FONT -antialias $FONT_DECO -annotate +20+80 "Al" -trim +repage info: | awk '{print $3}' | awk -F 'x' '{print $2}')
 
527
 
 
528
let "TILE_HEIGHT=TILE_HEIGHT+TITLE_BOTTOM_SPACE"
 
529
 
 
530
# Make text 'tiles' for each title. These will be pieced together later on. 
 
531
echo "Adding $NUM_TITLES titles to the menu:"
 
532
for ((i=0; i<$NUM_TITLES; i++)); do
 
533
    SEQNUM=$(printf "%02d" ${i})
 
534
    let "j=i+1"
 
535
    echo "    $j: ${TITLES[i]}"
 
536
    # Enumerate (S)VCD titles
 
537
    if test $RES = "vcd" ; then
 
538
        ENTRY="${j}. ${TITLES[i]}"
 
539
 
 
540
        # Get the width of text and pad it
 
541
        TILE_WIDTH=$(convert -size 720x200 xc:none -fill $TEXT_COLOR -pointsize $TITLE_SIZE -font $TITLE_FONT -antialias $FONT_DECO -annotate +20+80 "$ENTRY" -trim +repage info: | awk '{print $3}' | awk -F 'x' '{print $1}')
 
542
        let "TILE_WIDTH=TILE_WIDTH+TITLE_LEFT_SPACE"
 
543
 
 
544
        # Draw the text, and put it on a blank tile
 
545
        convert -size 720x100 xc:none -font $TITLE_FONT -antialias $FONT_DECO -fill $TEXT_COLOR -pointsize $TITLE_SIZE -annotate +20+80 "$ENTRY" -trim +repage "$TMP_DIR/01_entry-$SEQNUM.png"
 
546
        convert -size ${TILE_WIDTH}x${TILE_HEIGHT} xc:none "$TILE_CANVAS"
 
547
        composite -compose Over -gravity north "$TMP_DIR/01_entry-$SEQNUM.png" \
 
548
            "$TILE_CANVAS" "$TMP_DIR/01_entry-$SEQNUM.png"
 
549
 
 
550
    # Use a highlight cursor for DVD titles (drawn later, for now just text)
 
551
    else
 
552
        ENTRY="${TITLES[i]}"
 
553
 
 
554
        # Get the width of the text and pad it
 
555
        TILE_WIDTH=$(convert -size 720x200 xc:none -fill $TEXT_COLOR -pointsize $TITLE_SIZE -font $TITLE_FONT -antialias $FONT_DECO -annotate +20+80 "$ENTRY" -trim +repage info: | awk '{print $3}' | awk -F 'x' '{print $1}')
 
556
        let "TILE_WIDTH=TILE_WIDTH+TITLE_LEFT_SPACE"
 
557
 
 
558
        # Draw the text, and put it on a blank tile
 
559
        convert -size 720x100 xc:none -font $TITLE_FONT -antialias $FONT_DECO -fill $TEXT_COLOR -pointsize $TITLE_SIZE -annotate +20+80 "$ENTRY" -trim +repage "$TMP_DIR/00_title-$SEQNUM.png"
 
560
        convert -size ${TILE_WIDTH}x${TILE_HEIGHT} xc:none "$TILE_CANVAS"
 
561
        composite -compose Over -gravity northeast \
 
562
            "$TMP_DIR/00_title-$SEQNUM.png" "$TILE_CANVAS" \
 
563
            "$TMP_DIR/00_title-$SEQNUM.png"
 
564
 
 
565
        # Blank placeholder: no font decorations or color
 
566
        DIMS=$(identify "$TMP_DIR/00_title-$SEQNUM.png" | grep -o -e " [0-9]\{1,\}x[0-9]\{1,\} " | tr -d ' ')
 
567
        convert \
 
568
            -size $DIMS xc:none "$TMP_DIR/00_title-${SEQNUM}_placeholder.png"
 
569
    fi
 
570
done
 
571
 
 
572
# Finish DVD menu entries (all we have so far is text, no buttons!)
 
573
if test $RES = "dvd"; then
 
574
    echo "Making the DVD buttons...   "
 
575
 
 
576
    # Get the dimensions of the button
 
577
    TILE_WIDTH=$(convert -size 720x200 xc:none -fill white -stroke $BTN_STROKE -strokewidth 1 -pointsize $TITLE_SIZE -font $BUTTON_FONT +antialias -weight bold -annotate +20+80 "$BUTTON_CHAR" -trim +repage info: | awk '{print $3}' | awk -F 'x' '{print $1}')
 
578
    BUTTON_HEIGHT=$(convert -size 720x200 xc:none -fill white -stroke $BTN_STROKE -strokewidth 1 -pointsize $TITLE_SIZE -font $BUTTON_FONT +antialias -weight bold -annotate +20+80 "$BUTTON_CHAR" -trim +repage info: | awk '{print $3}' | awk -F 'x' '{print $2}')
 
579
 
 
580
    # Find how much to pad the top of the button and make the pad
 
581
    let "BUTTON_PADDING=(TILE_HEIGHT-BUTTON_HEIGHT-TITLE_BOTTOM_SPACE)/2"
 
582
    convert -size ${TILE_WIDTH}x${BUTTON_PADDING} xc:none "$BUTTON_PAD"
 
583
 
 
584
    # Draw the buttons
 
585
    convert -size 720x100 xc:none -font $BUTTON_FONT -stroke $BTN_STROKE -strokewidth 1 +antialias -weight bold -fill $HIGHLIGHT_COLOR -pointsize $TITLE_SIZE -annotate +20+80 "$BUTTON_CHAR" -trim +repage "$HIGHLIGHT_BUTTON"
 
586
    convert -size 720x100 xc:none -font $BUTTON_FONT -stroke $BTN_STROKE -strokewidth 1 +antialias -weight bold -fill $SELECT_COLOR -pointsize $TITLE_SIZE -annotate +20+80 "$BUTTON_CHAR" -trim +repage "$SELECT_BUTTON"
 
587
 
 
588
    # Pad the buttons so that they line up with the font's baseline
 
589
    convert -background none "$BUTTON_PAD" "$HIGHLIGHT_BUTTON" -append \
 
590
        "$HIGHLIGHT_BUTTON"
 
591
    convert -background none "$BUTTON_PAD" "$SELECT_BUTTON" -append \
 
592
        "$SELECT_BUTTON"
 
593
 
 
594
    # Make a blank button tile according to measured dimensions
 
595
    convert -size ${TILE_WIDTH}x${TILE_HEIGHT} xc:none "$TILE_CANVAS"
 
596
 
 
597
    # Put padded buttons on the blank tile
 
598
    composite -compose Over -gravity north "$HIGHLIGHT_BUTTON" \
 
599
        "$TILE_CANVAS" "$HIGHLIGHT_BUTTON"
 
600
    composite -compose Over -gravity north "$SELECT_BUTTON" \
 
601
        "$TILE_CANVAS" "$SELECT_BUTTON"
 
602
 
 
603
    # A placeholder to space the title text correctly
 
604
    DIMS=$(identify "$HIGHLIGHT_BUTTON" | grep -o -e " [0-9]\{1,\}x[0-9]\{1,\} " | tr -d ' ')
 
605
    convert -size $DIMS xc:none "$BUTTON_PLACEHOLDER"
 
606
    
 
607
    # Next, join the button tiles with the title tiles:
 
608
    i=0
 
609
    while test $i -lt $NUM_TITLES; do
 
610
        SEQNUM=$(printf "%02d" $i)
 
611
        # Text entries
 
612
        convert -background none "$BUTTON_PLACEHOLDER" \
 
613
            "$TMP_DIR/00_title-$SEQNUM.png" +append \
 
614
            "$TMP_DIR/01_entry-$SEQNUM.png"
 
615
        # Highlight entries
 
616
        convert -background none "$HIGHLIGHT_BUTTON" \
 
617
            "$TMP_DIR/00_title-${SEQNUM}_placeholder.png" \
 
618
            +append "$TMP_DIR/01_highlight_entry-$SEQNUM.png"
 
619
        # Select entries
 
620
        convert -background none "$SELECT_BUTTON" \
 
621
            "$TMP_DIR/00_title-${SEQNUM}_placeholder.png" \
 
622
            +append "$TMP_DIR/01_select_entry-$SEQNUM.png"
 
623
        let "i=i+1"
 
624
    done
 
625
fi
 
626
 
 
627
# Stack all 01_entry-N.png lines
 
628
echo "Creating the text menu...   "
 
629
convert -background none "$TMP_DIR/01_entry-*.png" -append "$MENU_TEXT"
 
630
 
 
631
# Stack all DVD button lines
 
632
if test $RES = "dvd"; then
 
633
echo "Creating the DVD button overlays...   "
 
634
    convert -background none "$TMP_DIR/01_highlight_entry-*.png" -append \
 
635
        "$HIGHLIGHT_TEXT"
 
636
    convert -background none "$TMP_DIR/01_select_entry-*.png" -append \
 
637
        "$SELECT_TEXT"
 
638
fi
 
639
 
 
640
# If specified, add a menu title
 
641
if $MAKE_TITLE; then
 
642
    echo "Adding the menu title..."
 
643
    
 
644
    # Default title font size = current text size + 4
 
645
    if test -z "$MENU_TITLE_SIZE"; then
 
646
        let "MENU_TITLE_SIZE=TITLE_SIZE+8"
 
647
    fi
 
648
    
 
649
    # Draw the menu title and pad the bottom with TILE_HEIGHT pixels
 
650
    convert -size 720x100 xc:none -font $TITLE_FONT -antialias $FONT_DECO -fill $TEXT_COLOR -pointsize $MENU_TITLE_SIZE -annotate +20+80 "$MENU_TITLE" -trim +repage "$MENU_TITLE_TEXT"
 
651
    convert "$MENU_TITLE_TEXT" -gravity south -background none -splice 0x$TILE_HEIGHT "$MENU_TITLE_TEXT"
 
652
 
 
653
    # Put the menu title above the menu text (and buttons for DVDs)
 
654
    convert -background none "$MENU_TITLE_TEXT" "$MENU_TEXT" -append \
 
655
        "$MENU_TEXT"
 
656
    if test $RES = "dvd"; then
 
657
        # DVD menus need an empty placeholder for the buttons
 
658
        DIMS=$(identify "$MENU_TITLE_TEXT" | grep -o -e " [0-9]\{1,\}x[0-9]\{1,\} " | tr -d ' ')
 
659
        convert -size $DIMS xc:none "$MENU_TITLE_PLACEHOLDER"
 
660
        convert -background none "$MENU_TITLE_PLACEHOLDER" "$HIGHLIGHT_TEXT" \
 
661
            -append "$HIGHLIGHT_TEXT"
 
662
        convert -background none "$MENU_TITLE_PLACEHOLDER" "$SELECT_TEXT" \
 
663
            -append "$SELECT_TEXT"
 
664
    fi
 
665
 
 
666
fi
 
667
 
 
668
# Put the text menus inside the safety area
 
669
if $SAFE_AREA; then FRAME_TYPE="safe area"; else FRAME_TYPE="frame"; fi
 
670
echo "Placing text menu in the $FRAME_TYPE...   "
 
671
convert -size ${FG_WIDTH}x${FG_HEIGHT} xc:none "$SAFETY_CANVAS"
 
672
composite -compose Over -gravity $TEXT_ALIGN \
 
673
    "$MENU_TEXT" "$SAFETY_CANVAS" "$ACTIVE_TEXT"
 
674
 
 
675
# Same for DVD buttons: into the safety area
 
676
if test $RES = 'dvd'; then
 
677
    echo "Placing DVD buttons in the $FRAME_TYPE...   "
 
678
    composite -compose Over -gravity $TEXT_ALIGN \
 
679
        "$HIGHLIGHT_TEXT" "$SAFETY_CANVAS" "$ACTIVE_HIGHLIGHT"
 
680
    composite -compose Over -gravity $TEXT_ALIGN \
 
681
        "$SELECT_TEXT" "$SAFETY_CANVAS" "$ACTIVE_SELECT"
 
682
fi
 
683
 
 
684
# Background image:
 
685
# If none was provided, create a default one (blue-black gradient)
 
686
if test -z "$BG_IMAGE"; then
 
687
    BG_CMD="convert -size ${WIDTH}x${HEIGHT} gradient:blue-black \
 
688
        -gravity center -matte \"$BG_CANVAS\""
 
689
# Otherwise, scale/crop the provided image
 
690
else
 
691
    if test "$SCALECROP" = "crop"; then
 
692
        SCALECROP="-resize ${WIDTH}x -resize \"x${HEIGHT}<\" -gravity center \
 
693
            -crop ${WIDTH}x${HEIGHT}+0+0"
 
694
    else
 
695
        SCALECROP="-resize ${WIDTH}x${HEIGHT}!"
 
696
    fi
 
697
        BG_CMD="convert \"$BG_IMAGE\" $SCALECROP -matte \"$BG_CANVAS\""
 
698
fi
 
699
echo "Working on the background...   "
 
700
$DEBUG && echo -e "\n\nBackground:\n$BG_CMD" >> $REDIR
 
701
eval $BG_CMD
 
702
 
 
703
# Put menu text over the background image
 
704
MAGICK_CMD="composite -compose Over -gravity center \
 
705
    \"$ACTIVE_TEXT\" \"$BG_CANVAS\" -depth 8 \"$MENU_PPM\""
 
706
echo "Centering the $FRAME_TYPE over the background...   "
 
707
$DEBUG && echo -e "\n\nMenu Text:\n$MAGICK_CMD" >> $REDIR
 
708
eval $MAGICK_CMD
 
709
 
 
710
# Same for DVDs
 
711
if test $RES = 'dvd'; then
 
712
    echo "Positioning the DVD button overlays on the background...   "
 
713
    composite -compose Src -gravity center \
 
714
        "$ACTIVE_HIGHLIGHT" "$BG_CANVAS" "$HIGHLIGHT_PNG"
 
715
    composite -compose Src -gravity center \
 
716
        "$ACTIVE_SELECT" "$BG_CANVAS" "$SELECT_PNG"
 
717
fi
 
718
 
 
719
 
 
720
# ======================================================================\
 
721
# P R E V I E W   &   F I N I S H
 
722
#
 
723
#
 
724
 
 
725
if ! $NOASK; then
 
726
    echo "Generating preview..."
 
727
    echo "Type 'q' to close the preview window."
 
728
    if test "$RES" = vcd; then
 
729
        display "$MENU_PPM"
 
730
    else
 
731
        composite -compose Over -gravity center "$HIGHLIGHT_PNG" \
 
732
            "$MENU_PPM" miff: | display
 
733
    fi
 
734
    wait
 
735
    echo -n "Was the preview ok [y/N]?  "
 
736
    ANSWER="N"
 
737
    read ANSWER
 
738
    if ! ( test "$ANSWER" = "y" || test "$ANSWER" = "Y" ); then 
 
739
        echo "Preview was not OK, exiting..."
 
740
        cleanup
 
741
        echo
 
742
        echo "Try:  -font FONT -fontsize NUM -textcolor '#RGB'"
 
743
        echo "      -fontdeco 'ImageMagick instr' -align left|center|right"
 
744
        echo "      -background IMAGE -audio AUDIOFILE"
 
745
        echo "to change the appearance. More in 'man makemenu'"
 
746
        exit 0
 
747
    else 
 
748
        echo "Preview was OK. Continuing..."
 
749
    fi
 
750
fi
 
751
 
 
752
# Background audio:
 
753
# If no background audio was provided, generate 4 seconds of silence
 
754
if test -z "$BG_AUDIO"; then
 
755
    if test -z "$MENU_LENGTH"; then
 
756
        MENU_LENGTH="-t 4"
 
757
    fi
 
758
    echo "Creating $(echo "$MENU_LENGTH" | cut -c 4-)-second silent $ASUF audio...   "
 
759
    AUDIO_CMD="ffmpeg -f s16le -i /dev/zero -ac 2 -ar $SAMPRATE -ab 224k \
 
760
        $MENU_LENGTH -acodec $ASUF -y \"$AUDIO_STREAM\""
 
761
    $DEBUG && echo -e "\n\nBG audio:\n$AUDIO_CMD" >> $REDIR
 
762
    eval $AUDIO_CMD >> $REDIR 2>&1
 
763
# Otherwise, convert provided audio to the target format
 
764
else
 
765
    echo "Converting \"$BG_AUDIO\" to $ASUF...   "
 
766
    AUDIO_CMD="ffmpeg -i \"$BG_AUDIO\" -ac 2 -ar $SAMPRATE -ab 224k \
 
767
        $MENU_LENGTH -acodec $ASUF -y \"$AUDIO_STREAM\""
 
768
    $DEBUG && echo -e "\n\nBG audio:\n$AUDIO_CMD" >> $REDIR
 
769
    eval $AUDIO_CMD >> $REDIR 2>&1
 
770
fi
 
771
 
 
772
# Calculate video length from length of audio
 
773
VID_LENGTH=$(mplayer -quiet -identify -frames 0 -vo null -ao null \
 
774
    "$AUDIO_STREAM" 2>&1 | grep '^ID_LENGTH' | awk -F '=' '{print $2}' | \
 
775
    sed -e "s/\.[0-9]*//g")
 
776
VID_LENGTH=$(expr $VID_LENGTH \* $FPS \/ 100)
 
777
 
 
778
# Make sure VID_LENGTH is nonzero
 
779
if test $VID_LENGTH -eq 0 ; then
 
780
    # Use 30 seconds as a sensible default
 
781
    VID_LENGTH=$(expr 30 \* $FPS \/ 100)
 
782
fi
 
783
 
 
784
# Make video of specified length from PPM background image
 
785
echo "Converting menu image to video. This may take a while...   "
 
786
VIDEO_CMD="ppmtoy4m $PPM_OPTS -n $VID_LENGTH -r \"$MENU_PPM\" 2>>$REDIR | \
 
787
  mpeg2enc -a 2 $MPEG2ENC_FMT $MPEG2ENC_SYS -o \"$VIDEO_STREAM\" >> $REDIR 2>&1"
 
788
$DEBUG && echo -e "\n\nTo mpeg:\n$VIDEO_CMD" >> $REDIR
 
789
eval $VIDEO_CMD
 
790
 
 
791
# Multiplex audio and video
 
792
echo "Multiplexing video and audio streams...   "
 
793
MPLEX_CMD="mplex $MPLEX_OPTS $MPEG2ENC_FMT -o \"$MENU_MPEG\" \
 
794
  \"$VIDEO_STREAM\" \"$AUDIO_STREAM\" >> $REDIR 2>&1"
 
795
$DEBUG && echo -e "\n\nMux:\n$MPLEX_CMD" >> $REDIR
 
796
eval $MPLEX_CMD
 
797
 
 
798
# For DVD, create spumux XML
 
799
if test "$RES" = "dvd"; then
 
800
 
 
801
(cat << EOF
 
802
<subpictures>
 
803
  <stream>
 
804
  <spu force="yes" start="00:00:00.00"
 
805
       highlight="$HIGHLIGHT_PNG"
 
806
       select="$SELECT_PNG"
 
807
       autooutline="infer">
 
808
  </spu>
 
809
  </stream>
 
810
</subpictures>
 
811
EOF
 
812
) > "$SPUMUX_XML"
 
813
    wait
 
814
    SPUMUX_CMD="spumux \"$SPUMUX_XML\" < \"$MENU_MPEG\" > \"$OUT_FILENAME\" 2>>$REDIR"
 
815
    echo "Adding the DVD buttons to menu...   "
 
816
    $DEBUG && echo -e "\n\nSpumux buttons:\n$SPUMUX_CMD" >> $REDIR
 
817
    eval $SPUMUX_CMD
 
818
 
 
819
# Rename the output menu for VCD and SVCD
 
820
else
 
821
    mv "$MENU_MPEG" "$OUT_FILENAME"
 
822
fi
 
823
 
 
824
cleanup
 
825
 
 
826
echo $SEPARATOR
 
827
if test -s "$OUT_FILENAME"; then
 
828
    if ! $QUIET; then
 
829
        echo "Finished! Your menu should be in the file \"$OUT_FILENAME\"."
 
830
        if test "$RES" = "vcd"; then
 
831
            echo "You can use this menu on an SVCD or VCD disc with:"
 
832
            echo
 
833
            echo "  makexml -vcd -menu \"$OUT_FILENAME\" <title 1> <title 2> ... \\"
 
834
            echo "          <output name>"
 
835
        else
 
836
            echo "You can use this menu on a DVD disc with:"
 
837
            echo
 
838
            echo "  makexml -dvd -menu \"$OUT_FILENAME\" <title 1> <title 2> ... \\"
 
839
            echo "          -out <output name>"
 
840
        fi
 
841
 
 
842
        echo
 
843
        echo "where <title N> is an MPEG video file corresponding to title N as listed"
 
844
        echo "on your menu. Run 'makexml' without any options for more usage information."
 
845
    else
 
846
        echo "Done!"
 
847
    fi
 
848
else
 
849
    echo "It looks like something went wrong, because there is no output file."
 
850
    echo "Please submit a bug report on the tovid homepage: add -debug to your"
 
851
    echo "command (makemenu -debug ...) and post the log file and terminal"
 
852
    echo "output. Sorry for the inconvenience. http://www.tovid.org"
 
853
    exit 1
 
854
fi
 
855
 
 
856
$QUIET || echo $SEPARATOR
 
857
$QUIET || echo "Thanks for using makemenu!"
 
858
exit 0