# Makefile for Debconf5 DVDs

# Copyright 2005 Ben Hutchings
# See "copyright" file for licence terms.

# You will need the following packages installed:
# - dvdauthor
# - mencoder (marillat)
# - mjpegtools (marillat)
# - mkisofs
# - python
# - python-gd
# - ttf-bitstream-vera

# You will also need the video files from dc5video.debian.net (as
# listed below) under a sub-directory called dc5video.debian.net.
# Define the variable DOWNLOAD if you want to have these downloaded
# automatically as needed.

# You MUST use a UTF-8 locale when generating menu VOBs (this is now
# checked by the rule for them).

# Finally, you will need a lot of disk space:
# - recoded video files: 8 GB for each of PAL and NTSC
# - temporary DVD tree: 5 GB for single-layer, 8 GB for dual-layer
# - DVD images: 8 GB for each set

# By default, this will generate single-layer disc images with video
# at PAL resolutions. Define the variable NTSC if you want to generate
# video at NTSC resolutions (currently this is broken; see TODO). 
# Define the variable DUAL_LAYER if you want to generate dual-layer
# disc images.

# Temporary directory to build the DVD tree in - this will be erased
# and created as needed.
DVD_TREE := /var/tmp/debconf5-dvd

BUILD_DIR = build-$(STANDARD)

# Determine video parameters
WIDTH := 352
WIDTH_HIRES := 704
ifdef NTSC
    HEIGHT := 240
    HEIGHT_HIRES := 480
    HEIGHT_HIRES_16_9 := 400
    STANDARD := ntsc
    FRAME_RATE := 30000/1001
    FRAME_RATE_RATIO := 30000:1001
    PIXEL_ASPECT_RATIO := 10:11
    MAX_GOP := 18
else
    HEIGHT := 288
    HEIGHT_HIRES := 576
    HEIGHT_HIRES_16_9 := 480
    STANDARD := pal
    FRAME_RATE := 25
    FRAME_RATE_RATIO := 25:1
    PIXEL_ASPECT_RATIO := 59:54
    MAX_GOP := 15
endif

# Output video size
SIZE := $(WIDTH):$(HEIGHT)

# Output codecs and bit rates
VIDEO_CODEC := mpeg1video
VIDEO_BITRATE := 300
AUDIO_CODEC := mp2
AUDIO_BITRATE := 96

