Skip to content

Instantly share code, notes, and snippets.

@romanlex
Created December 9, 2017 21:54
Show Gist options
  • Save romanlex/99741af338030dec3ac1a378e933231d to your computer and use it in GitHub Desktop.
Save romanlex/99741af338030dec3ac1a378e933231d to your computer and use it in GitHub Desktop.
package main
import (
"io/ioutil"
"log"
"fmt"
"os"
"sync"
"path/filepath"
"os/exec"
"strings"
"github.com/spf13/pflag"
//"bytes"
"time"
)
var opts = struct {
dir string
//threads int
//placeholder string
//workerTimeout time.Duration
//useNullSeparator bool
//hideJobID bool
//hideTimestamp bool
//hideName bool
}{}
func init() {
pflag.StringVar(&opts.dir, "directory", "./", "directory when get frames for convert")
//pflag.IntVarP(&opts.threads, "procs", "p", runtime.NumCPU(), "number of parallel programs")
//pflag.StringVar(&opts.placeholder, "replace", "{}", "replace this string in the command to run")
//pflag.DurationVar(&opts.workerTimeout, "timeout", 0*time.Second, "set maximum runtime per queued job (0s == no limit)")
//pflag.BoolVarP(&opts.useNullSeparator, "null", "0", false, "use null bytes as input separator")
//pflag.BoolVar(&opts.hideJobID, "no-id", false, "hide the job id in the log")
//pflag.BoolVar(&opts.hideTimestamp, "no-timestamp", false, "hide the time stamp in the log")
//pflag.BoolVar(&opts.hideName, "no-name", false, "hide the job name in the log")
pflag.Parse()
}
var (
lastLineCount = 0
)
func currentDir(msg bool) string {
currentDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if msg {
fmt.Println("Current directory: ", currentDir)
}
return currentDir
}
func listDirs(path string) []os.FileInfo {
var dirs []os.FileInfo
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found [%v] files in directory.\n", len(files))
fmt.Println("Listing dirs:")
for _, f := range files {
if f.Mode().IsDir() {
fmt.Println("\t -> " + f.Name(), f.Size(), "bytes")
dirs = append(dirs, f)
}
}
return dirs
}
func startConvertPng(dirs []os.FileInfo, statusWg *sync.WaitGroup) {
root := currentDir(false)
for index, dir := range dirs {
fmt.Printf("dir: %s, index: %v\n", dir.Name(), index)
statusWg.Add(1)
go func(index int, root string, dir os.FileInfo, statusWg *sync.WaitGroup) {
defer statusWg.Done()
path := filepath.Join(root, dir.Name())
convertPng(path, index)
}(index, root, dir, statusWg)
}
}
func convertPng(path string, routineNum int) {
fmt.Println(path)
os.Chdir(path)
time.Sleep(1000 * time.Millisecond)
currentDir, _ := os.Getwd()
fmt.Printf("routine %v: starting in dir %s\n", routineNum, currentDir)
cmd := exec.Command("/usr/bin/find", currentDir,
"-maxdepth", "1",
"-name", "*.png")
wc := exec.Command("wc","-l")
cmdOut, err := cmd.StdoutPipe()
cmd.Start()
wc.Stdin = cmdOut
wcOut, err := wc.Output()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + string(wcOut))
return
}
fmt.Printf("[%v]: Found %v png images.\n", currentDir, strings.TrimSpace(string(wcOut)))
//convert := exec.Command("/usr/bin/mogrify", "-verbose", "-format", "jpg", "*.png")
//var convertOut bytes.Buffer
//convert.Stdout = &convertOut
//err = convert.Start()
//if err != nil {
// log.Fatal(err)
// return
//}
//fmt.Println("Converting by mogrify " + currentDir)
//err = convert.Wait()
//fmt.Println("Convert successfully:" + convertOut.String())
}
func main() {
if &opts.dir != nil {
fmt.Println("Found directory argument:", &opts.dir)
}
path := currentDir(true)
fmt.Println("Change directory to work")
err := os.Chdir(opts.dir)
if err != nil {
log.Fatal(err)
}
fmt.Println("Reading directory...")
path = currentDir(true)
dirs := listDirs(path)
var statusWg sync.WaitGroup
fmt.Println("Start convert png to jpg by mogrify in each dir.")
startConvertPng(dirs, &statusWg)
statusWg.Wait()
fmt.Println("Done converting process")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment