Manipulate subtitles in GO (.srt, .ssa/.ass, .stl, .ttml, .vtt (webvtt), teletext, etc.)

Overview

GoReportCard GoDoc Travis Coveralls

This is a Golang library to manipulate subtitles.

It allows you to manipulate srt, stl, ttml, ssa/ass, webvtt and teletext files for now.

Available operations are parsing, writing, syncing, fragmenting, unfragmenting, merging and optimizing.

Installation

To install the library and command line program, use the following:

go get -u github.com/asticode/go-astisub/...

Using the library in your code

WARNING: the code below doesn't handle errors for readibility purposes. However you SHOULD!

// Open subtitles
s1, _ := astisub.OpenFile("/path/to/example.ttml")
s2, _ := astisub.ReadFromSRT(bytes.NewReader([]byte("00:01:00.000 --> 00:02:00.000\nCredits")))

// Add a duration to every subtitles (syncing)
s1.Add(-2*time.Second)

// Fragment the subtitles
s1.Fragment(2*time.Second)

// Merge subtitles
s1.Merge(s2)

// Optimize subtitles
s1.Optimize()

// Unfragment the subtitles
s1.Unfragment()

// Write subtitles
s1.Write("/path/to/example.srt")
var buf = &bytes.Buffer{}
s2.WriteToTTML(buf)

Using the CLI

If astisub has been installed properly you can:

  • convert any type of subtitle to any other type of subtitle:

      astisub convert -i example.srt -o example.ttml
    
  • fragment any type of subtitle:

      astisub fragment -i example.srt -f 2s -o example.out.srt
    
  • merge any type of subtitle into any other type of subtitle:

      astisub merge -i example.srt -i example.ttml -o example.out.srt
    
  • optimize any type of subtitle:

      astisub optimize -i example.srt -o example.out.srt
    
  • unfragment any type of subtitle:

      astisub unfragment -i example.srt -o example.out.srt
    
  • sync any type of subtitle:

      astisub sync -i example.srt -s "-2s" -o example.out.srt
    

Features and roadmap

  • parsing
  • writing
  • syncing
  • fragmenting/unfragmenting
  • merging
  • ordering
  • optimizing
  • .srt
  • .ttml
  • .vtt
  • .stl
  • .ssa/.ass
  • .teletext
  • .smi