# Source videos
TITLE_SOURCES_1 := \
        dc5video.debian.net/debcamp/1st-days.mpeg \
        dc5video.debian.net/debcamp/dc5-2nd_video.mpeg \
	dc5video.debian.net/debcamp/dc5-3rd-960x540.avi \
        dc5video.debian.net/2005-07-09/01-Greetings-Martin_Andreas_Branden.mpeg \
        dc5video.debian.net/2005-07-09/02-What_is_Debian-Jaldhar_H_Vyas.mpeg \
        dc5video.debian.net/2005-07-09/03-What_is_Free_Software-Gunnar_Wolf.mpeg \
        dc5video.debian.net/2005-07-09/04-Linux_Aktivaattori-Martin_Eric_Racine.mpeg \
        dc5video.debian.net/2005-07-09/05-Custom_Debian_Distributions_and_Debian_Derivatives-Aigars_Mahinovs.mpeg \
        dc5video.debian.net/2005-07-09/06-A_Travel_Through_Debian-Juan_Jose_Amor.mpeg \
        dc5video.debian.net/2005-07-09/07-Debian_Release_Processes-Andreas_Barth.mpeg \
	dc5video.debian.net/2005-07-09/08-Extremadura_Countys_70000_Debian_Workstations-Dario_Rapisardi_and_Pedro_Perez.mpeg \
        dc5video.debian.net/2005-07-09/09-Getting_Involved_as_an_End_User-Alexander_Schmehl.mpeg \
        dc5video.debian.net/2005-07-09/10-Developing_Software_for_Debian-Martin_Eric_Racine.mpeg \
        dc5video.debian.net/2005-07-09/11-How_Hewlett_Packard_benefits_from_Debian-Bdale_Garbee.mpeg \
        dc5video.debian.net/2005-07-10/01-Welcome_to_Debconf-Debconf5_Organizers.mpeg \
        dc5video.debian.net/2005-07-10/02-Debootstrap-Anthony_Towns.mpeg \
        dc5video.debian.net/2005-07-10/03-Small_Teams_in_Debian-Andreas_Schuldei.mpeg \
        dc5video.debian.net/2005-07-10/04-Write_the_Fine_Manual-Branden_Robinson.mpeg \
        dc5video.debian.net/2005-07-10/05-Multiarch_A_Proposal_and_an_Implementation-Tollef_Fog_Heen.mpeg \
        dc5video.debian.net/2005-07-10/06-Debtags-Enrico_Zini.mpeg \
        dc5video.debian.net/2005-07-11/01-Appealing_Presentations_with_LaTeX_Beamer-Andreas_Tille.mpeg \
        dc5video.debian.net/2005-07-11/02-Debian_Development_in_the_Third_World_Latin_America-Gunnar_Wolf_and_Christian_Perrier.mpeg \
        dc5video.debian.net/2005-07-11/03-Debian_Derivatives_Panel-Benjamin_Mako_Hill.mpeg \
        dc5video.debian.net/2005-07-11/04-Search_Engines_in_Debian-Takatsugu_Nokubi.mpeg \
        dc5video.debian.net/2005-07-11/05-Embedded_Debian_Demo-Wookey.mpeg \
        dc5video.debian.net/2005-07-11/06-Custom_Debian_Distribution-Andreas_Tille.mpeg \
        dc5video.debian.net/2005-07-11/07-Debian_Release_Management-Andreas_Barth.mpeg \
        dc5video.debian.net/2005-07-12/01-Alternate_Approaches_to_Package_Management_and_Autobuilding-Simon_Richter.mpeg \
        dc5video.debian.net/2005-07-12/02-Software_in_the_Public_Interest_Workshop-Benjamin_Mako_Hill.mpeg \
        dc5video.debian.net/2005-07-12/03-Debbugs_Tips_Tricks_and_Hacks-Anthony_Towns-pt1.mpeg \
        dc5video.debian.net/2005-07-12/04-Debbugs_Tips_Tricks_and_Hacks-Anthony_Towns-pt2.mpeg \
        dc5video.debian.net/2005-07-12/05-OpenOffice.org_in_Debian-Chris_Halls_Rene_Engelhard.mpeg \
        dc5video.debian.net/2005-07-12/06-Writing_Enterprise_Ready_Software-Petter_Reinholdtsen.mpeg \
        dc5video.debian.net/2005-07-12/07-Linda_A_Debian_Package_Checker_Written_in_Python-Steve_Kowalik.mpeg \
        dc5video.debian.net/2005-07-12/08-Securing_the_Testing_Distribution-Joey_Hess.mpeg
