Yes, it's possible to capture the button codes. Most of the Samsung Tvs use NEC infrared protocol. So, you will have no problems getting and regenerating the codes.
IRremote library, or any other infrared library will do it. You need an IR receiver IC like TSOP...
I don't understand why you need the relay. Just capture the key codes in sequence, then renegerate them whenever you need.