structural improvements
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package convertVideo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -4,16 +4,23 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"media-converter/convertVideo"
|
||||
"media-converter/optimizeImage"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const touchedFile = ".touched"
|
||||
|
||||
var touched = loadTouched()
|
||||
|
||||
var imageExts = map[string]bool{
|
||||
".jpeg": true,
|
||||
".jpg": true,
|
||||
".png": true,
|
||||
".gif": true,
|
||||
".bmp": true,
|
||||
".tiff": true,
|
||||
".tif": true,
|
||||
".webp": true,
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func main() {
|
||||
@@ -29,14 +36,14 @@ func main() {
|
||||
switch {
|
||||
case ext == ".mov":
|
||||
wg.Go(func() {
|
||||
ConvertVideo(path)
|
||||
convertVideo.ConvertVideo(path)
|
||||
})
|
||||
case imageExts[ext]:
|
||||
if touched[path] {
|
||||
if optimizeImage.Touched[path] {
|
||||
fmt.Printf("Skipping %s (already touched)\n", path)
|
||||
return nil
|
||||
}
|
||||
wg.Go(func() { OptimizeImage(path) })
|
||||
wg.Go(func() { optimizeImage.OptimizeImage(path) })
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -47,31 +54,3 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func loadTouched() map[string]bool {
|
||||
set := map[string]bool{}
|
||||
data, err := os.ReadFile(touchedFile)
|
||||
if err != nil {
|
||||
return set
|
||||
}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line != "" && !set[line] {
|
||||
set[line] = true
|
||||
}
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func markTouched(path string) {
|
||||
f, err := os.OpenFile(touchedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Error updating %s: %v\n", touchedFile, err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := fmt.Fprintln(f, path); err == nil {
|
||||
touched[path] = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
var imageExts = map[string]bool{
|
||||
".jpeg": true,
|
||||
".jpg": true,
|
||||
".png": true,
|
||||
".gif": true,
|
||||
".bmp": true,
|
||||
".tiff": true,
|
||||
".tif": true,
|
||||
".webp": true,
|
||||
}
|
||||
|
||||
func OptimizeImage(path string) {
|
||||
|
||||
fmt.Println("optiimg")
|
||||
cmd := exec.Command("mogrify",
|
||||
"-strip",
|
||||
"-interlace", "Plane",
|
||||
"-sampling-factor", "4:2:0",
|
||||
"-resize", "5000x3000",
|
||||
"-quality", "80%",
|
||||
path,
|
||||
)
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Printf("Error optimizing %s: %v\n%s", path, err, out)
|
||||
} else {
|
||||
markTouched(path)
|
||||
fmt.Printf("Optimized %s\n", path)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package optimizeImage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ImageExts = map[string]bool{
|
||||
".jpeg": true,
|
||||
".jpg": true,
|
||||
".png": true,
|
||||
".gif": true,
|
||||
".bmp": true,
|
||||
".tiff": true,
|
||||
".tif": true,
|
||||
".webp": true,
|
||||
}
|
||||
|
||||
const touchedFile = ".touched"
|
||||
|
||||
var Touched = loadTouched()
|
||||
|
||||
func OptimizeImage(path string) {
|
||||
|
||||
fmt.Println("optiimg")
|
||||
cmd := exec.Command("mogrify",
|
||||
"-strip",
|
||||
"-interlace", "Plane",
|
||||
"-sampling-factor", "4:2:0",
|
||||
"-resize", "5000x3000",
|
||||
"-quality", "80%",
|
||||
path,
|
||||
)
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Printf("Error optimizing %s: %v\n%s", path, err, out)
|
||||
} else {
|
||||
markTouched(path)
|
||||
fmt.Printf("Optimized %s\n", path)
|
||||
}
|
||||
}
|
||||
|
||||
func loadTouched() map[string]bool {
|
||||
set := map[string]bool{}
|
||||
data, err := os.ReadFile(touchedFile)
|
||||
if err != nil {
|
||||
return set
|
||||
}
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line != "" && !set[line] {
|
||||
set[line] = true
|
||||
}
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func markTouched(path string) {
|
||||
f, err := os.OpenFile(touchedFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Error updating %s: %v\n", touchedFile, err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := fmt.Fprintln(f, path); err == nil {
|
||||
Touched[path] = true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user