Image copyright Caroline Records

Rapper XXXTentacion filmed a music video for Sad! in which he attended his own funeral, just weeks before he was shot dead in Florida.

The posthumously-released video sees the musician fight his resurrected dead body, before "killing" his old self.

"You have done well at battling yourself," reads a caption at the end, reflecting the rapper's struggle to leave behind a life of violence.

The video was released on Thursday, the day his private funeral took place.

The 20-year-old was facing a series of domestic violence charges when he died.

Before his murder, his accuser Geneva Ayala gave an interview to the Miami New Times in which she detailed a shocking pattern of abuse - including strangulation, imprisonment and threats to cut out her tongue.

In a handwritten note at the end of the video for Sad!, XXXTentacion appeared to reference the accusations.

"People change. Things change. And so did I," it says.

"As long as the outcome is good for myself and family, I'm not complaining. Oh! And I want her love all to myself. Loyalty."

Image copyright Caroline Records

Throughout the clip, XXXTentacion is confronted by a hooded figure - presumably the grim reaper - who challenges him to fight his demons.

"Your worst enemy is yourself," it tells the rapper, who listens with his head bowed.

"Humans must learn to love and compromise rather than destroy."

A title card for the video said it was "written and creative directed" by XXXTentacion, whose real name was Jahseh Onfroy.

Private funeral

The song, whose lyrics depict the musician's battle with depression, rose to the top of the Billboard charts following his murder last week.

Among those attending Thursday's funeral was soul star Erykah Badu, who posted a photo of the order of service on Instagram.

"LONG LIVE XXX," she wrote in the caption. "They may try to erase your face But millions spring up in your place.

"May your mother, family, child's mother, and true fans find moments of peace."

Fans left their own tributes under the video for Sad!, which has been played 26 million times since it was uploaded.

"People say he was corrupting youth but he was the escape the youth needed from corruption," wrote one.

"Rest in paradise," added a user called Toastie. "You helped me so much, you made me understand that I'm not alone with my depression."

The nature of the video led some fans to speculate his death was a stunt. "Who else has a really strong feeling that he's still alive?﻿" read a typical comment.

However, a public open-casket memorial service was held on Wednesday.

One man has been arrested over the star's murder. Police have said the rapper's last movements were tracked before he became the victim of an armed robbery.

Follow us on Facebook, on Twitter @BBCNewsEnts, or on Instagram at bbcnewsents. If you have a story suggestion email entertainment.news@bbc.co.uk.