Issues
  • Support reading of non utf-8 ttml

    Support reading of non utf-8 ttml

    Not sure if you would be interested in this change? I had to deal with some pesky TTML files that were created on windows and were encoded in utf-16le charset (ironically not valid TTML). This was semi annoying to resolve. The problem is explained in here https://groups.google.com/forum/#!topic/golang-nuts/tXcECEKC2rs

    The problem lies with encoding/xml's design: in order to use the charset reader the xml library needs to examine the first line of text from the xml file (where the encoding is specified). unfortunately that first line contains invalid UTF-8 already, and libxml barfs before it even figures out what the encoding should be to pass it to our charset reader.

    This change should allow passing in utf-16 or utf-8 encoded files into the reader.

    opened by saintberry 11
  • add colors to webvtt output

    add colors to webvtt output

    I looked at a TTML to WebVTT converted file from https://transcribefiles.net/other/pages/caption-subtitle-converter.htm, they used the font attribute, so here it is ... Not sure if it's OK to rely on TTMLColor but at least it has the proper value (for now).

    opened by dlecorfec 8
  • TTML ticks higher than 15 minutes display invalid values

    TTML ticks higher than 15 minutes display invalid values

    Tick values higher than a certain amount will result in incorrect times.

    For instance, take this test TTML:

    <?xml version="1.0" encoding="UTF-8"?>
    <tt xmlns="http://www.w3.org/ns/ttml" xmlns:tt="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:tts="http://www.w3.org/ns/ttml#styling" ttp:tickRate="10000000" ttp:version="2" xml:lang="ja">
     <head>
      <styling>
       <initial tts:backgroundColor="transparent" tts:color="white" tts:fontSize="6.000vh"/>
       <style xml:id="style0" tts:textAlign="center"/>
       <style xml:id="style1" tts:textAlign="start"/>
       <style xml:id="style2" tts:ruby="container" tts:rubyPosition="auto"/>
       <style xml:id="style3" tts:ruby="base"/>
       <style xml:id="style4" tts:ruby="text"/>
       <style xml:id="style5" tts:ruby="text"/>
      </styling>
      <layout>
       <region xml:id="region0" tts:displayAlign="after"/>
      </layout>
      </head>
     <body xml:space="preserve">
      <div>
       <p xml:id="subtitle1" begin="18637368750t" end="18676157500t" region="region0" style="style0"><span style="style1">テソプ<span style="style2"><span style="style3">の所だ</span><span style="style4">カン食ン</span></span>食おう<br/><span style="style2"><span style="style3">江陵</span><span style="style5">カンヌン</span></span>で刺身でも食おう</span></p>
      </div>
     </body>
    </tt>
    

    The resulting ASS is:

    [Script Info]
    
    [V4 Styles]
    Format: Name
    Style: italic
    Style: span
    
    [Events]
    Format: Start, End, Text
    Dialogue: 00:0-8:0-44..48,00:0-8:0-43..02,TEST
    

    Probably something to do with ticks not being int64 but normal integers, not sure though.

    bug 
    opened by rrooij 8
  • I couldn't manage to find

    I couldn't manage to find "Item" components when reading SRT.

    I wanted to get the ID number of a specific item of an SRT file; but it brings nil.

    Tried this code: s1, _ := astisub.OpenFile("example-in.srt")

    fmt.Println(s1.Items[0].Region)

    bug 
    opened by MerNat 7
  • changed stl default chars and rows value to comply with standard

    changed stl default chars and rows value to comply with standard

    Hi,

    I need to have width and height datas formated to standardized values regarding maximum number of characters per line and maximum number of rows in stl file metadatas.

    Currently they are :

    maximumNumberOfDisplayableCharactersInAnyTextRow: 40, maximumNumberOfDisplayableRows: 23,

    which is way too much especially for Rows

    I changed it to

    maximumNumberOfDisplayableCharactersInAnyTextRow: 37, maximumNumberOfDisplayableRows: 2,

    Ideally, it would be nice to be able to edit theses values through flags when converting subtitle.

    As an information on standardized values, please go to this link https://tech.ebu.ch/docs/tech/tech3360.pdf

    and page 17 under "1.4 conversion strategies" second paragraphe which explain 👍

    Specifically the Teletext format has a line length limit of 40 characters, which includes the control characters to select colours and to activate background display for normal pages (‘Start box’ codes). Teletext pages marked as ‘sub-title’ or newsflash pages within a Teletext service should be displayed as ‘boxed text’ by a receiver.16 The control code overhead required for boxed text has resulted in a ‘common practise’ of using a maximum of 37 (or 36) characters in a subtitle row for text.

    and a number of row set to 2 seems to be the most used number of maximum lines

    thanks a lot

    Best regards

    Fabien

    opened by flenoir 7
  • ttml to vtt region settings migration

    ttml to vtt region settings migration

    1. WEBVTT: Nil pointer exception when the region doesn't have parent style.
    2. TTML to VTT region settings migrated.
    3. WebVTT read cue settings from parent style if no setting present inline
    4. TTML to VTT cue position settings migrated.
    opened by discovery-avishekgulshan 7
  • webvtt,srt: Tolerate edge whitespace (and print line numbers on error)

    webvtt,srt: Tolerate edge whitespace (and print line numbers on error)

    Hi! I'm by no means an expert of WebVTT format but this fixed a parsing problem for me. Feel free to reject if it's a bad change, or improve it or whatever you'd like.

    PS. It'd be really helpful to output the line number when an error is encountered. Would you be open to a PR for that?

    opened by mholt 5
  • Cheetah CAP Files?

    Cheetah CAP Files?

    Hello there- I apologize if this is the wrong venue for a simple question.

    Do you plan to support CAP files in this library in the near future?

    Thanks.

    question 
    opened by davidkotas 5
  • TTML: Fixed improper spacing between captions

    TTML: Fixed improper spacing between captions

    1. Fixed improper spacing between captions while converting TTML -> VTT
    2. Converted TTML style attributes to pointers
    3. Fixed Webvtt out not using parent style attributes in the "region" section
    opened by discovery-avishekgulshan 5
  • Add support to extract voicename

    Add support to extract voicename

    Hi, I added the support to extract the speaker name from the vtt text.

    	1
    	00:02:34.00 --> 00:02:35.00
    	<v.first.local Roger Bingham> I'm the fist speaker
    
    	2
    	00:02:34.00 --> 00:02:35.00
    	<v Bingham> I'm the second speaker
    

    Should now be able to extract the speaker to VoiceName in Line struct.

    thanks, arun

    opened by arun1587 4
  • srtTimeBoundariesSeparator will lead to incorrect results

    srtTimeBoundariesSeparator will lead to incorrect results

    const ( srtTimeBoundariesSeparator = " --> " )

    when the srt content is like:

    1
    00:08:29,079 -->00:08:30,119
    hello
    

    because there lose a space, this line will not be indefiy, output string will be nothing.

    i hope return a error for this

    enhancement 
    opened by eager7 1
  • TTML to VTT style migration

    TTML to VTT style migration

    • Update TTM L parser to push parent style to child tags
    • Creating new custom style classes by combining inline style and style class
    • Added additional style propagation from TTML to VTT
    opened by Avishek-Gulshan 4
  • how to support vtt style

    how to support vtt style

    when use subtitles.ReadFromWebVTT, then style message will be losted.

    STYLE
    ::cue {
    color: #fff;
    text-shadow: 0 1px #000, 1px 0 #000, -1px 0 #000, 0 -1px #000;
    font-size: 15vw;
    }
    
    help wanted 
    opened by eager7 1
  • stl: have vertical position no lower than 1

    stl: have vertical position no lower than 1

    According to EBU 3264 (https://tech.ebu.ch/docs/tech/tech3264.pdf):

    For teletext subtitles, VP contains a value in the range 1-23 decimal (01h-17h)
    corresponding to theteletext row number of the first subtitle row.
    
    For in-vision subtitles, VP contains a value in the range 0..NN decimal,
    where NN is the maximumnumber of rows indicated in the MNR field in the GSI block
    (Note: NN cannot be greater than 99 decimal(63h)).
    This VP represents the number of row locations from the top of the screen to the first subtitle row.
    

    My motivation: some STL subs I encountered had an erroneous zero vertical position.

    opened by dlecorfec 8
  • When it reads STL files, it generates an array of ttiblocks in the cl…

    When it reads STL files, it generates an array of ttiblocks in the cl…

    …ass Subtitles. If we use sync from a stl filel, it changes starttimes in ttiblocks array and it saves the file from ttiblocks instead of Items, mantaining lines properties.

    It takes account colour and text position converting from STL to VTT. If creattion date and modification date of STL are empty, it continues conversion from STL to VTT

    opened by inremen 2
  • TTML styling conversion

    TTML styling conversion

    Are there any plans to add features for converting the styling in TTML input to various output formats (I'm hoping for VTT)

    help wanted 
    opened by saintberry 4
Owner
Quentin Renard
Freelance | Senior backend developer (GO)
Quentin Renard
golang library to read and write various subtitle formats

libgosubs Golang library to read and write subtitles in the following formats Advanced SubStation Alpha v4 SRT TTML v1.0 - This is based on the spec p

Wargarblgarbl 14 May 20, 2021
Parse and generate m3u8 playlists for Apple HTTP Live Streaming (HLS) in Golang (ported from gem https://github.com/sethdeckard/m3u8)

go-m3u8 Golang package for m3u8 (ported m3u8 gem https://github.com/sethdeckard/m3u8) go-m3u8 provides easy generation and parsing of m3u8 playlists d

Tan Quang Ngo 75 Jun 2, 2021
Parser and generator of M3U8-playlists for Apple HLS. Library for Go language. :cinema:

M3U8 This is the most complete opensource library for parsing and generating of M3U8 playlists used in HTTP Live Streaming (Apple HLS) for internet vi

Alexander I.Grafov 841 Jul 17, 2021
Plays videos using Prometheus and Grafana, e.g. Bad Apple.

prometheus_video_renderer Plays videos using Prometheus and Grafana, e.g. Bad Apple. Modes Currently 3 different modes are supported. Bitmap The bitma

Jacob Colvin 44 Jul 11, 2021
falco is VCL parser and linter optimized for Fastly.

falco falco is VCL parser and linter optimized for Fastly. Disclaimer This is a VCL parser, but dedicated to Fastly's VCL (version 2.x), so we don't c

Yoshiaki Sugimoto 20 Jul 15, 2021