TITLE_SOURCES_2 := \
        dc5video.debian.net/2005-07-14/01-Shared_Library_Packaging-Junichi_Uekawa.mpeg \
        dc5video.debian.net/2005-07-14/02-Ubuntu_Talk-Mark_Shuttleworth.mpeg \
        dc5video.debian.net/2005-07-14/03-Debian_Website_Round_Table-Frank_Lichtenheld-pt1.mpeg \
        dc5video.debian.net/2005-07-14/04-Debian_Website_Round_Table-Frank_Lichtenheld-pt2.mpeg \
        dc5video.debian.net/2005-07-14/05-Enrico_Zini-Zen_and_the_art_of_Free_Software.mpeg \
	dc5video.debian.net/2005-07-14/06-Autobuilding_Experimental-Andreas_Barth.mpeg \
        dc5video.debian.net/2005-07-14/08-Debian_New_Maintainer_Process-Hanna_Wallach_Dafydd_Harries_Moray_Allan.mpeg \
	dc5video.debian.net/2005-07-15/01-Documentation_in_Debian-Takatsugu_Nokubi.mpeg \
	dc5video.debian.net/2005-07-15/02-Freezing_HEL_Over-Scott_James_Remnant.mpeg \
	dc5video.debian.net/2005-07-15/03-Debian_Women_and_Women_in_Free_Software-Erinn_Clark_Magni_Onsoien.mpeg \
	dc5video.debian.net/2005-07-15/04-Distributed_Revision_Control-Mark_Shuttleworth.mpeg \
	dc5video.debian.net/2005-07-15/05-Evaluating_Open_Source_Software_for_Enterprise_Use-Martin_Langhoff.mpeg \
	dc5video.debian.net/2005-07-15/06-Customization_of_GNU_libc_locale_files-Denis_Barbier.mpeg \
	dc5video.debian.net/2005-07-15/07-Debian_Installer-Holger_Levsen_Joey_Hess_Christian_perrier_Frans_Pop.mpeg \
	dc5video.debian.net/2005-07-15/08-Are_We_Really_Devoted_to_Our_Users-Margarita_Manterola.mpeg \
        dc5video.debian.net/2005-07-16/Conducting_Cooperatively_towards_a_better_Debian-Jeroen_van_Wolffelaar.mpeg \
        dc5video.debian.net/2005-07-16/FAI_the_Fully_Automatic_Installation-Thomas-Lange.mpeg \
        dc5video.debian.net/2005-07-16/Safer_Hex_in_Public-Holger_Levsen.mpeg \
        dc5video.debian.net/2005-07-16/Structural_Evolution-Bdale_Garbee.mpeg \
        dc5video.debian.net/2005-07-16/Tutorial_of_translation_using_gettext-Christian_Perrier-part1.mpeg \
        dc5video.debian.net/2005-07-16/Tutorial_of_translation_using_gettext-Christian_Perrier-part2.mpeg \
        dc5video.debian.net/2005-07-17/Cross_Compiling_Debian_from_Scratch-Timo_Savola.mpeg \
	dc5video.debian.net/2005-07-17/01-Next_Generation_Debian_Initscripts-Henrique_de_Moraes_Holschuh.mpeg \
	dc5video.debian.net/2005-07-17/02-The_Debian_Free_Software_Guidelines-Matthew_Garret.mpeg \
	dc5video.debian.net/2005-07-17/03-Volatile_Archive_for_Debian-Andreas_Barth.mpeg \
	dc5video.debian.net/2005-07-17/04-Free_Standards-Matt_Taggart.mpeg \
	dc5video.debian.net/2005-07-17/05-Cross_Compiling_Debian_from_Scratch-Timo_Savola.mpeg \
	dc5video.debian.net/2005-07-17/07-Using_Debian_for_Science_Research-Helen_Faulkner.mpeg \
	dc5video.debian.net/2005-07-17/08-Debconf_Closing_Remarks-Alexander_Schmehl_Branden_Robinson.mpeg
TITLE_SOURCES_DL := $(TITLE_SOURCES_1) $(TITLE_SOURCES_2)
MENU_SOURCES_1 := main.dvdmenu debcamp.dvdmenu debian-day.dvdmenu   \
	talks-disc1.dvdmenu talks-10th.dvdmenu talks-11th.dvdmenu   \
	talks-12th-disc1.dvdmenu group-with-banner.ppm
MENU_SOURCES_2 := talks-disc2.dvdmenu talks-14th.dvdmenu            \
	talks-15th.dvdmenu talks-16th.dvdmenu talks-17th.dvdmenu 
MENU_SOURCES_DL := main.dvdmenu debcamp.dvdmenu debian-day.dvdmenu  \
	talks.dvdmenu talks-10th.dvdmenu talks-11th.dvdmenu         \
	talks-12th.dvdmenu talks-14th.dvdmenu talks-15th.dvdmenu    \
	talks-16th.dvdmenu talks-17th.dvdmenu group-with-banner.ppm

ifdef DUAL_LAYER
default : $(BUILD_DIR)/debconf5-video-dl-$(STANDARD).iso
else
default : \
	$(BUILD_DIR)/debconf5-video-1-$(STANDARD).iso \
	$(BUILD_DIR)/debconf5-video-2-$(STANDARD).iso
endif

all : $(BUILD_DIR)/debconf5-video-dl-$(STANDARD).iso  \
	$(BUILD_DIR)/debconf5-video-1-$(STANDARD).iso \
	$(BUILD_DIR)/debconf5-video-2-$(STANDARD).iso

