Bodycam footage shows officer saving man just seconds from being hit by oncoming train

The man, who appeared to be lying on the tracks, thanked the officer.

Body camera footage shows the harrowing moment when a New Jersey police officer saves a man who was on the train tracks just seconds before an oncoming train came speeding by.

The shaky video begins as Perth Amboy Police Officer Kyle Savoia runs alongside the tracks toward the man wearing a red shirt, who appears to be lying on the tracks.

As Savoia gets closer to the man, so does the train. The officer yells at the man in a panic, telling him to get off the tracks.

"Hey, bud! Hey, move!" he shouts before screaming for the train to stop.

Miraculously, the train stops before hitting the man, who then stood up and jumped to the side of the tracks. Savoia could be heard breathing a huge sigh of relief after the man scurried to safety.

The man then crosses the tracks, again, in front of the stopped train, to greet the officer.

"Sit down," Savoia told the man, who appeared to be distressed.

When Savoia asked him if he was OK, the man replied between heavy breaths, "Where'd you come from? Thank you. Oh my God. Thank you so much."

The incident occurred on Thursday morning, police said. Two other officers who were with Savoia notified the New Jersey Transit Police Department "immediately" upon receiving the call detailing the man on the tracks, which gave the train time to slow down and eventually stop, the Perth Amboy Police Department said.

It is unclear what the man was doing on the tracks.

The police department credited Savoia with saving the man, "who was just moments away from being struck by a moving train," it wrote on Facebook.

Savoia told NJ.com that police officers are required "to make quick decisions in difficult situations and act on them."

"Also, at such an early stage of my career, to be able to make an impactful difference in a person's life is incredible and makes me love what I do every day," Savoia told the local newspaper.

Savoia was just sworn into office in January, but his father, who died in 2016, was a Perth Amboy police officer for more than 28 years, according to NJ.com.

ABC News Gregory Croft contributed to this report.