An off-duty NYPD officer put a gun in his mouth and nearly pulled the trigger after he was stopped on Long Island for a traffic violation, according to multiple sources.

Long Island officers were able to convince the despondent cop to put the weapon down before they called for backup, police sources told The Post.

“They had to talk him out of it,” one police source said.

The officer, who is in his 30s and was on his way into work at a Brooklyn precinct, but was not in uniform, was taken to Nassau University Medical Center.

“I never would have expected him to do this,” said a source who knew the cop.

The NYPD is facing a mental health emergency amid a spate of 10 officer suicides this year — a rate already more than double what it was in previous years.

Experts have blamed the deaths, in-part, on an anti-cop sentiment that swelled in the wake of Eric Garner’s 2014 death.

“The abuse they have to put up with is taking a toll,” John Petrullo, director of the Police Organization Providing Peer Assistance, previously told The Post.

The anti-cop sentiment was on full display Friday, with more than 1,000 protesters taking to the streets of Brooklyn to protest a planned subway fare-evasion crackdown.

Protesters held signs reading “F–k the police” and “Punch that cop!”

A day earlier, cop-hating vandals in Brownsville reportedly dumped piles of garbage onto a cop cruiser on Halloween night.

“Trick or treat, motherf—rs!” onlookers yelled, according to witnesses.

Cops have said the NYPD needs to do more to prevent officers from taking their own lives — a sentiment echoed Saturday in the wake of the near-death incident on Long Island.

“This is a real issue and as long as they keep acting like it’s not it’s going to keep happening,” the police source said.