-
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.
Merge pull request #7 from nandesh-dev/development
Add library scan routine
- Loading branch information
Showing
41 changed files
with
699 additions
and
287 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,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) |
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
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 | ||
} |
Oops, something went wrong.