Skip to content

Commit

Permalink
intelligent naming for digestion
Browse files Browse the repository at this point in the history
  • Loading branch information
crushr3sist committed Oct 29, 2023
1 parent 3213e34 commit 9b1c740
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 49 deletions.
16 changes: 10 additions & 6 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ import (
)

func ConvertAll(paths []string) {
fmt.Println(paths)

for _, file := range paths {
fileDir := filepath.Join("videos", filepath.Base(file[:len(file)-4]))

fileDir := filepath.Join("videos", ExtractShowName(DeFormatter(ExtractFolderPath(FormatPath(file)))), filepath.Base(file[:len(file)-4]))
err := os.MkdirAll(fileDir, os.ModePerm)
if err != nil {
fmt.Println(err)
return
continue
}

in := ffmpeg.Input(file)
out := in.Output(filepath.Join(fileDir, "output.m3u8"), ffmpeg.KwArgs{
"c:v": "h264_nvenc", // NVIDIA NVENC hardware acceleration
"b:v": "5M", // Video bitrate
"c:a": "aac", // AAC audio codec
"c:v": "h264_nvenc",
"b:v": "5M",
"c:a": "aac",
"strict": "-2",
"start_number": "0",
"hls_time": "10",
"hls_list_size": "0",
"f": "hls", // Output format
"f": "hls",
})
err = out.OverWriteOutput().WithOutput(os.Stdout).Run()
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions convert/dataJsonHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package convert

import (
"encoding/json"
"fmt"
"os"
)

type ShowEpisodes struct {
Episodes []string `json:"converted"`
}

// Define a struct for the entire JSON structure
type ConvertedData struct {
Converted map[string]ShowEpisodes `json:"converted"`
}

func filterPathList(paths []string) []string {
return []string{}
}

func readConvertedJson() ConvertedData {

file, err := os.Open("../videos/converted.json")
if err != nil {
os.Create("../videos/converted.json")
fmt.Println("error opening the file", err)
} else {

defer file.Close()
}

decoder := json.NewDecoder(file)

var readData ConvertedData

if err := decoder.Decode(&readData); err != nil {
fmt.Println("error decoding json", err)
}

return readData
}
75 changes: 75 additions & 0 deletions convert/formatters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package convert

import (
"path/filepath"
"strings"
"unicode"
)

func ExtractShowName(pathString string) string {
pathString = filepath.ToSlash(pathString)
pathString = strings.ReplaceAll(pathString, " ", "")

parts := strings.Split(pathString, "/")

showKeywords := []string{"season", "series", "show", "s"}

// Iterate over path parts from the end to the beginning
for i := len(parts) - 1; i >= 0; i-- {
// Check if the part contains a show keyword
for _, keyword := range showKeywords {
if strings.Contains(strings.ToLower(parts[i]), keyword) && i > 0 {
// Find the next non-empty part as the show name
for j := i - 1; j >= 0; j-- {
if parts[j] != "" {
return strings.TrimSpace(parts[j])
}
}
}
}
}
return ""
}
func ExtractFolderPath(pathString string) string {
// takes a path string and extracts the video name
// algorithm works by splitting the string by the last \\
// left side is the path

backSlashCount := strings.Count(pathString, "\\")
counter := 0
retIndex := 0

for i := 0; i < len(pathString); i++ {
if string(pathString[i]) == "\\" {
counter += 1
if counter == backSlashCount {
retIndex = i
}
}
}

retIndex += 1
return pathString[:retIndex]
}

func FormatPath(pathString string) string {
formattedString := ""

for i := range pathString {
if unicode.IsSpace(rune(pathString[i])) || string(pathString[i]) == " " {
formattedString = strings.Replace(pathString, " ", "%20", -1)
}
}

if len(formattedString) != 0 {
return formattedString

} else {
return pathString
}
}

func DeFormatter(mediaName string) string {
mediaName = strings.ReplaceAll(mediaName, "%20", "")
return mediaName
}
75 changes: 41 additions & 34 deletions directory/directory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package directory

import (
"log"
"os"
"path"
"path/filepath"
Expand All @@ -16,57 +15,33 @@ func ScanRecursive(dir_path string, ignore []string) ([]string, []string) {
folders := []string{}
files := []string{}

// Scan
filepath.Walk(dir_path, func(path string, f os.FileInfo, err error) error {
filepath.Walk(dir_path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

_continue := false

// Loop : Ignore Files & Folders
for _, i := range ignore {

// If ignored path
if strings.Index(path, i) != -1 {

// Continue
_continue = true
}
}

if _continue == false {

f, err = os.Stat(path)

// If no error
if err != nil {
log.Fatal(err)
}

// File & Folder Mode
f_mode := f.Mode()

// Is folder
f_mode := info.Mode()
if f_mode.IsDir() {

// Append to Folders Array
folders = append(folders, path)

// Is file
} else if f_mode.IsRegular() {

// Append to Files Array
files = append(files, path)
}
}

return nil
})

return folders, files
}

func FormatPath(pathString string) string {
// makes the path formatted correctly to query

formattedString := ""

for i := range pathString {
Expand All @@ -84,13 +59,45 @@ func FormatPath(pathString string) string {
}

func ExtractVideoName(pathString string) string {
_, filename := filepath.Split(pathString)
return filename
// takes a path string and extracts the video name
// algorithm works by splitting the string by the last \\
// right side is the file
backSlashCount := strings.Count(pathString, "\\")
counter := 0
retIndex := 0

for i := 0; i < len(pathString); i++ {
if string(pathString[i]) == "\\" {
counter += 1
if counter == backSlashCount {
retIndex = i
}
}
}
retIndex += 1
return pathString[retIndex:]
}

func ExtractFolderPath(pathString string) string {
dir, _ := filepath.Split(pathString)
return dir
// takes a path string and extracts the video name
// algorithm works by splitting the string by the last \\
// left side is the path

backSlashCount := strings.Count(pathString, "\\")
counter := 0
retIndex := 0

for i := 0; i < len(pathString); i++ {
if string(pathString[i]) == "\\" {
counter += 1
if counter == backSlashCount {
retIndex = i
}
}
}

retIndex += 1
return pathString[:retIndex]
}

type DirectoryInstance struct {
Expand Down
12 changes: 3 additions & 9 deletions directory/directoryRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import (
)

func parseDir(c *fiber.Ctx) error {
// gets the path from request : done
// walks the dir if found : done
// we save the path into the database
//

c.Accepts("application/json")

dataStruct := struct {
Expand All @@ -28,11 +25,8 @@ func parseDir(c *fiber.Ctx) error {
}

parsedDirs := ParseDirs(dataStruct.DirectoryToTarget, dataStruct.ContentType)

convert.ConvertAll(parsedDirs.RawPaths)

fmt.Print(parsedDirs)

return c.JSON(parsedDirs)

}
Expand All @@ -47,7 +41,7 @@ type ShowResponse struct {
}

func ReturnShowsList() ShowResponse {
showsDir := "./videos" // Adjust the path to your videos directory
showsDir := "./videos"

showsList, err := ioutil.ReadDir(showsDir)
if err != nil {
Expand Down Expand Up @@ -79,7 +73,7 @@ func ReturnShowsList() ShowResponse {
}

func returnDirs(c *fiber.Ctx) error {
data := ReturnShowsList() // Assuming ReturnShowsList is defined in the same package
data := ReturnShowsList()
return c.Status(fiber.StatusOK).JSON(data)
}

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func appInstance() *fiber.App {
}

func main() {

app := appInstance()
db.InitialMigration()
// Add middleware
Expand Down

0 comments on commit 9b1c740

Please sign in to comment.