-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure project and add library scan routine
- Loading branch information
1 parent
1f2bb5d
commit 64e0519
Showing
39 changed files
with
670 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
internal/pb | ||
generated/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.