Easy way to show SRT files on AVPlayerViewController

Overview

Logo

issuesstarslicense

AVPlayerViewController-Subtitles is a library to display subtitles on iOS. It's built as a Swift extension and it's very easy to integrate.

How To Get Started

Installation with CocoaPods

platform :ios, '8.0'
pod "AVPlayerViewController-Subtitles"

Manually installation

Download (right-click) and add to your project.

Requirements

Version Language Minimum iOS Target
1.2.x Swift 3.0 iOS 8
1.0.x Swift 2.x iOS 8

Usage with player

import AVPlayerViewControllerSubtitles
// Video file
let videoFile = Bundle.main.path(forResource: "trailer_720p", ofType: "mov")

// Subtitle file
let subtitleFile = Bundle.main.path(forResource: "trailer_720p", ofType: "srt")
let subtitleURL = URL(fileURLWithPath: subtitleFile!)

// Movie player
let moviePlayer = AVPlayerViewController()
moviePlayer.player = AVPlayer(url: URL(fileURLWithPath: videoFile!))
present(moviePlayer, animated: true, completion: nil)

// Add subtitles
moviePlayer.addSubtitles().open(file: subtitleURL)
moviePlayer.addSubtitles().open(file: subtitleURL, encoding: String.Encoding.utf8)

// Change text properties
moviePlayer.subtitleLabel?.textColor = UIColor.red

// Play
moviePlayer.player?.play()

Screenshot

Screenshoot

Usage without player

From version 1.2 you can search text in the SubRip file or text without need play any file.

import AVPlayerViewControllerSubtitles
// Subtitle file
let subtitleFile = Bundle.main.path(forResource: "trailer_720p", ofType: "srt")
let subtitleURL = URL(fileURLWithPath: subtitleFile!)

// Subtitle parser
let parser = Subtitles(file: subtitleURL, encoding: .utf8)

// Do something with result
let subtitles = parser.searchSubtitles(at: 2.0) // Search subtitle at 2.0 seconds

Contact

License

Licensed under Apache License v2.0.
Copyright 2017 Marc Hervera.

