Skip to content

Commit

Permalink
Merge pull request #7 from nandesh-dev/development
Browse files Browse the repository at this point in the history
Add library scan routine
  • Loading branch information
nandesh-dev authored Oct 3, 2024
2 parents f60734e + 3fdbf9e commit 4c1aadb
Show file tree
Hide file tree
Showing 41 changed files with 699 additions and 287 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
internal/pb
generated/*
21 changes: 21 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
# Development

## Setup

1. Install dependencies listed in [README.md](https://github.com/nandesh-dev/subtle/blob/main/README.md) or [shell.nix](https://github.com/nandesh-dev/subtle/blob/main/shell.nix)

2. Generate protobuf files

```bash
make proto
```

3. Run go

```bash
go run ./cmd/subtle
```

## Docker Image

### Volumes

|Path|Use|
|---|---|
|`/media`|Directory where all your media content is stored|
|`/config`|Directory where all the subtle data will be stored|

## References

### Golang Packages
[gosseract](https://pkg.go.dev/github.com/otiai10/gosseract/[email protected])
[ffmpeg\_go](https://pkg.go.dev/github.com/u2takey/ffmpeg-go)
[grpc](https://grpc.io/docs/languages/go/)
[yaml](https://pkg.go.dev/gopkg.in/yaml.v3)

### File Formats
[Presentation Graphic Stream](https://blog.thescorpius.com/index.php/2017/07/15/presentation-graphic-stream-sup-files-bluray-subtitle-format/)
[Sub Station Alpha v4.00+](http://www.tcax.org/docs/ass-specs.htm)
[SubRip](https://docs.fileformat.com/video/srt/)

### Other
[tesseract](https://github.com/tesseract-ocr/tesseract)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: proto
.PHONY: proto run

proto:
protoc ./proto/**/*.proto --go_out=. --go-grpc_out=.
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ Your tool to manage all the subtitles in your personal media library. Subtle hel
2. Install dependencies

```bash
apt install tesseract-ocr libtesseract-dev libleptonica-dev
apt install tesseract-ocr libtesseract-dev libleptonica-dev ffmpeg
```

3. Build the package

```bash
go build ./cmd/tui
go build ./cmd/subtle
```

### Docker

```bash
docker run -v /path/to/config:/config -v /path/to/media:/media nandeshdev/subtle
```

## License
Expand Down
59 changes: 4 additions & 55 deletions cmd/subtle/main.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
package main

import (
"fmt"
"log"
"net"

"github.com/nandesh-dev/subtle/internal/filemanager"
"github.com/nandesh-dev/subtle/internal/pb/library"
"github.com/nandesh-dev/subtle/internal/pgs"
"github.com/nandesh-dev/subtle/internal/services"
"github.com/nandesh-dev/subtle/internal/subtitle"
"github.com/nandesh-dev/subtle/internal/tesseract"
"golang.org/x/text/language"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"github.com/nandesh-dev/subtle/internal/routine/library"
"github.com/nandesh-dev/subtle/pkgs/config"
)

