As the sound of gunshots grew closer, Janderson Viera knew that the rival gang that had taken over his neighborhood was coming for him.

Running to his bedroom, he called the only lifeline he had left: the Rev Arnaldo Barros.

"I want to convert," he said.

As gang wars drive Brazil's homicide rate to historic highs, evangelical pastors - long revered in the nation's slums and prisons - have come up with a new way to protect members looking for a way out.

Gang leaders say the only way to leave the business alive is to convert to Christianity. So Barros, a televangelist popular here in western Brazil, memorializes a gang member's embrace of the ancient articles of faith using the most modern of tools: He records the conversion on his smartphone and posts the videos on YouTube, Facebook and WhatsApp. The converts gain immunity against retribution by rival gangs and their own.

Gang leaders and law enforcement officials say it works.

"We aren't going to go against the will of God," a local leader of the powerful Comando Vermelho, the gang that was pursuing Viera, told The Washington Post. "God comes first, above everything."

"It's become a nonviolent escape route," agreed Lucas Gomes, the head of prisons here in Acre state. "A way to publicize, justify and explain the exit."

Former gang member Janderson Viera, 23, learned a rival gang planned to execute him. He called the Rev Arnaldo Barros.

Barros, meanwhile, keeps close watch on each new Christian to make sure the conversion sticks.

If it doesn't, he lets the gangs know.

Gang violence has made Brazil one of the most dangerous countries in Latin America - killings nationwide reached a record 64,000 in 2017, and the death toll remains high.

The carnage, and the sense that the government wasn't doing enough to stop it, helped the right-wing populist Jair Bolsonaro get elected last year. The former military officer campaigned on promises to loosen gun ownership laws for private citizens and to give police more authority to shoot suspects.

That pitch resonated in Acre, where Bolsonaro won 77 percent of the vote, more than in any other state. The sparsely populated western state, wedged between Peru and Bolivia, is so often neglected by the federal government that Brazilians joke it doesn't exist. But for the narcotrafficking gangs battling for control of Brazil's profitable cocaine route, it has become hotly disputed turf.

The gang wars have transformed sleepy Rio Branco, a jungle-covered town of ramshackle houses and polluted canals, into one of Brazil's most violent cities. The homicide rate in Acre's capital rose to 64 per 100,000 in 2017, double that of the rest of the country.

Making converts has long been Barros' business. As the death toll mounted, so did the calls. But it was a challenge spreading the message that the new Christians were out of the game in time to save their lives.

"They come to me desperate for help," the 56-year-old pastor said. "This is the only exit, the only way out. I thought, 'How am I going to get the gang leaders to see this?' "

For years, Brazilian gangs have posted cellphone videos on social media to keep members in line, intimidate rivals and orchestrate attacks.

As the violence has intensified, the videos have become increasingly gruesome. In 2016, a gang posted footage of the live decapitation of two men from a rival gang. By 2018, members were extracting the hearts of their decapitated rivals and waving them in front of the camera.

Barros, pastor of Rio Branco's Igreja Geracao Eleita - the Elected Generation Church -saw these videos circulating on his feeds and decided to co-opt the approach. The social media savvy televangelist began to film gang members' conversions and post them online to declare that the new converts were off limits. Other pastors in Acre have followed his example.

Political scientist Bruno Paes Manso studies gangs and violence at the University of Sao Paulo.

"What's interesting is that the response here isn't coming from the universities, from intellectuals or from the state," he said. "It's coming from the people who are living with these problems, who had to react and came up with this solution."

The Rev. Adilson de Oliveira says the church's endorsement of the videos adds a sense of legitimacy in a world of shifting alliances.

Oliveira, 60, spent nine years behind bars for armed robberies and drug dealing before he converted himself 20 years ago. Now he helps prison inmates who are looking for a different life.

"Sometimes people don't believe that a member is leaving," he said. "They think, 'He was our partner. We know what he is capable of.'

"But a pastor is someone they can trust. The video says, 'I'm leaving, but I want to stay on good terms.'"

