How to add external .vtt subtitle file to AVPlayerViewController in tvOS

天大地大妈咪最大 提交于 2020-12-04 08:55:34

问题


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

回答1:


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.




回答2:


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()
        }
    }


来源:https://stackoverflow.com/questions/39589710/how-to-add-external-vtt-subtitle-file-to-avplayerviewcontroller-in-tvos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!