A woman found footage of a man raping her while trying to uncover his alleged affair.
The victim was sickened to discover Martin Gavin had taken advantage of her while she was asleep.
A judge heard how the supermarket worker had also taken “several hundred” intimate photos of the woman.
The 59-year-old first offender was jailed for five years and seven months at the High Court in Glasgow on Monday.
He earlier pled guilty to rape, sexual assault and recording the victim without her knowledge.
The crimes spanned between 2012 and 2016.
Prosecutor Alan Parfery told how the victim “became suspicious” in September 2020 that Gavin was having an affair.
Mr Parfery: “She looked at his iPad and saw a picture of a naked woman.
“This gave her further cause to suspect that he was being unfaithful.”
The woman was later back at his home in Whitburn, West Lothian, while Gavin was not in.
She noticed his iPad was no longer in its usual place, but two USB sticks were there instead.
The woman took the storage devices away with her.
Rather than finding evidence of any adultery, she found how predator Gavin had preyed on her.
Mr Parfery: “She saw numerous images of her.
“She expressed shock and was unaware they had been taken by Gavin.
“There was also a recording of Gavin setting up a camera before she came into the room.”
A friend of the woman reported the find to police on her behalf.
Officers went on to recover a number of electronic devices belonging to Gavin.
They contained videos which included Gavin having sex with the woman while she was asleep.
There were a further three clips of him molesting her while she was unaware.
Mr Parfery told the court of “several hundred” intimate images of the victim.
Frances Connor, defending, said: “When I first consulted with him, he indicated immediately his intention to accept responsibility for his wrongdoing.
“He could not tell me in great detail the content of these devices.”
The court heard how social workers compiling a pre-sentencing report “took a particularly dim view” to Gavin apparently trying to lessen the seriousness of the crimes.
But, judge Lady Stacey told Gavin: “This is a horrific breach of trust. What you did without her knowledge is extremely serious.”
Gavin was also put on the sex offenders list.
Detective inspector Jennifer Priestley, from the Divisional Rape Investigation Unit, said: “Justice has been done and Martin Gavin now faces the consequences of his despicable and distressing actions with a jail sentence.
“Sexual crime, of any nature, is not to be tolerated and we will thoroughly investigate any reports made to us, no matter how much time has passed.
“I would urge people to come forward and you can be assured of being fully supported by officers and our partner agencies.”