A “delusional” YouTube software engineer attacked four friends — including one he stabbed with a pencil — and rammed a stolen truck into pedestrians in an LSD-fueled rampage that left eight people injured in California, authorities said.

Suspect Betai Koffi, 32, of San Francisco was listed in critical condition at a hospital after he was shot at least three times by a sheriff’s deputy in Bodega Bay during his frenzied Fourth of July acid trip, authorities said.

Koffi, whose LinkedIn page identified him as a YouTube employee since November, had rented a home for the holiday with five longtime friends in Bodega Bay in Sonoma County and took two doses of LSD about noon July 4, Sgt. Spencer Crum said in a news release.

Koffi “became delusional” after ingesting the hallucinogenic, prompting his friends to try to keep him calm, authorities said. But then he took two more doses hours later, setting off a violent confrontation late Thursday when his friends tried to stop him from leaving the rental home, Crum said.

Authorities said Koffi escaped the residence after attacking his friends inside — stabbing one man with a pencil, punching a woman in the chest and choking another guy. He then allegedly hopped into his rented blue Hyundai but crashed into the garage of the home as he drove toward another pal trying to stop him from leaving.

Koffi got out of the sedan and ran to a nearby home, where he was confronted by a neighborhood security guard, whom he stabbed with the metal stake of a landscape light, officials said. Koffi then allegedly stole the guy’s pickup truck, which was unlocked and running at the time.

“As Koffi fled, he drove straight towards an unrelated man and woman walking on the road,” Crum said of the street incident, which was caught on video. “Koffi drove straight towards the pair and violently hit the woman with the truck, causing significant injuries.”

The man, who was hit in the arm, was not seriously wounded. But Koffi’s alleged bad acid trip continued when he drove after another man and woman walking on a nearby bluff, striking the woman and leaving her seriously hurt, Crum said.

The wild, acid-soaked hysteria only ended when the tech worker was shot through the windshield of the stolen pickup as he rammed it into a California Highway Patrol vehicle, Crum said.

The victims are expected to survive, authorities said.

“We’re really lucky that none of those victims died in this,” Crum told KTVU. “Really lucky … Several members of his party took a half tab of acid, but Koffi took four doses of acid and had a really bad trip with it.”

Several nearby home security cameras caught Koffi trying to intentionally run the four people over. He’s now facing charges of attempted murder, assault with a deadly weapon and carjacking, Crum said.

“The couple that did not see it coming, who are walking arm in arm, it’s really sad,” Crum told the station. “And when you watch the video, you can’t help but feel emotional.”

A message seeking comment from Google, which owns YouTube, was not immediately returned Monday.

The sheriff’s department intends to release footage of the attack from the deputy’s bodycam, Crum said.