Singer Zachary Zortman gave the performance of his life while a surgeon dissected his brain.

During a six-hour operation to remove a tumor, Zortman, 29, lay on his side, awake — his skull cut open in a scene out of ‘Hannibal.’ He proceeded to croon his favorite songs, including “Thinking Out Loud.”

“I usually perform because I enjoy it, but this time I had my life on the line,” he told The Post.

Zortman sang to help his neurosurgeon, Zarina Ali, chart an incision to avoid irritating key sections of his cerebral cortex.

“The tumor was emanating from an area of the brain that controls his ability to speak — and to sing,” Ali said. “It became even more important to preserve those functions for him.”

Zortman, who lives in York, Pa., is a child therapist who moonlights as a lead singer in a two-man alternative pop band, The AKT.

While driving last September, Zortman suddenly lost his ability to speak. Because it had happened before, girlfriend Annie Mc Glynn insisted they go straight to the closest emergency room. Doctors found the tumor and rushed Zortman to the Hospital of the University of Pennsylvania in Philadelphia.

“That was the scariest part — seeing the ambulance door close and my fiancée crying,” he said.

Zortman met Dr. Ali, 34, who grew up on Staten Island after immigrating from India as an infant with her parents, both doctors. She attended PS 54, IS 72, Susan Wagner HS, where she was an Intel Science semi-finalist, and the University of Rochester medical school. She has three sons and a fourth on the way.

The operation began with Zortman asleep. Ali drilled into his skull to open a large flap and sliced through membranes covering his brain.

Then Zortman was awakened. He felt no discomfort because his scalp was anesthetized, and brain matter does not sense pain.

The surgical team had Zortman name objects shown on a laptop, like a clown, while Ali used electrical stimulation to mark spots on his brain that did not hinder his speech. “Do you feel up to singing a song for us?” Ali asked the patient.

“Sure!” Zortman said. He told himself, “You’ve got to really come through. You’ve got to give it your all.”

The operating room was spellbound.

With 10 doctors, nurses and aides as his audience, Zortman pumped out “Bust a Move” by Young MC, and Ed Sheeran’s romantic “Thinking out Loud.”

“It was a very peaceful and serene moment.” Ali recalled. “The circulating nurse was tearing up. It was so touching to witness.”

When Zortman finished, the OR team burst into applause.“I heard them laughing — like ‘you did a heckuva job,’’’ Zortman said.

But bad news followed a lab test on the tumor. Zortman was diagnosed with anaplastic astrocytoma, an aggressive cancer. It could still kill him in two to five years, but some patients are still alive after 20 years.

Amid a chemotherapy regime, he proposed to Annie on Christmas Eve, and they plan to marry in November.

Zortman, whose stage name is Zachary William, also wrote and recorded a song, “Meant to Live,” to encourage other cancer patients.

Posted on YouTube, the lyrics include: “Put everything into life, then it takes you by surprise, you got ten months they say. It will be alright, it will be okay,… just something that got in the way of a normal life you were meant to live; but you can still live.”