Editor's note: Some viewers may find the embedded video of the collision shocking.

A sheriff's deputy was able to pull a driver from a car moments before it was hit by a train in Northern California, officials said.

Commuter train tracks were shut down in both directions in Sunnyvale after two separate trains struck a vehicle at the Mary Avenue crossing, Caltrain tweeted Monday evening.

Video posted on YouTube shows a deputy pulling a driver out of a car stuck on the tracks. The pair is seen running away from the vehicle as a train can be heard fast approaching the area.

The driver then stumbles to the ground and the deputy is seen dragging him out of harm's way moments before the car is hit by a train.

"If it had not been for the deputies being at the right place at the right time, the driver would have been significantly injured," San Mateo County Sheriff's spokesman Salvador Zuno said.

Zuno said deputies Lance Whitted and Erik Rueppel were conducting enforcement in the area when they heard what sounded like a traffic collision. The deputies, who were just a few cars away, saw a vehicle had collided into the crossing guard.

Big shout out to @SMCSheriff deputies on scene who were able to pull the driver from the vehicle, preventing a possible tragedy. #thankyou — Caltrain (@Caltrain_News) August 4, 2015

Meet Deputies Whitted & Rueppel. They pulled a man to safety and saved a life. #heroes #thank you @SMCSheriff pic.twitter.com/k0L9mtVzuV — Caltrain (@Caltrain_News) August 4, 2015

Whitted and Rueppel ran to the collision and found major front-end damage to the vehicle and that a train was approaching the area. Zuno said one of the deputies ran on the tracks to notify the incoming train, while the other deputy removed the driver from the vehicle.

"I believe because of their actions this person is alive today." Zuno said.

The driver appeared to be under the influence, according to Zuno, who said the department was still investigating the incident.

Trains were temporarily forced to share the northbound tracks. Northbound train No. 385 and southbound train No. 274 hit the vehicle, Caltrain tweeted.