Show subtitles with an AVFoundation AVPlayer on OS X
Asked Answered
L

2

16

I'm trying to display subtitles when playing video using AVFoundation on OS X.

I've looked through the documentation and I can't find a way to enable a subtitle track. The API contains multiple references to subtitle tracks, which leads me to believe that it's supported.

On iOS the method -[AVPlayerItem selectMediaOption:inMediaSelectionGroup:] is used to enable subtitle tracks. This method isn't available in the 10.7 SDK. Is there another way to show subtitles?

EDIT: Quicktime Player X has subtitle support, for example on opening this movie the subtitle submenu appears to offer a choice of language, and will display them when English is chosen. This leads me to believe that they're included in the API...

Leaved answered 3/7, 2012 at 13:35 Comment(6)
Did you try closedCaptionDisplayEnabled = YESMandle
@DustinRowland Thanks, I'd missed that. Just tried it, didn't change anything as far as I could see.Leaved
What worked for me was using AVAsset and AVPlayerItem, that gave me more fine control over my objects.Vatic
closedCaptionDisplayEnabled only applies to closed caption tracks. It should work on movies from iTunes and definitely works on videos exported from EyeTV. It has no effect on subtitle tracks.Iseult
@Leaved did you find a solution for this problem?Verticillate
@tommychheng no, I've not. We ended up using libVLC for other reasons, so I'm not looking any more! But the subtitle support there is better.Leaved
C
6

I ran into this same issue myself. I found that unfortunately the only way to do it, other than switching to QTKit, is to make a separate subtitles layer (a CATextLayer) and position it appropriately as a sublayer to the player layer. The idea is that you set up a periodic time observer to trigger every second or so and update the subtitles, along with (and this is optional) some UI element you might have that shows what the elapsed time is in the video.

I created a basic SubRip (.srt) file parser class; you can find it here: https://github.com/sstigler/SubRip-for-Mac . Be sure to check the wiki for documentation. The class is available under the terms of the BSD license.

Another challenge you might run into is how to dynamically adjust the height of the CATextLayer to account for varying lengths of subtitles, and varying widths of the containing view (if you choose to make it user-resizable). I found a great CALayoutManager subclass that does this, and made some revisions to it in order to get it to work for what I was trying to do: https://github.com/sstigler/height-for-width .

I hope this helps.

Cryosurgery answered 29/9, 2012 at 20:51 Comment(1)
Many thanks. Helpful answer, but I don't think it'll do the trick in my case. We're receiving the subtitles as part of a video stream or video file, we don't currently have access to an SRT file. Might come in handy if I figure out a way to extract one though?!Leaved
U
2

The way to add the subtitle file is by adding scc subtitle as AVMediaTypeClosedCaption track to the AVPlayer using AVMutableCompositionTrack and the player will control it I tried it and it worked or by adding a text file with the quick time text or any text as AVMediaTypeText track and the player will show the subtitle I don't know why this AVMediaTypeSubtitle track what subtitle file is supported for it

Uranology answered 14/1, 2016 at 7:10 Comment(3)
Please, it would be really helpful if you could share sample code. Thanks.Everyplace
Use sublerCLI you can convert srt or scc subtitles to subtitle track AVMediaTypeSubtitle and AVMediaTypeClosedCaption track inside a container m4v ================> var sublerTask = NSTask() sublerTask.launchPath = sublerPath // SublerCLI Path sublerTask.arguments = [ "-source",srtFilePath,"-dest",tempM4V,"-language",language,"-metadata", language] sublerTask.launch() sublerTask.waitUntilExit()Uranology
after that you can read the m4v as AVAsset and take the subtitle track from itUranology

© 2022 - 2024 — McMap. All rights reserved.