Skip to content

Commit

Permalink
Restructure project and add library scan routine
Browse files Browse the repository at this point in the history
  • Loading branch information
nandesh-dev committed Oct 3, 2024
1 parent 1f2bb5d commit 64e0519
Show file tree
Hide file tree
Showing 39 changed files with 670 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
internal/pb
generated/*
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
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
}
44 changes: 44 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package server

import (
"fmt"
"net"

"github.com/nandesh-dev/subtle/generated/api/library"
library_service "github.com/nandesh-dev/subtle/internal/server/library"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

type server struct {
listener *net.Listener
grpcServer *grpc.Server
}

func New() *server {
return &server{}
}

func (s *server) Listen(port int, enableReflection bool) error {
listener, err := net.Listen("tcp", ":3000")
if err != nil {
return fmt.Errorf("failed to create listener: %v", err)
}

s.listener = &listener

s.grpcServer = grpc.NewServer()

if enableReflection {
reflection.Register(s.grpcServer)
}

libraryService := library_service.LibraryServiceServer{}
library.RegisterLibraryServiceServer(s.grpcServer, &libraryService)

if err := s.grpcServer.Serve(listener); err != nil {
return fmt.Errorf("failed to serve: %v", err)
}

return nil
}
Loading

0 comments on commit 64e0519

Please sign in to comment.