Chris Hume, 29, admitted the offences which took place in a Glasgow flat on 11 October 2016.
The High Court in Glasgow heard he had never been alone with the child until the day of the incident, when he was asked to babysit him.
Judge Lady Scott also placed Hume on the sex offenders' register.
The judge told him: "Unfortunately for you, your young victim immediately told his parents and the footage was found on the phone."
The court heard the boy's father had left the flat at about 14:00 to pick up his wife and asked Hume to babysit.
Prosecutor Angela Gray said: "The accused took the opportunity to rape the boy and whilst doing so filmed the act using his mobile telephone."
When the parents returned home Hume was sitting on the couch and the boy was in his bedroom.
The child then entered the living room and accused Hume of sexually abusing him, the court was told.
At this point Hume became irate and started to wave his arms around and told the boy: "Mate, you were the one that was being bad and pulling down your trousers."
He added: "I am no having this" and swore at the boy.
Hume then ran away leaving behind his two mobile phones and his jacket.
The family contacted the police and Police Scotland's cybercrime unit managed to extract footage from Hume's phone.
Defence counsel Frances Connor said: "My client is genuinely horrified by his actions that day."
She told the court he was undergoing programmes to try to understand why he committed the offences.
A paedophile who filmed himself sexually abusing a three-year-old boy he had been asked to look after has been jailed for 45 months.