The hammer-swinging madman charged with killing an Asian chef at a Brooklyn buffet told cops he was inspired by a movie about Chinese mistreatment of women, law enforcement sources said Wednesday.

A “rambling” Arthur Martunovich told detectives that after seeing the flick — the name of which wasn’t immediately known — he had no choice but to unleash terror inside the packed, Asian-owned Seaport Buffet early Tuesday evening, the sources said.

Hammer in hand, the 34-year-old construction worker allegedly burst into the Emmons Avenue eatery around 5:10 p.m. and bashed owner Kheong Ng-Thang over the head in the entryway, according to sources.

He then stormed into the kitchen, where he allegedly clocked manager Tsz Mat Pun and chef Fufai Pun — who are not related — before dashing out of the gore-soaked restaurant into the street, sources said.

As a horrified bystander screamed, “He did it! That’s the guy right there,” cops cuffed Martunovich without incident at the corner of Emmons and East 19th Street.

A bloody hammer was found sitting at the foot of a coat rack in the restaurant, which remained shuttered Wednesday as detectives scoured the crime scene for clues.

Back at the local station house, detectives quizzed the unhinged suspect for a motive behind the grisly, unprovoked attack — and got answers that only invited more questions.

“He’s been rambling,” said one law enforcement source. “He’s all over the place.”

At one point, Martunovich claimed he was acting out of chivalry by defending Chinese women, having recently seen an unspecified movie about their mistreatment by Chinese men, sources said.

“He had problems with Asian men,” one law enforcement source said. “He talked about how Asian women are being treated by their men.”

Fufai Pun, 34, was killed, while Tsz Mat Pun, 50, and Ng-Thang, 60, remain hospitalized in critical condition.

Martunovich — who a longtime acquaintance said emigrated from Estonia as a teen — also invoked the extraterrestrial in his meandering interrogation, sources said.

“He was saying psycho stuff about space aliens,” one high-ranking police source told The Post.

Whatever prompted Martuonvich’s break, it apparently came recently, as he had no known criminal history in New York before Tuesday, where he was charged with murder, attempted murder and criminal possession of a weapon, sources said.

He was suspected in a 2016 assault, sources said, but was never charged because the alleged victim declined to go forward with the case.

He had also been the victim in two city assaults, in 2004 and 2016, but details about those cases couldn’t immediately be provided.

Martunovich, wearing a hospital gown and a blank stare, was wheeled out of a Coney Island police station and into the back of an FDNY ambulance on Wednesday evening.

A former longtime pal of Martunovich said that when he heard from his friend a week or two ago for the first time since falling out of touch over a year ago, he didn’t sound himself.

“He was saying things that sounded off, sounded kind of crazy,” said Aleksandr Krupetskiy. “He was telling me about some personal stuff that I’d told him a couple months ago — but we hadn’t spoken a couple months ago.”