3

Question:

How do I add an external WebVTT file to my AVPlayer in tvOS?

Description:

I've been watching this "What's New in HTTP Live Streaming" by Apple where they talk about different ways in implementing an external WebVTT file.

The whole subtitle domain is quite new to me, so I'm having hard time grasping the concept quite well. Within the video they talk about many different things which I do not quite understand such as ( Subtitle playlist ). However, getting passed all that my main concern is adding .vtt file to my AVPlayer.

In the video they talk about this AVMediaSelectionGroup() but I'm quite confused on how to use and how to implement this with my AVPlayerViewController :

class PlayerViewController: AVPlayerViewController {

    override func viewDidLoad() {
        self.setupVideoPlayerView()
    }

    private func setupVideoPlayerView()
    {
        let path = "https://link.to.my.video.mp4"
        let subTitlePath = "https://link.to.my.webvtt.file.vtt"

        let nsURL = URL(string: path)

        let avPlayer = AVPlayer(url: nsURL!)

        self.player = avPlayer

        self.player!.seek(to: kCMTimeZero)
        self.player!.play()

    }
}

The AVMediaSelectionGroup doesn't seem to have any methods to add a subtitle? The closest thing I was able to find (that mentioned subtitles) are the following methods:

//Where self is the instance of AVPlayerViewController
self.allowedSubtitleOptionLanguages
self.requiresFullSubtitles
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131

3 Answers3

3

This works for me.......

let localVideoAsset = AVURLAsset(url: URL(string: url) ?? URL(string:"")!)

let videoPlusSubtitles = AVMutableComposition()

let videoTrack = videoPlusSubtitles.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)

do{

guard localVideoAsset.tracks.count > 0 else{
             // error msg
                return
            }

            try? videoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: localVideoAsset.duration),
                                             of: localVideoAsset.tracks(withMediaType: .video)[0],
                                             at: CMTime.zero)
}

let subtitleURL = URL(fileURLWithPath: model.data?[self.selected].subtitlePath ?? "")
            let subtitleAsset = AVURLAsset(url: subtitleURL)

            let subtitleTrack = videoPlusSubtitles.addMutableTrack(withMediaType: .text, preferredTrackID: kCMPersistentTrackID_Invalid)
            do{
                guard subtitleAsset.tracks.count > 0 else{
                    //error msg
                    return
                }
                try? subtitleTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: localVideoAsset.duration),
                                                    of: subtitleAsset.tracks(withMediaType: .text)[0],
                                                    at: CMTime.zero)
            }
        let playerViewController = AVPlayerViewController()
        let player = AVPlayer(playerItem: AVPlayerItem(asset: videoPlusSubtitles))
        playerViewController?.player = player

        self.present(playerViewController ?? UIViewController(), animated: true) {
            self.videoPlaying = true
            self.playerViewController?.player?.play()
        }
    }
Rahul Gusain
  • 269
  • 3
  • 6
  • This is for a local video! how to add subtitles for a streaming video? – Rajesh Aug 08 '19 at 08:11
  • @Rajesh Did you got a solution for embedding subtitles for streaming videos ? – Pallav Trivedi Sep 17 '19 at 05:56
  • @PallavTrivedi I didn't find a way to make this work, Kemicofa's answer seems legit. Although I found another way to display the subtitles using an UILabel extension that I found online https://github.com/mhergon/AVPlayerViewController-Subtitles – Rajesh Sep 20 '19 at 09:13
2

While it was almost mentioned no where in the apple documentation, I read in an article that subtitles need to be embedded in the HLS stream.

Subtitles are not intended to be added manually.. though I did find a StackOverflow post showing a hack. Unsure if it works or not.

Seeing that I use VIMEO Pro, the HLS stream that is provided has the WebVTT subtitles (that I uploaded to vimeo) embedded, thus solving my problem.

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
-1

Okay, I think I solve this issue. I was looking for some solutions and I haven't found, so I implemented one to work. I made available a SPM with the solution and it is quite simple to use. So I called it SimpleSubtitles. It does support WebVTT, without Styles, and could be improved to support other formats

More info: https://github.com/peantunes/SimpleSubtitles

Pedro Antunes
  • 192
  • 1
  • 6