A federal judge cut conservative filmmaker and author Dinesh D’Souza a break Tuesday, sentencing him to five years of probation for illegally shelling out $20,000 in contributions in the name of others for Republican Wendy Long’s failed 2012 US Senate bid to defeat incumbent Kirsten Gillibrand (D-NY).

Manhattan federal Judge Richard Berman, however, spent most of a nearly three-hour hearing ripping D’Souza’s character before sentencing him to a non-prison sentence that includes eight months in a halfway house, community service and weekly “therapeutic counseling” for breaking federal campaign laws through the use of straw donors.

Dinesh D’Souza interviews George Obama in his film “2016: Obama’s America.”AP

He called D’Souza, 53, a “compulsive talker” who was out of line by repeatedly claiming prosecutors “selectively” sought a jail sentence for him following his May guilty plea to a charge of breaking federal campaign laws — mostly because of D’Souza’s harsh criticism of President Obama.

“I think I’ve figured out a little more about Mr. D’Souza,” said Berman, while briefly taking a stab at psychoanalysis. “He’s a talker. He’s a compulsive talker. I don’t think he’s a listener.”

“I am just relieved and I want to thank the judge for issuing a fair sentence,” D’Souza said afterward.

The deck had appeared to be stacked against D’Souza after the judge played a five-minute tape of the filmmaker’s appearance on Newsmax TV and said he vehemently disagreed with his allegations during the interview of being selectively prosecuted by the feds over political beliefs.

Berman then cited an inflammatory letter he received a day earlier from D’Souza’s estranged wife, Dixie — whom the filmmaker is in the process of divorcing after 20 years of marriage.

Dixie claims in the letter her husband lacks “truthfulness” and assaulted her in April 2012 “using his purple belt karate skills.” She claimed D’Souza “kicked me in the head and shoulder, knocking me to the ground and creating injuries that pain me to this day.” She also claimed that D’Souza benefited from giving the donations because Long hooked him up with a top investor for his anti-Obama flick.

D’Souza’s lawyer, Benjamin Brafman, denied the allegations.

“Never get sentenced if you’re going through a bitter divorce,” he said.

Brafman appeared to win the judge over to his side by arguing that no first-time offender who ever pleaded guilty to the same crime — and wasn’t seeking to profit from the crime — ever was sentenced to jail time.

“There is not a single case,” Brafman told the judge.

D’Souza made two $10,000 donations on behalf of two close associate and their spouses. One was Denise Odie Joseph II, who was D’Souza’s mistress at the time. She wrote a $10,000 check to Long on behalf of herself and her husband, Louis, and was reimbursed the next day in cash by D’Souza.

Had the case gone to trial, prosecutors said, Long would’ve testified that D’Souza lied to her about the source of the donations. The straw donors also would’ve testified against D’Souza, who resigned under fire as president of the Christian-teaching King’s College in 2012 following allegations he was having an extramarital affair.

Under federal law, candidates can’t receive more than $5,000 per contributor or $10,000 per married couple for election campaigns.

Born in Mumbai, India, D’Souza is also a former policy adviser to President Ronald Reagan.

Long graduated from Dartmouth College in 1982, a year before D’Souza. The two are longtime friends.

D’Souza told the judge it was a “stupid idea” to use straw donors to bolster Long’s campaign. He claimed he only did so because he wanted to help his friend — even though he knew she had no shot at winning the election.

He said it was obvious to him she was being “humiliated” because her campaign “was kind of a joke,” so he wanted to “help” Long.