#!/usr/bin/perl ############################################################################## # Winfred van Kuijk - http://winfred.vankuijk.net # # Parse the metadata of a .hmt file coming from a Humax 5050C # The layout is described in the excellent document from Peter: # http://www.patrn.nl/Projects/5050c/5050c.html # # syntax: hmt_meta # # v1.0, 2009/12/31, initial release ############################################################################## use Encode; #################################################################################### # subroutine: read specific bytes from the file # - offset, length: where to start and how many bytes # - type: 1 for numbers, 0 for string # - desc: optional, if provided: will be used as label to display the debugging details sub myseek { my($offset, $length, $type, $desc) = @_; my($buf, $result, $hexstring) = ""; my($values,$bytes) = ""; seek(FILE, $offset, 0); read(FILE, $buf, $length); if ($type == 1) { $result = hex(unpack("H*",$buf)); } else { #$result = unpack("Z*",$buf); # only keep the chars, remove the \0 $result = $buf; # fast conversion from iso-8859-1 to utf-8 (not pretty, but hey...) $result =~ s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg; #sometimes the event name starts one character later, too lazy to find out why $result =~ s/[\x00-\x05]//g; #if one later then the first char is 05, take them out $length = length($result); } if ($desc) { #for debugging, remove next comment #printf ("%5d/%05x, %3d/%03x - %12s: %s %s %s\n",$offset,$offset,$length,$length,$desc,$result, $bytes); } return $result; } #################################################################################### # the main loop sub process_hmt { open(FILE, $ARGV[0]) or die("Error reading file, stopped"); binmode FILE; #print "###################################################\n"; $timestamp = myseek(25,4,1); # timestamp - localtime(...) (0x19) $name = myseek(288,255,0,"name"); #name of what we wanted to record (0x120) $channel_nr = myseek(hex(11),2,1,"channel nr"); # Humax channel nr, not used $channel_name = myseek(hex(220),33,0,"channel name"); #name of the channel (RTL etc.) #---------------------------------------------------------------- # the file can contain multiple events # the event we recorded is not necessarily the first one # e.g.: recorded "yes", event list "no, yes, no, no" or "yes, no, no", or ... $count = myseek(hex(1000),4,1,"events"); #how many events are there? (0x1000) $start = hex(1004); # start of the first event (0x1004) for ($loop = 1; $loop <= $count; $loop++) { ##print "---- event #".$loop."/".$count." ------------------------\n"; $event_id[$loop] = myseek($start,2,1,"event id"); # unique nr for event, not used (+ 0x2) $event_name[$loop] = myseek($start+21,40,0,"event name"); # name of the event (+ 0x15) $event_size[$loop] = myseek($start+540,4,1,"event size"); # nr of bytes for event (+ 0x21) $event_desc[$loop] = ""; # the description of each event is broken up in blocks $block_cnt = myseek($start+548,4,1,"blocks"); # number of description blocks (+ 0x224) $desc_start = $start + 564; # where does first block start (+ 0x234) for ($desc = 0; $desc < $block_cnt; $desc++) { $len = myseek($desc_start,2,1); # length of this block of the description $event_desc[$loop] .= myseek($desc_start+3,$len-1,0); # add (next part of) the description $event_lang[$loop] = myseek($desc_start+$len+2,3,0); # lang of description, not used $desc_start += 14 + $len; #get ready for the next block } # for debugging remove next comment (shows each description) ##print $event_desc[$loop]."\n"; $start += 540+4 + $event_size[$loop]; #get ready for the next event ##print ">".$name."< >".$event_name[$loop]."<\n"; ##print ">".length($name)."< >".length($event_name[$loop])."<\n"; if ($event_name[$loop] eq $name) { $description = $event_desc[$loop]; } } #print "$name ($channel_name: ".gmtime($timestamp).")\n"; print "$name\n"; print "$channel_name\n"; print gmtime($timestamp)."\n"; print "$description\n"; close(FILE); } #------------------------------------------------------------------------------ # to do: combine the transfer_transcode shell script and this one # step 0: parse the arguments # define the variables: url, url_hmt, output filename # wget the url_hmt # if fails: Humax not online -> exit # process hmt to get metadata process_hmt(); # wget the url + transcode # add the metadata # use iFlicks to add additional metadata and add to iTunes