GithubHelp home page GithubHelp logo

dragondave / pressurecooker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learningequality/pressurecooker

0.0 1.0 0.0 171 KB

A library of various media processing functions and utilities

License: MIT License

Makefile 0.53% Python 97.56% HTML 1.91%

pressurecooker's Introduction

pressurecooker

A library of various media processing functions and utilities

Setting up your environment

Converting caption files

The pressurecooker library contains utilities for converting caption files from a few various formats into the preferred VTT format. The currently supported formats include:

Within pressurecooker, the term "captions" and "subtitles" are used interchangeably. All of the classes and functions handling conversion use the "subtitles" term.

Language codes

The DFXP, SAMI, and TTML formats can encapsulate caption contents for multiple languages within one file. The SCC, SRT, and VTT formats are generally limited to a single language that isn't defined in the file (the VTT may be an exception to this rule, but our converters do not detect its language). Therefore when converting these files we cannot know what language we are working with and must instead use the constant LANGUAGE_CODE_UNKNOWN to extract the converted subtitles.

Note also that language codes used within the subtitle files might differ from the LE internal language codes defined in le-utils.

Creating a converter from a file

To create a subtitle converter from a local file path, use these commands:

from pressurecooker.subtitles import build_subtitle_converter_from_file

converter = build_subtitle_converter_from_file('/path/to/file.srt')

Creating a converter from a string

If you already have the captions loaded into a string variable, you can create the converter like so:

from pressurecooker.subtitles import build_subtitle_converter

captions_str = ''   # In this example, `captions_str` holds the caption contents
converter = build_subtitle_converter(captions_str)

Converting captions

For the SCC, SRT, and VTT subtitles format that do not contain language code info, you must refer to the language as the constant LANGUAGE_CODE_UNKNOWN at the time of extracting the converted subtitles:

from pressurecooker.subtitles import build_subtitle_converter_from_file
from pressurecooker.subtitles import LANGUAGE_CODE_UNKNOWN

converter = build_subtitle_converter_from_file('/path/to/file.srt')

# Option A: Obtain the contents of the converted VTT file as a string
output_str = converter.convert(LANGUAGE_CODE_UNKNOWN)

# Option B: Write the converted subtitles to a local path
converter.write("/path/to/file.vtt", LANGUAGE_CODE_UNKNOWN)

The LANGUAGE_CODE_UNKNOWN constant is the internal representation pycaption uses to denote subtitles in an unknown language code. This will be the default and only language code for SCC, SRT, and VTT subtitle converters.

If you are unsure of the format, but you know the language of the file, it is safer to conditionally replace the LANGUAGE_CODE_UNKNOWN with that language:

from pressurecooker.subtitles import build_subtitle_converter_from_file
from pressurecooker.subtitles import LANGUAGE_CODE_UNKNOWN

converter = build_subtitle_converter_from_file('/path/to/file')

# Replace unknown language code if present
if converter.has_language(LANGUAGE_CODE_UNKNOWN):
    converter.replace_unknown_language('en')
    
assert converter.has_language('en'), 'Must have English after replace'

output_str = converter.convert('en')

An example showing how to handle the subtitle formats like DFXP, SAMI, and TTML, which have multiple languages is shown below:

from pressurecooker.subtitles import build_subtitle_converter_from_file
from pressurecooker.subtitles import LANGUAGE_CODE_UNKNOWN, InvalidSubtitleLanguageError

converter = build_subtitle_converter_from_file('/path/to/file')

for lang_code in converter.get_language_codes():
    # `some_logic` would be your decisions on whether to use this language
    if some_logic(lang_code):
        converter.write("/path/to/file-{}.vtt".format(lang_code), lang_code)
    elif lang_code == LANGUAGE_CODE_UNKNOWN:
        raise InvalidSubtitleLanguageError('Unexpected unknown language')

pressurecooker's People

Contributors

ivanistheone avatar aronasorman avatar bjester avatar jayoshih avatar kollivier avatar elaeon avatar jamalex avatar

Watchers

James Cloos 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.