short answer is "virtualdub, textsub for virtualdub, codec of your choice"

long answer is "read, a lot. encoding (when done badly) can be trivial, but to do it well is a big undertaking. your best reference is http://www.doom9.org "