The videos show burly men such as Dianne Farias looking tearfully into the camera and making their confessions.

Standing before bullet-pocked buildings - and often bullet-pocked themselves - they state their names, code names and ID numbers within their gangs. They list their crimes, the number of people they have killed and announce they are now men of God:

"My brothers, I have to think about my family."

"I don't want this life for me."

"I have kids to raise."

Barros places a hand on their shoulders and pronounces them free: "In the name of Jesus, you are officially unaffiliated."

Barros estimates he has saved 500 men through social media conversions since he began posting them in 2014. In the past year, the pace of killing in Acre has slowed. Violent deaths in the state fell 22 percent from 2017 to 2018, government figures show.

While the conversion videos appear to protect the converts, it's unclear how much impact they've had on the larger homicide rate. Acre's state government has strengthened its police, invested in new technology and increased prison inspections.

To nonbelievers, the video conversions might smack of compulsory spirituality. But for those who fear death at the hands of rival gangs, the choice is clear. Inside prisons, where Barros and other pastors film weekly, converts are stacking up.

Gomes, the prison official, said "the result is visible."

To keep religious inmates at the Francisco d'Oliveira Conde prison safe during unrest, guards confined them to a separate cell. But they're converting at such a pace that the state is now constructing a new building to house them all.

Of course, conversion by video is no guarantee that a gang member will stay straight. But converts who return to their gangs face serious danger.

Lucas Cunha, 18, was at work when a rival gang broke into his house. Worried they would eventually find and kill him, he called Barros and asked him to record a video.

Former gang member Lucas Cunha, 18, converted after a rival group invaded his house and tried to kill him.

When his attackers saw it, they dropped their pursuit. But they monitored him for months, checking to see if he was going to church or had contact with his former leaders.

"If I do anything wrong, they will kill me," Cunha said. "I have to take the video seriously. They don't tolerate regressions."

Viera, 23, managed to escape both Comando Vermelho - the Red Command - and his own gang, Bonde dos 13. He's staying at a rehabilitation center run by Barros's church while looking for a place to live.

Barros says vouching for the converts can put him in danger. His home on the outskirts of Rio Branco is ringed by surveillance cameras. Barking dogs announce anyone who passes too closely to the gate.

He estimates that 5 percent of them slide back into crime. So he keeps a close watch on new converts. If he catches wind that one is back in the game, he informs gang leaders - to protect the integrity of the conversions.

Barros learned this month that Francisco Marinaldo, a former member of Comando Vermelho, had resumed using drugs. He tried to coax Marinaldo back to the church, he says, but was rebuffed.

Before Barros had a chance to let gang leaders know, he says, Marinaldo was stabbed to death.

On a recent afternoon in a shantytown outside Rio Branco, Marcos Adriano considered his options.

The 25-year-old had spent nearly half his life behind bars. He began selling marijuana when he was 7 and worked his way up to trafficking cocaine from Colombia to Brazil for Comando Vermelho. And then there were the killings - 22, he says, by the time he went to prison in 2008.

When he got out last year, he decided to convert in a video with Barros. Then he slowly got his life together. He found a job and tried to make new friends. But the $12 a day he made working at a local bakery paled in comparison to the $1,000 he said he sometimes brought home dealing drugs.

He says he went back to the drug trade a few months ago.

When Barros stopped by his house to check on him, Adriano admitted he had rejoined a gang. But now he was having second thoughts.

"I want to go back to the church," he said. "Too many people are dying. My chest isn't made of steel."

He pointed to his bullet wounds - fleshy craters bulging from his neck, abdomen and legs.

And so Adriano steadied himself in front of Barros' phone, looked into the camera and took a deep breath. For the second time, he announced his name, nickname and his gang.

But when Barros finished recording, Adriano stopped him.

"Don't send it just yet," he said. "I'll let you know when to post it."

(Except for the headline, this story has not been edited by NDTV staff and is published from a syndicated feed.)