func main() {
dir, err := filemanager.ReadDirectory("/media")
if err != nil {
if err := config.Init("/config"); err != nil {
log.Fatal(err)
}

fmt.Println(dir)

videos, warnings := dir.VideoFiles()
fmt.Println(warnings)

rawStreams, err := subtitle.ExtractRawStreams(&videos[0])
if err != nil {
log.Fatal(err)
}

subtitle, _, err := pgs.DecodeSubtitle(&rawStreams[0])
if err != nil {
log.Fatal(err)
}

tes := tesseract.NewClient()

for _, segment := range subtitle.Segments() {
for _, img := range segment.Images() {
text, err := tes.ExtractTextFromImage(img, language.English)
if err != nil {
log.Fatal(err)
}

fmt.Println(text)
}
}

listener, err := net.Listen("tcp", ":3000")
if err != nil {
log.Fatalln("failed to create listener: ", err)
}

s := grpc.NewServer()
reflection.Register(s)

libraryService := services.Services().Library
library.RegisterLibraryServiceServer(s, &libraryService)

if err := s.Serve(listener); err != nil {
log.Fatalln("failed to serve: ", err)
}
library.RunLibraryRoutine()
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ require (
golang.org/x/text v0.18.0
google.golang.org/grpc v1.67.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/aws/aws-sdk-go v1.38.20 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-sqlite3 v1.14.23 // indirect
github.com/u2takey/go-utils v0.3.1 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down Expand Up @@ -72,6 +70,7 @@ google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw=
google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
65 changes: 0 additions & 65 deletions internal/filemanager/directory.go

This file was deleted.

31 changes: 0 additions & 31 deletions internal/filemanager/read_dir.go

This file was deleted.

92 changes: 92 additions & 0 deletions internal/routine/library/library.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package library

import (
"fmt"
"os"
"path/filepath"
"slices"

"github.com/nandesh-dev/subtle/pkgs/config"
"github.com/nandesh-dev/subtle/pkgs/filemanager"
"github.com/nandesh-dev/subtle/pkgs/subtitle"
"github.com/nandesh-dev/subtle/pkgs/warning"
"golang.org/x/text/language"
)

func RunLibraryRoutine() {
warnings := warning.NewWarningList()

for _, watchDirectoryConfig := range config.Config().Media.WatchDirectories {
dir, _, err := filemanager.ReadDirectory(watchDirectoryConfig.Path)
if err != nil {
return
}

for _, videoFile := range dir.VideoFiles() {
missingSubtitleLanguages := make([]language.Tag, 0)
for _, languageCode := range watchDirectoryConfig.AutoExtract.Languages {
languageTag, err := language.Parse(languageCode)
if err != nil {
warnings.AddWarning(fmt.Errorf("Invalid language code in config: %v; %v", languageCode, err))
continue
}

hasSubtitleLanguage, wrn := videoFile.HasSubtitleLanguage(languageTag)
warnings.Append(wrn)

if !hasSubtitleLanguage {
missingSubtitleLanguages = append(missingSubtitleLanguages, languageTag)
}
}

if len(missingSubtitleLanguages) == 0 {
continue
}

rawStreams, err := subtitle.ExtractRawStreams(videoFile.Filepath())
if err != nil {
warnings.AddWarning(fmt.Errorf("Error extracting raw streams from video file: %v", err))
}

for _, formatCode := range watchDirectoryConfig.AutoExtract.Formats {
format, err := subtitle.ParseFormat(formatCode)
if err != nil {
warnings.AddWarning(fmt.Errorf("Invalid subtitle format in config: %v", formatCode))
continue
}

for _, rawStream := range rawStreams {
if format == rawStream.Format() && slices.Contains(missingSubtitleLanguages, rawStream.Language()) {
sub, wrn, err := subtitle.FromRawStream(rawStream)
warnings.Append(wrn)
if err != nil {
warnings.AddWarning(fmt.Errorf("Error extracting subtitle: %v", err))
return
}

outputFormat, err := subtitle.ParseFormat(watchDirectoryConfig.AutoExtract.OutputFormat)
if err != nil {
warnings.AddWarning(fmt.Errorf("Invalid output format in config: %v", err))
return
}

encodedSubtitle, _ := sub.Encode(outputFormat)

for _, file := range encodedSubtitle.Files() {
path := filepath.Join(videoFile.DirectoryPath(), videoFile.Basename()+"."+rawStream.Language().String()) + file.Extension()

if err := os.WriteFile(path, file.Content(), 0644); err != nil {
fmt.Print(err)
}
}
}
}
}
}
}

fmt.Println("WARNINGS BEGIN")
for _, warning := range warnings.Warnings() {
fmt.Println(warning)
}
}
15 changes: 15 additions & 0 deletions internal/server/library/library.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package library

import (
"context"

"github.com/nandesh-dev/subtle/generated/api/library"
)

type LibraryServiceServer struct {
library.UnsafeLibraryServiceServer
}

func (s *LibraryServiceServer) GetMedia(ctx context.Context, req *library.GetMediaRequest) (*library.GetMediaResponse, error) {
return nil, nil
}
Loading

0 comments on commit 4c1aadb

Please sign in to comment.