Skip to content

Commit

Permalink
merge in tools into same repo, retire their old repos
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed Jun 19, 2017
1 parent 9228f87 commit b96fd96
Show file tree
Hide file tree
Showing 35 changed files with 826 additions and 216 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: go

go:
- 1.8
- tip

before_install:
- go get github.com/stretchr/testify/assert
- go get github.com/onsi/gomega
- go get github.com/onsi/ginkgo
- go get golang.org/x/tools/cmd/cover

script:
- go test -coverprofile=coverage.txt -covermode=atomic
- go test -v ./...

after_success:
- bash <(curl -s https://codecov.io/bash)
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# About

[![Travis-CI](https://api.travis-ci.org/martinlindhe/subtitles.svg)](https://travis-ci.org/martinlindhe/subtitles)
[![GoDoc](https://godoc.org/github.com/martinlindhe/subtitles?status.svg)](https://godoc.org/github.com/martinlindhe/subtitles)

Subtitles is a go library for handling .srt, .vtt and .ssa subtitles


Subtitles is a go library for handling .srt and .ssa subtitles

WARNING: The API is unstable, work in progress!


# Installation

```
go get -u github.com/martinlindhe/subtitles
go get -u github.com/martinlindhe/subtitles/...
```

# Example - convert srt to vtt

```go
import "github.com/martinlindhe/subtitles"

in := "1\n" +
"00:00:04,630 --> 00:00:06,018\n" +
"Go ninja!\n" +
Expand All @@ -26,14 +26,14 @@ in := "1\n" +
"00:01:09,630 --> 00:01:11,005\n" +
"No ninja!\n"

res, _ := NewFromSRT(in)
res, _ := subtitles.NewFromSRT(in)

// Output: WEBVTT
//
// 00:00:04.630 --> 00:00:06.018
// 00:04.630 --> 00:06.018
// Go ninja!
//
// 00:01:09.630 --> 00:01:11.005
// 01:09.630 --> 01:11.005
// No ninja!
fmt.Println(res.AsVTT())
```
Expand All @@ -44,16 +44,17 @@ fmt.Println(res.AsVTT())
f, _ := os.Open(fileName)
defer f.Close()

finder := NewSubFinder(f, fileName, "en")
finder := subtitles.NewSubFinder(f, fileName, "en")

text, err := finder.TheSubDb()
```


# See also

- [subber](https://github.com/martinlindhe/subber) command line tool for subtitles
- [ssa2srt](https://github.com/martinlindhe/ssa2srt) for converting .ssa to .srt
- [subber](https://github.com/martinlindhe/subtitles/subber) command line tool for subtitles
- [ssa2srt](https://github.com/martinlindhe/subtitles/ssa2srt) for converting .ssa to .srt
- [dcsub2srt](https://github.com/martinlindhe/subtitles/dcsub2srt) for converting dcsubtitles to .srt


# License
Expand Down
4 changes: 2 additions & 2 deletions caption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func TestRenderTime(t *testing.T) {

cap := Caption{
1,
MakeTime(18, 40, 22, 110),
MakeTime(18, 41, 20, 123),
makeTime(18, 40, 22, 110),
makeTime(18, 41, 20, 123),
[]string{"<i>Go ninja!</i>"},
}

Expand Down
45 changes: 8 additions & 37 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,8 @@ import (
log "github.com/Sirupsen/logrus"
)

// CleanupSub parses .srt or .ssa, performs cleanup and renders to a .srt, returning a string. caller is responsible for passing UTF8 string
func CleanupSub(utf8 string, filterName string, keepAds bool, sync int) (string, error) {
var subtitle Subtitle
var err error

if looksLikeSrt(utf8) {
subtitle, err = NewFromSRT(utf8)
} else {
// falls back on .ssa decoding, for now
subtitle, err = NewFromSSA(utf8)
}
if err != nil {
return "", err
}

if !keepAds {
subtitle.removeAds()
}

if sync != 0 {
subtitle.resyncSubs(sync)
}

subtitle.filterSubs(filterName)
out := subtitle.AsSRT()

return out, nil
}

func (subtitle *Subtitle) resyncSubs(sync int) {
// ResyncSubs adjust text timing by `sync` milliseconds
func (subtitle *Subtitle) ResyncSubs(sync int) {
// log.Printf("resyncing with %d\n", sync)
for i := range subtitle.Captions {
subtitle.Captions[i].Start = subtitle.Captions[i].Start.
Expand All @@ -46,9 +18,8 @@ func (subtitle *Subtitle) resyncSubs(sync int) {
}
}

// RemoveAds removes advertisement from the subtitles
func (subtitle *Subtitle) removeAds() *Subtitle {
ads := []string{
var (
advertisements = []string{
// english:
"captions paid for by",
"english subtitles",
Expand Down Expand Up @@ -107,24 +78,24 @@ func (subtitle *Subtitle) removeAds() *Subtitle {
// french:
"relecture et corrections finales:",
}
)

// RemoveAds removes advertisement from the subtitles
func (subtitle *Subtitle) RemoveAds() *Subtitle {
seq := 1
res := []Caption{}
for orgSeq, sub := range subtitle.Captions {

isAd := false

for _, line := range sub.Text {
x := strings.ToLower(line)
for _, adLine := range ads {
for _, adLine := range advertisements {
if !isAd && strings.Contains(x, adLine) {
isAd = true
log.Println("[ads]", (orgSeq + 1), sub.Text, "matched", adLine)
break
}
}
}

if !isAd {
sub.Seq = seq
res = append(res, sub)
Expand Down
22 changes: 11 additions & 11 deletions cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ func TestRemoveAds(t *testing.T) {

in := Subtitle{[]Caption{{
1,
MakeTime(0, 0, 4, 630),
MakeTime(0, 0, 6, 18),
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
[]string{"Go ninja!"},
}, {
2,
MakeTime(0, 1, 9, 630),
MakeTime(0, 1, 11, 005),
makeTime(0, 1, 9, 630),
makeTime(0, 1, 11, 005),
[]string{"Subtitles By MrCool"},
}, {
3,
MakeTime(0, 1, 9, 630),
MakeTime(0, 1, 11, 005),
makeTime(0, 1, 9, 630),
makeTime(0, 1, 11, 005),
[]string{"No ninja!"},
}}}

expected := Subtitle{[]Caption{{
1,
MakeTime(0, 0, 4, 630),
MakeTime(0, 0, 6, 18),
makeTime(0, 0, 4, 630),
makeTime(0, 0, 6, 18),
[]string{"Go ninja!"},
}, {
2,
MakeTime(0, 1, 9, 630),
MakeTime(0, 1, 11, 005),
makeTime(0, 1, 9, 630),
makeTime(0, 1, 11, 005),
[]string{"No ninja!"},
}}}

assert.Equal(t, &expected, in.removeAds())
assert.Equal(t, &expected, in.RemoveAds())
}
9 changes: 9 additions & 0 deletions cmd/dcsub2srt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# About

Command line util to convert DCSubtitles into .srt format


# Installation
```
go get -u github.com/martinlindhe/subtitles/cmd/dcsub2srt
```
31 changes: 31 additions & 0 deletions cmd/dcsub2srt/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// XXX should just invoke "subber vid.xml -o vid.srt"

package main

import (
"fmt"
"io/ioutil"
"log"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/martinlindhe/subtitles"
)

var (
name = kingpin.Arg("name", "DCSubtitle file.").Required().String()
)

func main() {
kingpin.Parse()

data, err := ioutil.ReadFile(*name)
if err != nil {
log.Fatal(err)
}
sub, err := subtitles.NewFromDCSub(string(data))
if err != nil {
log.Fatal(err)
}
fmt.Print(sub.AsSRT())
}
17 changes: 17 additions & 0 deletions cmd/ssa2srt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# About

Converts a .ssa to .srt


# Installation

```
go install github.com/martinlindhe/subtitles/cmd/ssa2srt
```


# Usage

```
$ ssa2srt subtile.ssa
```
96 changes: 96 additions & 0 deletions cmd/ssa2srt/ssa2srt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// XXX should just invoke "subber vid.ssa -o vid.srt"

package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"

"github.com/martinlindhe/subtitles"

"gopkg.in/alecthomas/kingpin.v2"
)

var (
file = kingpin.Arg("file", "A .srt (to clean) or video file (to fetch subs).").Required().File()
keepAds = kingpin.Flag("keep-ads", "Do not strip advertisement captions.").Bool()
filterName = kingpin.Flag("filter", "Filter (none, caps, html).").Default("none").String()
)

const version = "0.1.1"

func main() {
// support -h for --help
kingpin.Version(version)
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Parse()

inFileName := (*file).Name()
ext := path.Ext(inFileName)
subFileName := inFileName[0:len(inFileName)-len(ext)] + ".srt"

// skip "hidden" .dotfiles
baseName := filepath.Base(inFileName)
if baseName[0] == '.' {
// fmt.Printf("Skipping hidden %s\n", inFileName)
os.Exit(1)
}

data, err := ioutil.ReadFile(inFileName)
if err != nil {
log.Fatal(err)
}

out, err := cleanupSub(data, *filterName, *keepAds, 0)
if err != nil {
log.Fatal(err)
}

writeText(subFileName, false, out)

fmt.Printf("Written %s\n", subFileName)
}

// cleanupSub parses .srt or .ssa, performs cleanup and renders to a .srt, returning a string. caller is responsible for passing UTF8 string
func cleanupSub(data []byte, filterName string, keepAds bool, sync int) (string, error) {
subtitle, err := subtitles.Parse(data)
if err != nil {
return "", err
}
if !keepAds {
subtitle.RemoveAds()
}
if sync != 0 {
subtitle.ResyncSubs(sync)
}
subtitle.FilterCaptions(filterName)
out := subtitle.AsSRT()
return out, nil
}

func writeText(outFileName string, skipBackups bool, text string) error {

if !skipBackups && fileExists(outFileName) {
backupFileName := outFileName + ".org"
os.Rename(outFileName, backupFileName)
// fmt.Printf("Backed up to %s\n", backupFileName)
}

err := ioutil.WriteFile(outFileName, []byte(text), 0644)
if err != nil {
return err
}

return nil
}

func fileExists(name string) bool {
if _, err := os.Stat(name); err == nil {
return true
}
return false
}
Loading

0 comments on commit b96fd96

Please sign in to comment.