“Legs straight out or you’re getting tased,” the police officer can be heard saying on video, talking to an unarmed black man sitting on a curb.

The man appears to begin following the officer’s directions, then folds his legs up again. The officer fires his stun gun. The man writhes in pain, then is handcuffed by officers while lying on his stomach.

That encounter, captured by a bystander on a cellphone video Thursday morning in Lancaster, Pa., and then widely shared after being posted to Facebook, quickly led to a public outcry, including from the town’s mayor, who said an investigation would be conducted.

The video shows Officer Philip Bernot of the Lancaster City Police Bureau repeatedly asking the man, Sean D. Williams, 27, to straighten his legs as Mr. Williams is sitting on a curb.