# 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 all the video files from dc5video.debian.net
# (except duplicates in 2005-07-15) under a sub-directory called
# dc5video.debian.net.

# You MUST use a UTF-8 locale when calling make.

# 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

# 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 := 360
    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 := 432
    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 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/08-Debian_New_Maintainer_Process-Hanna_Wallach_Dafydd_Harries_Moray_Allan.mpeg \
        dc5video.debian.net/2005-07-15/Are_we_really_devoted_to_our_users-by_Margarita_Manterola.mpeg \
        dc5video.debian.net/2005-07-15/Debian_Women_and_Women_in_Free_Software-by_Erinn_Clark__Magni_Onsoien.mpeg \
        dc5video.debian.net/2005-07-15/distributed_rev_control-by_Mark_Shuttleworth.mpeg \
        dc5video.debian.net/2005-07-15/FriJul15-0856.mpg \
        dc5video.debian.net/2005-07-15/FriJul15-1345.mpg \
        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/Next-generation_Debian_initscripts-Henrique_de_Moraes_Holschuh.mpeg \
        dc5video.debian.net/2005-07-17/Using_Debian_for_science_research-Helen_Faulkner.mpeg \
        dc5video.debian.net/2005-07-17/Volatile_archive_for_Debian-Andreas_Barth.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, \
              $(patsubst %.mpg,%.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, \
              $(patsubst %.mpg,%.vob, \
                $(TITLE_SOURCES_1)))))
$(BUILD_DIR)/debconf5-video-2-$(STANDARD).iso : \
	$(addprefix $(BUILD_DIR)/, \
	  $(patsubst %.dvdmenu,%-menu.vob, \
            $(MENU_SOURCES_2)) \
	  $(patsubst %.mpeg,%.vob, \
            $(patsubst %.mpg,%.vob, \
              $(TITLE_SOURCES_2))))

$(BUILD_DIR)/debconf5-video-%-$(STANDARD).iso : debconf5-%.dvdauthor
	rm -rf $(DVD_TREE)
	ln -sf $(BUILD_DIR) vobs
	dvdauthor -j -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: Don't we need to 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
	./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) \
    -lavcopts vcodec=$(VIDEO_CODEC):vbitrate=$(VIDEO_BITRATE):vratetol=100000:vqcomp=0.8:keyint=$(MAX_GOP):acodec=$(AUDIO_CODEC):abitrate=$(AUDIO_BITRATE) \
    -vf scale=$(SIZE),harddup $(RESAMPLE)

AUDIO_CODEC_TYPE := lavc
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_STFU := 2>&1 >/dev/null | grep -v "^[0-9]* duplicate frame\|^$$\|frameno\.avi\| but disabled$$\| font[: ]\|selected video_out device is incompatible"

MENCODER_COMMAND = mkdir -p $(dir $@) && \
	(mencoder $(MENCODER_FLAGS) -o $@ $< || rm -f $@) $(MENCODER_STFU) && \
	test -f $@

# Convert MPEG1 or AVI (assumed to use MPEG4) to MPEG2 PS
$(BUILD_DIR)/%.vob : %.mpeg
	$(MENCODER_COMMAND)
$(BUILD_DIR)/%.vob : %.mpg
	$(MENCODER_COMMAND)
$(BUILD_DIR)/%.vob : %.avi
	$(MENCODER_COMMAND)

# The DebCamp videos were recorded at 16:9 aspect and originally encoded
# at higher rates than the others, so maintain these 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)

# 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 :=

# The sources for these were recorded at 29.97fps for some reason.
# mencoder doesn't recognise this as being essentially the same as
# 30000/1001 so fudge it.
$(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 : \
	MENCODER_FLAGS += -fps 30000/1001

# These sources are available under two different names.  Add links to
# make the expected names if they are available under the other names.
dc5video.debian.net/2005-07-15/Debian_Women_and_Women_in_Free_Software-by_Erinn_Clark__Magni_Onsoien.mpeg :
	ln dc5video.debian.net/2005-07-15/FriJul15-0953.mpg $@
dc5video.debian.net/2005-07-15/distributed_rev_control-by_Mark_Shuttleworth.mpeg :
	ln dc5video.debian.net/2005-07-15/FriJul15-1300.mpg $@
dc5video.debian.net/2005-07-15/Are_we_really_devoted_to_our_users-by_Margarita_Manterola.mpeg :
	ln dc5video.debian.net/2005-07-15/FriJul15-1440.mpg $@

# These sources were originally available under names including
# accented characters, with either ISO 8859-1 or UTF-8 encoding.
# Add links to make the expected names, if possible.
dc5video.debian.net/2005-07-09/08-Extremadura_Countys_70000_Debian_Workstations-Dario_Rapisardi_and_Pedro_Perez.mpeg :
	ln dc5video.debian.net/2005-07-09/08-Extremadura_Countys_70000_Debian_Workstations-Dario_Rapisardi_and_Pedro_Pérez.mpeg $@ \
	|| ln dc5video.debian.net/2005-07-09/08-Extremadura_Countys_70000_Debian_Workstations-Dario_Rapisardi_and_Pedro_Prez.mpeg $@
dc5video.debian.net/2005-07-09/10-Developing_Software_for_Debian-Martin_Eric_Racine.mpeg :
	ln dc5video.debian.net/2005-07-09/10-Developing_Software_for_Debian-Martin_Éric_Racine.mpeg $@ \
	|| ln dc5video.debian.net/2005-07-09/10-Developing_Software_for_Debian-Martin_ric_Racine.mpeg $@

.PHONY : all clean default