clean :
	rm -f vobs $(BUILD_DIR)/*.iso
	rm -rf $(DVD_TREE)
	@echo "WARNING: It will take several hours to regenerate the VOB files."
	@echo "Interrupt within 5 seconds if you don't really want to delete them."
	sleep 5
	rm -rf $(BUILD_DIR)

$(BUILD_DIR)/debconf5-video-dl-$(STANDARD).iso :                   \
	$(addprefix $(BUILD_DIR)/,                                 \
	  $(patsubst %.dvdmenu,%-menu.vob, $(patsubst %.ppm,%.vob, \
            $(MENU_SOURCES_DL)))                                   \
	  $(patsubst %.avi,%.vob, $(patsubst %.mpeg,%.vob,         \
            $(TITLE_SOURCES_DL))))
$(BUILD_DIR)/debconf5-video-1-$(STANDARD).iso :                    \
	$(addprefix $(BUILD_DIR)/,                                 \
	  $(patsubst %.dvdmenu,%-menu.vob, $(patsubst %.ppm,%.vob, \
            $(MENU_SOURCES_1)))                                    \
	  $(patsubst %.avi,%.vob, $(patsubst %.mpeg,%.vob,         \
            $(TITLE_SOURCES_1))))
$(BUILD_DIR)/debconf5-video-2-$(STANDARD).iso :                    \
	$(addprefix $(BUILD_DIR)/,                                 \
	  $(patsubst %.dvdmenu,%-menu.vob, $(MENU_SOURCES_2))      \
	  $(patsubst %.mpeg,%.vob, $(TITLE_SOURCES_2)))

$(BUILD_DIR)/debconf5-video-%-$(STANDARD).iso : debconf5-%.dvdauthor
	rm -rf $(DVD_TREE)
	ln -sf $(BUILD_DIR) vobs
	dvdauthor -o $(DVD_TREE) -x $<
	rm -f vobs
	mkisofs -o $@ -dvd-video $(DVD_TREE) || (rm -f $@ ; false)
	rm -rf $(DVD_TREE)

# Convert PPM to MPEG2.  This is needed for including still true-colour
# images.
# TODO: Scale or use different source PPMs for NTSC and PAL.
$(BUILD_DIR)/%.mpeg2 : %.ppm
	mkdir -p $(dir $@)
	ppmtoy4m -v0 -n 1 -F $(FRAME_RATE_RATIO) -A $(PIXEL_ASPECT_RATIO) \
	    -I p -S 420_mpeg2 <$<                                         \
	    | mpeg2enc -v0 -f 8 -o $@

# Convert MPEG2 to MPEG2 PS
%.vob : %.mpeg2
	mplex -v0 -f 8 -o $@ $<

# Generate menu images and multiplex with background to create MPEG2 PS.
# The colours are white, dark red (matching the Debconf logo) and bright
# red.
$(BUILD_DIR)/%-menu.vob : %.dvdmenu $(BUILD_DIR)/menu-background.mpeg2
	test "$$(locale LC_CTYPE | head -5 | tail -1)" = "UTF-8" # Verify use of UTF-8 locale
	./make-menu-vob -w $(WIDTH_HIRES) -h $(HEIGHT_HIRES)         \
	    -n 255,255,255 -i 175,44,14 -s 255,0,0                   \
	    -f /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf \
	    -m $< -b $(BUILD_DIR)/menu-background.mpeg2 -o $@

# mencoder options
MENCODER_FLAGS = $(NOSKIP) -mc 1 -of mpeg -ovc lavc -oac $(AUDIO_CODEC_TYPE) \
    -mpegopts format=dvd:reorder:vframerate=$(FRAME_RATE):vaspect=$(FRAME_ASPECT_RATIO) \
    -lavcopts vcodec=$(VIDEO_CODEC):vbitrate=$(VIDEO_BITRATE):vratetol=100000:vqcomp=0.8:keyint=$(MAX_GOP):acodec=$(AUDIO_CODEC):abitrate=$(AUDIO_BITRATE) \
    -ofps $(FRAME_RATE) -vf scale=$(SIZE),harddup $(RESAMPLE)

# Default values for mencoder options that need to be overridden for some files.
NOSKIP := -noskip
AUDIO_CODEC_TYPE := lavc
FRAME_ASPECT_RATIO := 4/3
RESAMPLE := -af resample=48000:0:2 -srate 48000

# mencoder command-line suffix to suppress unnecessary messages.
# Unfortunately this hides the exit code so we also need to ensure the
# output file is deleted if mencoder fails.
MENCODER_JUNK := "^[0-9]* duplicate frame|^$$|frameno\.avi| but disabled$$| font[: ]|selected video_out device is incompatible"

MENCODER_COMMANDS =                                                           \
	mkdir -p $(dir $@) &&                                                 \
	(mencoder $(MENCODER_FLAGS) -o $@ $< || rm -f $@) 2>&1 >/dev/null     \
            | egrep -v $(MENCODER_JUNK) &&                                    \
	test -f $@

# Convert all sources to MPEG2 PS.
$(BUILD_DIR)/%.vob : %.mpeg
	$(MENCODER_COMMANDS)
$(BUILD_DIR)/%.vob : %.avi
	$(MENCODER_COMMANDS)

# The DebCamp videos were recorded at 16:9 aspect and made available
# at higher resolutions and rates than the others, so maintain these
# as far as possible when re-encoding.
$(BUILD_DIR)/dc5video.debian.net/debcamp/1st-days.vob        \
$(BUILD_DIR)/dc5video.debian.net/debcamp/dc5-2nd_video.vob   \
	: VIDEO_BITRATE := 1000
$(BUILD_DIR)/dc5video.debian.net/debcamp/dc5-3rd-960x540.vob \
	: VIDEO_BITRATE := 2000
$(BUILD_DIR)/dc5video.debian.net/debcamp/%.vob               \
	: VIDEO_CODEC := mpeg2video
$(BUILD_DIR)/dc5video.debian.net/debcamp/%.vob               \
	: AUDIO_BITRATE := 128
$(BUILD_DIR)/dc5video.debian.net/debcamp/%.vob               \
	: SIZE := $(WIDTH_HIRES):$(HEIGHT_HIRES_16_9)
$(BUILD_DIR)/dc5video.debian.net/debcamp/%.vob               \
	: FRAME_ASPECT_RATIO := 16/9

# These have 48 kHz MP2 audio which we need not re-encode.
$(BUILD_DIR)/dc5video.debian.net/debcamp/1st-days.vob      \
$(BUILD_DIR)/dc5video.debian.net/debcamp/dc5-2nd_video.vob \
$(BUILD_DIR)/dc5video.debian.net/2005-07-14/02-Ubuntu_Talk-Mark_Shuttleworth.vob \
	: AUDIO_CODEC_TYPE := copy
# These have either 48 kHz MP2 or MP3 audio and need not be resampled; mencoder
# will crash if we tell it to!
$(BUILD_DIR)/dc5video.debian.net/debcamp/%.vob             \
$(BUILD_DIR)/dc5video.debian.net/2005-07-14/02-Ubuntu_Talk-Mark_Shuttleworth.vob \
	: RESAMPLE :=

ifeq ($(STANDARD),pal)
# These need their frame rate reduced so don't prevent frame skipping.
NTSC_RATE_VOBS := \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-10/03-Small_Teams_in_Debian-Andreas_Schuldei.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-11/01-Appealing_Presentations_with_LaTeX_Beamer-Andreas_Tille.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-12/02-Software_in_the_Public_Interest_Workshop-Benjamin_Mako_Hill.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-12/05-OpenOffice.org_in_Debian-Chris_Halls_Rene_Engelhard.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-14/03-Debian_Website_Round_Table-Frank_Lichtenheld-pt1.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-14/04-Debian_Website_Round_Table-Frank_Lichtenheld-pt2.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-14/06-Autobuilding_Experimental-Andreas_Barth.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-15/01-Documentation_in_Debian-Takatsugu_Nokubi.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-15/06-Customization_of_GNU_libc_locale_files-Denis_Barbier.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-17/02-The_Debian_Free_Software_Guidelines-Matthew_Garret.vob \
	$(BUILD_DIR)/dc5video.debian.net/2005-07-17/04-Free_Standards-Matt_Taggart.vob
$(NTSC_RATE_VOBS) : NOSKIP :=
$(NTSC_RATE_VOBS) : MENCODER_JUNK := $(MENCODER_JUNK)"|^Skipping frame!"
endif
	
# Download missing files, if enabled.
ifdef DOWNLOAD
dc5video.debian.net/%.avi dc5video.debian.net/%.mpeg :
	mkdir -p $(dir $@)
	wget -c -O $@.partial http://$@
	mv $@.partial $@
endif

.PHONY : all clean default
.PRECIOUS : \
	dc5video.debian.net/%.avi dc5video.debian.net/%.mpeg $(BUILD_DIR)/%.vob
