Giter Club home page Giter Club logo

swiftwebvtt's Introduction

Swift WebVTT

Overview

Swift WebVTT is a parser of WebVTT files that is based on a sample parser in the specification.

Originally written as part of Skipjack.

Functionality

Functionality is currently limited to extracting a list of cues with their timings and text content. Extracting regions, stylesheets etc. is not implemented. Text styling such as bold, italics, ruby etc. is also not implemented.

As part of parsing text content of cues, this project implements a custom parser for HTML Character Entities based on the HTML 5.1 specification.

Saving files in WebVTT format and showing cues on screen is considered outside of scope of this project.

Installation

Carthage

Put github "auramagi/SwiftWebVTT" in your Cartfile.

CocoaPods

Put pod 'SwiftWebVTT' in your Podfile.

Usage

// Assuming `fileContents` is a `String` with WebVTT file data
let parser = WebVTTParser(string: fileContents)
let webVTT = try? parser.parse()
// webVTT?.cues holds an array of cues

Making automatically-generated WebVTT from YouTube readable

YouTube has a different native format (SRV3) for captions, but their servers are able to convert to several different formats including WebVTT. However, for their ASR (automatic speech recognition) captions, YouTube bakes in a certain presentation that effectively duplicates cues.

This doesn't have a negative effect when presenting caption-by-caption, but makes content unreadable when extracting all text or just presenting all captions at once.

Swift WebVTT has a helper function to de-duplicate and reformat such files.

// var webVTT: WebVTT // A parsed file
let filteredCues = WebVTTParser.deduplicateCues(webVTT.cues)
webVTT = WebVTT(cues: filteredCues)

HTML Character References

The custom parser for HTML Character References can also be used standalone to decode text with HTML entities.

let text = "Hello, world! 👋"
print(text.decodingHTMLEntities())
// > "Hello, world! ๐Ÿ‘‹"

Performance

This implementation is reasonably fast and not memory-intensive. A ~1 MB file with 2h30m worth of captions loads in less than 1 second on an iPhone 7 Plus.

License

Swift WebVTT is available under the MIT license. See the LICENSE file for more info.

swiftwebvtt's People

Contributors

auramagi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.