#!/bin/bash
# a gross hack

#
# written by / copyright 2006, 2008 Holger Levsen <holger@layer-acht.org>
# licenced under GPL
#

#
# needs dvgrab, ffmpeg, ffmpeg2theora, mencoder
#

# grab like this:
#
#dvgrab --size 0 --format qt etch+1_aba

AUTHOR="FOSDEM 2008 videoteam"
COPYRIGHT="DebConf6 Video Licence"
# see http://meetings-archive.debian.net/pub/debian-meetings/LICENSE

HERTZ=32000 		# use the same as the dv files

SOURCE=/home/fosdem/rawdv
TARGET=/home/fosdem/encoded


#mkdir -p ${TARGET}/xvid ${TARGET}/mpeg/720x576 ${TARGET}/ogg_theora/720x576 ${TARGET}/ogg_theora/384x288 ${TARGET}/mpeg/384x288
mkdir -p  ${TARGET}/mpeg/720x576 ${TARGET}/ogg_theora/720x576 ${TARGET}/ogg_theora/384x288 


  todo[0]="Apt_Xapian_Index___Enrico_Zini.dv  00:00:00  00:51:23  00:51:23"
  todo[1]="Creating_a_nice_view_into_the_Debian_archives___Frank_Lichtenheld.dv  00:00:25  00:36:09  00:36:34"
  todo[2]="Cross-building_Debian_and_Tdebs___Neil_Williams.dv  00:00:00  00:54:39  00:54:39"
  todo[3]="How_accessible_is_Debian___Samuel_Thibault.dv  00:02:42  00:29:27  00:32:09"
  todo[4]="Lenny_the_past_and_the_future___Luk_Claes.dv  00:00:06  00:41:52  00:41:58"
  todo[5]="Persistent_device_naming___Frans_Pop.dv  00:05:56  00:26:24  00:32:20"
  todo[6]="Reordering_the_Debian_boot_sequence_for_correctness_and_speed___Petter_Reinholdtsen.dv  00:00:06  00:43:41  00:43:47"
  todo[7]="Running_Debian_on_Inexpensive_Network_Storage_Devices___Martin_Michlmayr.dv  00:00:00  00:49:40  00:49:40"
  todo[8]="The_Belgian_electronic_ID_card_in_Debian___Wouter_Verhelst.dv  00:00:00  00:27:06  00:27:06"
  todo[9]="The_long_road_to_Debian_Edu_in_main___Holger_Levsen.dv  00:00:00  00:40:27  00:40:27"
  todo[10]="Update_on_Virtualization_in_Debian___Henning_Sprang.dv  00:00:00  01:10:55  01:10:55"
  todo[11]="etch-n-half_-_Supporting_new_hardware_within_stable___Andreas_Barth.dv  00:00:00  00:34:23  00:34:23"
  todo[12]="git___Pierre_Habouzit.dv  00:05:03  00:43:47  00:48:50"
  todo[13]="netconf_-_design_and_overview___Martin_Krafft.dv  00:00:15  00:56:34  00:56:49"

  cd ${SOURCE} 

  DNR=10
  while [ $DNR -lt ${#todo[*]} ] ; do
    dvfile=`echo ${todo[$DNR]} | cut -d " " -f 1`
    # use cut to get the pieces
    begin=`echo ${todo[$DNR]} | cut -d " " -f 2`
    # use this date hack to get the seconds
    begin=`date -u --date "Jan 1, 1970 $begin +0000" +%s`
    ffmend=`echo ${todo[$DNR]} | cut -d " " -f 3`
    ffmend=`date -u --date "Jan 1, 1970 $ffmend +0000" +%s`
    f2tend=`echo ${todo[$DNR]} | cut -d " " -f 4`
    f2tend=`date -u --date "Jan 1, 1970 $f2tend +0000" +%s`
    echo $dvfile		
    echo $begin	i		# where the action starts
    echo $ffmend		# ffmpeg uses then relative length
    echo $f2tend		# while ffmpeg2theora uses the absolute end
    i=$dvfile
    # low quality theoras
    [ ! -f $TARGET/ogg_theora/384x288/${i%.dv}.ogg ] && time nice -n 19 ffmpeg2theora -s $begin -e $f2tend --artist "$AUTHOR" --copyright "$COPYRIGHT" -x 384 -y 288 -V 200 -H $HERTZ -a 5 -c 1 $i -o  $TARGET/ogg_theora/384x288/${i%.dv}.ogg 
    # good theoras
    [ ! -f $TARGET/ogg_theora/720x576/${i%.dv}.ogg ] && time nice -n 19 ffmpeg2theora -s $begin -e $f2tend --artist "$AUTHOR" --copyright "$COPYRIGHT" -x 720 -y 576 -V 1200 -H $HERTZ -a 5 -c 2 $i -o  $TARGET/ogg_theora/720x576/${i%.dv}.ogg 
    # good mpegs
    [ ! -f $TARGET/mpeg/720x576/${i%.dv}.mpeg ] && time nice -n 19 ffmpeg -ss $begin -t $ffmend -author "$AUTHOR" -copyright "$COPYRIGHT" -i $i -target pal-dvd -b 1500 $TARGET/mpeg/720x576/${i%.dv}.mpeg 
    echo "----"
    let "DNR += 1"
  done

# create md5sums
cd $TARGET/ogg_theora/384x288 && rm MD5SUMS.txt ; md5sum * > MD5SUMS.txt 
cd $TARGET/ogg_theora/720x576 && rm MD5SUMS.txt ; md5sum * > MD5SUMS.txt 
cd $TARGET/mpeg/720x576 && rm MD5SUMS.txt ; md5sum * > MD5SUMS.txt 


cd $SOURCE
cd ..
# xvids
#cd ${SOURCE} && time for i in *.dv ; do time nice -n 19 mencoder "$i" -of avi -ovc xvid -xvidencopts bitrate=1800 -oac mp3lame -srate 48000 -o ../$TARGET/xvid/${i%.dv}_xvid.avi ; done && cd ../$TARGET/xvid && rm MD5SUMS.txt && md5sum * > MD5SUMS.txt && cd ../..

# create dvd
