-
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 #8 from nandesh-dev/development
Development
- Loading branch information
Showing
70 changed files
with
4,584 additions
and
761 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,3 @@ | ||
generated/* | ||
generated/ | ||
web/node_modules/ | ||
web/gen |
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
2. Generate protobuf files | ||
|
||
```bash | ||
make proto | ||
buf generate | ||
``` | ||
|
||
3. Run go | ||
|
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,8 @@ | ||
version: v2 | ||
plugins: | ||
- local: protoc-gen-go | ||
out: generated | ||
opt: paths=source_relative | ||
- local: protoc-gen-connect-go | ||
out: generated | ||
opt: paths=source_relative |
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,16 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/nandesh-dev/subtle/internal/routine/library" | ||
"github.com/nandesh-dev/subtle/internal/server" | ||
"github.com/nandesh-dev/subtle/pkgs/config" | ||
"github.com/nandesh-dev/subtle/pkgs/db" | ||
) | ||
|
||
func main() { | ||
if err := config.Init("/config"); err != nil { | ||
fmt.Println("Initilizing config") | ||
if err := config.Init("./config"); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
library.RunLibraryRoutine() | ||
fmt.Println("Initilizing database") | ||
if err := db.Init(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
server := server.New() | ||
|
||
server.Listen(3000, true) | ||
} |
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 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,169 @@ | ||
package extract | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"image/png" | ||
"log" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/nandesh-dev/subtle/pkgs/ass" | ||
"github.com/nandesh-dev/subtle/pkgs/config" | ||
"github.com/nandesh-dev/subtle/pkgs/db" | ||
"github.com/nandesh-dev/subtle/pkgs/filemanager" | ||
"github.com/nandesh-dev/subtle/pkgs/pgs" | ||
"github.com/nandesh-dev/subtle/pkgs/subtitle" | ||
"github.com/nandesh-dev/subtle/pkgs/warning" | ||
"golang.org/x/text/language" | ||
) | ||
|
||
func Run() warning.WarningList { | ||
warnings := warning.NewWarningList() | ||
for _, rootDirectoryConfig := range config.Config().Media.RootDirectories { | ||
dir, _, err := filemanager.ReadDirectory(rootDirectoryConfig.Path) | ||
if err != nil { | ||
warnings.AddWarning(fmt.Errorf("Error reading root directory: %v; %v", rootDirectoryConfig.Path, err)) | ||
continue | ||
} | ||
|
||
warns := extractSubtitleFromDirectory(*dir, rootDirectoryConfig.AutoExtract) | ||
warnings.Append(warns) | ||
} | ||
|
||
return *warnings | ||
} | ||
|
||
func extractSubtitleFromDirectory(dir filemanager.Directory, autoExtractConfig config.AutoExtract) warning.WarningList { | ||
warnings := warning.NewWarningList() | ||
|
||
for _, video := range dir.VideoFiles() { | ||
var videoEntry db.Video | ||
if err := db.DB().Where(&db.Video{DirectoryPath: video.DirectoryPath(), Filename: video.Filename()}). | ||
Preload("Subtitles"). | ||
Preload("Subtitles.Segments"). | ||
First(&videoEntry).Error; err != nil { | ||
log.Fatal("Error getting entry: ", err, video.DirectoryPath(), video.Filename()) | ||
} | ||
|
||
rawStreams, err := video.RawStreams() | ||
if err != nil { | ||
warnings.AddWarning(fmt.Errorf("Error extracting raw stream from video: %v; %v", video.Filepath(), err)) | ||
continue | ||
} | ||
|
||
rawStreamRanks := map[filemanager.RawStream]int{} | ||
|
||
for _, rawStream := range *rawStreams { | ||
if !slices.ContainsFunc(videoEntry.Subtitles, func(subtitleEntry db.Subtitle) bool { | ||
subtitleEntryLanguageTag, _ := language.Parse(subtitleEntry.Language) | ||
return subtitleEntryLanguageTag == rawStream.Language() | ||
}) && slices.ContainsFunc(autoExtractConfig.Languages, func(lang language.Tag) bool { | ||
return lang == rawStream.Language() | ||
}) { | ||
rawStreamRanks[rawStream] = 1 | ||
|
||
for _, titleKeyword := range autoExtractConfig.RawStreamTitleKeywords { | ||
if strings.Contains(rawStream.Title(), titleKeyword) { | ||
rawStreamRanks[rawStream]++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
highestRank := 0 | ||
var highestRankRawStream filemanager.RawStream | ||
for rawStream, rank := range rawStreamRanks { | ||
if rank >= highestRank { | ||
highestRank = rank | ||
highestRankRawStream = rawStream | ||
} | ||
} | ||
|
||
if highestRank == 0 { | ||
continue | ||
} | ||
|
||
rawStream := highestRankRawStream | ||
var sub subtitle.Subtitle | ||
|
||
switch rawStream.Format() { | ||
case subtitle.ASS: | ||
s, warns, err := ass.DecodeSubtitle(rawStream) | ||
warnings.Append(warns) | ||
if err != nil { | ||
warnings.AddWarning(fmt.Errorf("Error decoding subtitle for video: %v; %v", video.Filepath(), err)) | ||
continue | ||
} | ||
|
||
sub = *s | ||
|
||
case subtitle.PGS: | ||
s, warns, err := pgs.DecodeSubtitle(rawStream) | ||
warnings.Append(warns) | ||
if err != nil { | ||
warnings.AddWarning(fmt.Errorf("Error decoding subtitle for video: %v; %v", video.Filepath(), err)) | ||
continue | ||
} | ||
|
||
sub = *s | ||
} | ||
|
||
if sub == nil { | ||
continue | ||
} | ||
|
||
switch sub := sub.(type) { | ||
case subtitle.TextSubtitle: | ||
subtitleEntry := db.Subtitle{ | ||
Language: rawStream.Language().String(), | ||
Filepath: "", | ||
IsImage: false, | ||
Segments: make([]db.Segment, 0), | ||
} | ||
|
||
for _, segment := range sub.Segments() { | ||
segmentEntry := db.Segment{ | ||
StartTime: segment.Start(), | ||
EndTime: segment.End(), | ||
Text: segment.Text(), | ||
} | ||
|
||
subtitleEntry.Segments = append(subtitleEntry.Segments, segmentEntry) | ||
} | ||
|
||
videoEntry.Subtitles = append(videoEntry.Subtitles, subtitleEntry) | ||
|
||
db.DB().Save(&videoEntry) | ||
case subtitle.ImageSubtitle: | ||
subtitleEntry := db.Subtitle{ | ||
Language: rawStream.Language().String(), | ||
Filepath: "", | ||
IsImage: true, | ||
Segments: make([]db.Segment, 0), | ||
} | ||
|
||
for _, segment := range sub.Segments() { | ||
imageDataBuffer := new(bytes.Buffer) | ||
if err := png.Encode(imageDataBuffer, segment.Image()); err != nil { | ||
warnings.AddWarning(fmt.Errorf("Error encoding image to png for video: %v; %v", video.Filepath(), err)) | ||
continue | ||
} | ||
|
||
segmentEntry := db.Segment{ | ||
StartTime: segment.Start(), | ||
EndTime: segment.End(), | ||
ImageData: imageDataBuffer.Bytes(), | ||
} | ||
|
||
subtitleEntry.Segments = append(subtitleEntry.Segments, segmentEntry) | ||
} | ||
|
||
videoEntry.Subtitles = append(videoEntry.Subtitles, subtitleEntry) | ||
|
||
db.DB().Save(&videoEntry) | ||
} | ||
} | ||
|
||
return *warnings | ||
} |
Oops, something went wrong.