A Moroccan rapper who recorded a viral track denouncing the state of the country has been sentenced to a year in prison for insulting the police in a case that rights groups have called “an outrageous assault on free speech”.

Mohamed Mounir, who performs under the name Gnawi, was also fined the equivalent of $103 (£79) after confessing to cursing about the police in a video he posted online in late October. He can appeal the sentence.

Supporters argue Mounir was targeted for a blistering song he released a few days before his arrest titled Long Live the People, which sharply criticised the government and has been viewed more than 15m times.

“Tell me, will we really shut up about this humiliation?” begins the track, which references state torture, drug abuse and corruption by Morocco’s rulers and sardonically suggests poverty will be eliminated by 2020 – “because everyone will have left the country”.

The song spread quickly in a country where discontent is growing: 50% of all respondents in a recent survey said they wanted to see rapid political change, the highest rate in the Arab world.

Mounir, a former serviceman, told the court he had been drunk when he recorded the video criticising the police, and said officers had mistreated him when they stopped him at a checkpoint earlier that same evening.

Police say the charges are unrelated to the song, pointing out the other two rappers involved have not been arrested. “This trial has nothing to do with freedom of expression. This is a penal code matter,” police lawyer Abdelfattah Yatribi said in court.

Mounir’s lawyers have argued the performer should have been tried under a separate set of laws governing the press and publishing that do not allow imprisonment.

The track, by Mounir and two other rappers, crosses many of the “red lines” that govern speech in Morocco including by indirectly criticising the king and his advisers, one of whom is described as having “Jewish blood” – considered an insult in the Arab country.

Yahya Semlali, who recorded the song with Mounir under the name Lz3er, told the Associated Press: “We didn’t do this project to point fingers or create controversy. We voiced what the majority of Moroccans feel but fear to say. We said it all and it naturally upset those who do not want change.”

The government spokesman Hassan Abyaba said the track was out of step with Moroccan attitudes. “Songs of all kinds must respect the citizens, the constancy of the nation and the principles and values that are part of the Moroccans’ education,” he told a news conference last week.

Amnesty International has called the charges absurd and said Mounir should be freed immediately. “He is blatantly being punished for expressing his critical views of the police and the authorities,” said Heba Morayef, the organisation’s Middle East and north Africa director.

“While Gnawi may have used offensive language to refer to the police, the right to freely express one’s views, even if shocking or offensive, is protected by international human rights law. No one should face punishment for freely speaking their minds.”

Morocco’s king, Mohammed VI, was confronted with widespread protests during the 2011 Arab spring, and sought to compromise by writing a new constitution and holding early elections, which led to new parties winning seats in the country’s parliament.

But discontent has continued to boil, fuelled by rampant corruption and worsening economic conditions, and small protest movements have started again, this time spreading to regional areas as well as cities.

Analysts say the government has sought to keep a lid on unrest by increasingly cracking down on dissent and demonstrations in the past two years, including by arresting rappers who express popular grievances.

The 2011 protests gave prominence to another rapper, Mouad Belrhouate, who was jailed for two years, then sought political asylum in Belgium.

Belrhouate, who raps as El Haqed, or the defiant one, told AP: “Though I love my country very much, it suffocated me. I was always followed, watched. I felt in prison outside of prison, and yet I dream of the day I return to my neighbourhood, my little bunker in my neighbourhood in Casablanca.”