2

我们已经在 Azure 媒体播放器中成功配置了字幕/字幕,它在 Web 端播放媒体。

但是,我们如何为 Native AVPlayer 在 iOS 中播放由 AMS 管理的媒体进行相同的配置呢?我们知道字幕/字幕可以通过 Sidecar WebVTT 文件在原生 iOS 播放器中播放,但是 AMS 生成的“transcript.vtt”文件是 Sidecar WebVTT 文件吗?

如果没有,我们如何生成 sidecar WebVTT 文件?

我们已经实现了如下代码,从 AMS 链接访问媒体文件和本地下载的 transcript.vtt 文件,但它失败了。

[编辑:20200413]

但是,当我们有本地媒体文件和本地 transcript.vtt 文件时,或者当我们直接访问媒体存储帐户(https://mediastorageaccount.blob.core.windows.net/container/file.mp4)中的媒体文件时工作正常。但是,当我们从 AMS Transform 生成的链接访问编码文件时(https://mediaservice-inct.streaming.media.azure.net/788888-6666-4444-aaaa-823422j218/file.ism/manifest(format= m3u8-cmaf) ) 它失败了。

这里有什么问题?

func playVideo()

{ 让 strUrl = " https://mediaservice-inct.streaming.media.azure.net/79510-6eb-340-a90-824218/German-FAST_Lesson-2-Dialog.ism/manifest(format=m3u8-cmaf) "

    localVideoAsset = AVURLAsset(url: URL(string: strUrl)!)

    //We have to add tracks in AVMutableComposition same like bellow

    //First we have to add video track on 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: seconds)
    }


    //After that we have to add subtitle track in AVMutableComposition
    if isEnglishSubtitle {
        setSubtitleTrack(subtitle: "transcript")

    }else{
        setSubtitleTrack(subtitle: "transcript_tr")
    }



    //After set the video track and subtitle track we have to set in the player same like bellow
    player = AVPlayer(playerItem: AVPlayerItem(asset: videoPlusSubtitles))

    playerLayer.removeFromSuperlayer()
    playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.videoView.bounds
    playerLayer.videoGravity = .resizeAspect
    self.videoView.layer.addSublayer(playerLayer)

    player.play()

}

func setSubtitleTrack(subtitle : String){ print(subtitle) print(seconds)

    //Here we have to check if any pre track available. If available then we have to remove it same like bellow
    if subtitleTrack != nil{
        videoPlusSubtitles.removeTrack(subtitleTrack!)
    }


    //We have to get subtitle file from path same like bellow
    let subtitleAsset = AVURLAsset(url: Bundle.main.url(forResource: subtitle, withExtension: ".vtt")!)

    // And we have to add new track from here
    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: seconds)
    }

}
4

1 回答 1

0

我怀疑这个问题不是由 AMS 流引起的。要仔细检查,您可能想尝试使用另一个流 HLS(例如,尝试 Apple 提供的 HLS)。Apple 对在 AVPlayer 中播放 VTT 有特定要求。我包含了一个 Apple 文档链接,其中包含很多关于流媒体的示例,以及其他可能有用的链接:

https://developer.apple.com/streaming/examples/

如何在 tvOS 中将外部 .vtt 字幕文件添加到 AVPlayerViewController

AVUrlAsset 和 WebVTT

于 2020-03-26T20:18:25.343 回答