Comments
  • Support subtitles & cc on/off from the default AVPlayerViewController button

    Support subtitles & cc on/off from the default AVPlayerViewController button

    Would be great to have support for on/off subtitles & cc from the default AVPlayerViewController UI.

    I'll give a try to implement it, hope is possbile.

    enhancement 
    opened by MihaiPantiru 3
  • adding support to add Dictionary content to subtitles

    adding support to add Dictionary content to subtitles

    added function showByDictionary that has input NSMutableDictionary to bypass the parsing method srt.

    i use this parsing XML from youtube like this:

    let subtitlesDictionary = NSMutableDictionary()
    if CaptionUrl != nil {
        Alamofire.request(CaptionUrl!).response {
            response in
    
                let xml = SWXMLHash.parse(response.data!)
    
            var counter = 0
            for elem in xml["transcript"]["text"].all {
                let fromTime = Double((elem.element ? .attribute(by: "start") ? .text) !)
                let toTime = fromTime!+Double((elem.element ? .attribute(by: "dur") ? .text) !) !
                let text = elem.element ? .text
    
                // Create final object
                let final = NSMutableDictionary()
                final["from"] = fromTime
                final["to"] = toTime
                final["text"] = replacedText
                subtitlesDictionary[counter] = final
                counter += 1
            }
        }
    }
    
    
    // Add subtitles
    playerViewController?.addSubtitles().showByDictionary(dictionaryContent: subtitlesDictionary)
    
    opened by alexookah 3
  • subtitleLabel.backgroundColor layout problem

    subtitleLabel.backgroundColor layout problem

    ` // Change text properties

    moviePlayer.subtitleLabel?.textColor = UIColor.red moviePlayer.subtitleLabel?.backgroundColor = UIColor.black `

    when using backgroundColor there is some problem when the subtitle is not active. Any idea why this is happening?

    simulator screen shot 30 jun 2017 00 39 31 simulator screen shot 30 jun 2017 00 39 38

    opened by alexookah 3
  • WebVTT subtitles

    WebVTT subtitles

    Hi , just wondering if you guys support WEBVTT subtitles as I have several videos with VTT subtitles (see attachments ) but they won't show up.

    Appreciate your help :)

    sample_video_subtitles.vtt.zip

    enhancement 
    opened by MohgaNabil 3
  • func parseSubRip hava bug

    func parseSubRip hava bug

    I found the func parseSubRip let regexStr = "(?m)(^[0-9]+)([\s\S]*?)(?=\n\n)" let regex = try NSRegularExpression(pattern: regexStr, options: .caseInsensitive) let matches = regex.matches(in: payload, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, payload.characters.count))

    the matches will lost last lines text

    defaul srt have 6 lines

    but only match 5 lines

    how to fix this bug?

    opened by g001613001 3
  • Problem in iOS 11.0 and 11.1

    Problem in iOS 11.0 and 11.1

    I'm facing a problem with the iOS 11.0 and 11.1. In both cases the system totally avoid the subtitleLabel constraints, it's like none of them were been implemented. Have you guys ever faced this issue?

    opened by Cris-Burlamaqui 2
  • add NSDictionary subtitles support

    add NSDictionary subtitles support

    I am using this library to get the captions from youtube url while parsing the XML caption youtube provides. Creating a NSdictionary and using the NSDictionary in the library.

    Should i make a pull request for this to be able to use a NSDictionary ?

    enhancement 
    opened by alexookah 2
  • Remote subtitle

    Remote subtitle

    Please help!!!

    I dont want to download subtitle file to bundle and I want to get subtitle file from server (https://...)

    Please help me with other way

    Thank sir!!!

    let subtitleFile = Bundle.main.path(forResource: "1", ofType: "srt")
    subtitleURL = URL(fileURLWithPath: subtitleFile!)
    parser = Subtitles(file: subtitleURL!, encoding: .utf8)
    
    opened by leminhtuan2015 2
  • Fix regex due to couple timestamp standards

    Fix regex due to couple timestamp standards

    Hello, Thank you for this repo, very helpful. But I've found an issue with timestamps and your current regex. In some cases there is some differences: 00:00:00.935 --> 00:00:02.668 00:00:00,935 --> 00:00:02,668

    opened by MaksymBilenko 2
  • Crash when using custom player view

    Crash when using custom player view

    Hey! First of all, thank you for the extension, it is really simple and adds a lot of value.

    This is more of an improvement than an issue actually, but happens when you are using a custom view for the player controller that the extension may crash depending on your implementation.

    One common scenario on this is when you try to keep playing videos while in background and you pick one specific solution of the two provided by Apple here

    In this scenarios, the extension may crash since it uses the contentOverlayView in order to add the subtitleLabel. If the contentOverlayView is nil, the app will crash trying to figure out the superview when creating constraints with the visual format specified.

    Based on this, I changed the following

        fileprivate func addSubtitleLabel() {
            
            guard let _ = subtitleLabel else {
                
               // ... label setup
                contentOverlayView?.addSubview(subtitleLabel!)
                
                // ... continue constraints
        }
    

    with something a little bit more defensive like:

        fileprivate func addSubtitleLabel() {
            
            guard let _ = subtitleLabel else {
                
               // ... label setup
               if contentOverlayView != nil {
                    contentOverlayView?.addSubview(subtitleLabel!)
                } else {
                    view.addSubview(subtitleLabel!)
                }            
    
                // ... continue constraints
        }
    

    Hope this helps

    opened by clackmac 2
  • scanner deprecated since iOS 13

    scanner deprecated since iOS 13

    'scanDouble' was deprecated in iOS 13 scanString(_:into:)

      scanner.scanDouble(&h)
       scanner.scanString(":", into: nil)
       scanner.scanDouble(&m)
       scanner.scanString(":", into: nil)
       scanner.scanDouble(&s)
       scanner.scanString(",", into: nil)
         scanner.scanDouble(&c)
    

    fixing this to doesn't show subtitles

    opened by rkorat1 1
Releases(v1.3.1)
Owner
Marc Hervera
 iOS, macOS, tvOS, watchOS, Vapor developer, instructor and more... 
Marc Hervera
A lightweight app to play videos from the Files app in a better (dark) interface which avoids losing your playback position.

Playerly Playerly is a very lightweight Swift app that allows you to select a file (video or movie) from the built in Document Browser, and play it in

Julian Schiavo 28 Dec 3, 2022
A Swift library to upload video files to api.video platform.

api.video IOS video uploader api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and manag

api.video 7 Dec 9, 2022
This project is a clone of YouTube. But the main intention is to show how to write clean code, using proper MVC patterns and re-usable coding methodologies!

YouTubeClone This project is a clone of YouTube. But the main intention is to show how to write clean code, using proper MVC patterns and re-usable co

Vamshi Krishna 169 Dec 10, 2022
▶️ video player in Swift, simple way to play and stream media on iOS/tvOS

Player Player is a simple iOS video player library written in Swift. Looking for an obj-c video player? Check out PBJVideoPlayer (obj-c). Looking for

patrick piemonte 2k Jan 2, 2023
The best way to watch Put.io on iPhone and iPad

The best way to watch Put.io on iPhone and iPad

Fetch 89 Oct 27, 2022
This Google Cast demo app shows how to cast videos from an iOS device in a way that is fully compliant with the Cast Design Checklist.

CastVideos-ios (reference iOS sender app) This Google Cast demo app shows how to cast videos from an iOS device in a way that is fully compliant with

Google Cast 168 Jan 6, 2023
A video composition framework build on top of AVFoundation. It's simple to use and easy to extend.

A high-level video composition framework build on top of AVFoundation. It's simple to use and easy to extend. Use it and make life easier if you are implementing video composition feature.

VideoFlint 1.4k Dec 25, 2022
A high-performance, flexible, and easy-to-use Video compressor library written by Swift.

FYVideoCompressor A high-performance, flexible and easy to use Video compressor library written by Swift. Using hardware-accelerator APIs in AVFoundat

null 30 Dec 30, 2022
YHPlayer - An easy-to-use video player based on swift language

YHPlayer An easy-to-use video player based on swift language Features Plays loca

null 9 Dec 1, 2022
A SwiftUI framework which makes it easy to integrate Video Call and Chat within a few lines of code.

Welcome to iStream! This SwiftUI Framework allows you to add Video Call and Chat to your project within a few lines of code. To use this Framework, yo

null 2 Aug 19, 2022
Easy way to show SRT files on MPMoviePlayerController

    MPMoviePlayerController-Subtitles is a library to display subtitles on iOS. It's built as a Swift extension and it's very easy to integrate. How T

Marc Hervera 186 May 27, 2022
Package for creating, modifying, and managing subtitle files, such as SubRip (.srt).

SubtitleKit Package for creating, modifying, and managing subtitle files, such as SubRip (.srt). Supported formats Format File extension Is supported

GGorAA 2 Oct 28, 2022
Easy and beautiful way for a user to pick content, files or images. Written in Objective C

##JVTImageFilePicker Description ImageFilesPicker Is a butifuly designed UICompenent for uploading content Preview from recent camera roll Live Camera

Matan Abravanel 60 Jun 19, 2022
Localization/I18n: Incrementally update/translate your Strings files from .swift, .h, .m(m), .storyboard or .xib files.

Installation • Configuration • Usage • Build Script • Donation • Migration Guides • Issues • Contributing • License BartyCrouch BartyCrouch incrementa

Flinesoft 1.3k Jan 1, 2023
Mahmoud-Abdelwahab 5 Nov 23, 2022
Show the confetti only when the user is having fun, and if not having fun, don't show it.

SPConfetti - A simple solution to show the confetti to the user. Smoothly starts and stops. Allow set multiply diffrent particles at once. You can chang

Ivan Vorobei 225 Dec 30, 2022
A Swift sample code to reads ISO 10303-21 exchange structures (STEP P21 files for AP242) split into multiple files using external references approach.

multipleP21ReadsSample A Swift sample code to reads ISO 10303-21 exchange structures (STEP P21 files for AP242) split into multiple files using extern

Tsutomu Yoshida 1 Nov 23, 2021
Classes-and-structures-in-swift - This source files show what is the difference between class and structure

This source files show what is the difference between class and structure You ca

null 0 Jan 4, 2022
BeatboxiOS - A sample implementation for merging multiple video files and/or image files using AVFoundation

MergeVideos This is a sample implementation for merging multiple video files and

null 3 Oct 24, 2022
Shawn Frank 2 Aug 31, 2022