149 lines
3.3 KiB
Go
149 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/jaypipes/ghw"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
file = flag.String("file", "", "file to transcode")
|
|
outname = flag.String("out", "", "file base name to rename result to")
|
|
hevc = flag.Bool("hevc", false, "transcodes to x265 instead of x264")
|
|
lib = flag.Bool("lib", false, "uses the x264/x265 software encoder instead of hardware encoder")
|
|
keep = flag.Bool("keep", false, "Keeps video file after transcodes")
|
|
class = flag.String("class", "blu", "class of source file (e.g. dvd, blu for bluray)")
|
|
runs = flag.Int("runs", 1, "number of times to do the same encode")
|
|
args = []string{}
|
|
format = "x264"
|
|
filename string
|
|
)
|
|
flag.Parse()
|
|
basename := filepath.Base(*file)
|
|
if *outname == "" {
|
|
filename = filepath.Base(*file)
|
|
} else {
|
|
filename = *outname
|
|
if filepath.Ext(filename) == "" {
|
|
filename += ".mkv"
|
|
}
|
|
}
|
|
|
|
gpu, err := ghw.GPU()
|
|
if err != nil {
|
|
fmt.Printf("Error getting GPU info: %v", err)
|
|
}
|
|
|
|
cpu, err := ghw.CPU()
|
|
if err != nil {
|
|
fmt.Printf("Error getting CPU info: %v", err)
|
|
}
|
|
cpuName := strings.ToLower(cpu.Processors[0].Model)
|
|
for _, rep := range []struct {
|
|
old string
|
|
new string
|
|
}{
|
|
{"amd", ""},
|
|
{"ryzen ", "r"},
|
|
{"8-core", ""},
|
|
{"7-core", ""},
|
|
{"6-core", ""},
|
|
{"5-core", ""},
|
|
{"4-core", ""},
|
|
{"processor", ""},
|
|
} {
|
|
cpuName = strings.ReplaceAll(cpuName, rep.old, rep.new)
|
|
}
|
|
|
|
cpuName = strings.ReplaceAll(strings.TrimSpace(cpuName), " ", "-")
|
|
gpuName := strings.ToLower(gpu.GraphicsCards[0].DeviceInfo.Product.Name)
|
|
if strings.Contains(gpuName, "[") {
|
|
gpuName = strings.Split(strings.Split(gpuName, "[")[1], "]")[0]
|
|
}
|
|
for _, rep := range []struct {
|
|
old string
|
|
new string
|
|
}{
|
|
{"amd", ""},
|
|
{"radeon ", "r"},
|
|
{"nvidia", ""},
|
|
{"geforce", ""},
|
|
{"processor", ""},
|
|
} {
|
|
gpuName = strings.ReplaceAll(gpuName, rep.old, rep.new)
|
|
}
|
|
|
|
gpuName = strings.ReplaceAll(strings.TrimSpace(gpuName), " ", "-")
|
|
|
|
hardwareName := gpuName
|
|
|
|
if *hevc {
|
|
args = []string{"--hevc"}
|
|
format = "x265"
|
|
}
|
|
|
|
if *lib {
|
|
hardwareName = cpuName
|
|
if *hevc {
|
|
args = []string{"--x265"}
|
|
} else {
|
|
args = []string{"--x264"}
|
|
}
|
|
}
|
|
args = append(args, *file)
|
|
GOOS := runtime.GOOS
|
|
if len(GOOS) > 5 {
|
|
GOOS = GOOS[:5]
|
|
}
|
|
o := fmt.Sprintf("%s.% 5s.% 8s.%s.%s", strings.TrimSuffix(filename, filepath.Ext(filename)), GOOS, hardwareName, format, *class)
|
|
if *runs < 2 {
|
|
transcode(args, basename, o, 0, *keep)
|
|
} else {
|
|
for i := 1; i <= *runs; i++ {
|
|
transcode(args, basename, o, i, *keep)
|
|
}
|
|
}
|
|
}
|
|
|
|
func transcode(args []string, basename, outname string, run int, keep bool) {
|
|
fmt.Println("other-transcode", args)
|
|
cmd := exec.Command("other-transcode", args...)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
stat, err := os.Stat(basename)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var result string
|
|
if run > 0 {
|
|
result = fmt.Sprintf(outname+".%03d.%012d%s", run, stat.Size(), filepath.Ext(basename))
|
|
} else {
|
|
result = fmt.Sprintf(outname+".%012d%s", stat.Size(), filepath.Ext(basename))
|
|
}
|
|
fmt.Println(result)
|
|
if keep {
|
|
err = os.Rename(basename, result)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
} else {
|
|
os.Remove(basename)
|
|
}
|
|
err = os.Rename(basename+".log", result+".log")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|