package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
func main() {
args := os.Args[1:]
if len(args) < 2 {
fmt.Println( "usage: src dst")
return
}
b, err := transform(args...)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(string(b))
}
func copy(w io.Writer, r io.Reader) error {
header := make([]byte, 9)
_, err := io.ReadFull(r, header)
if err != nil {
return err
}
_, err = io.Copy(w, r)
return err
}
func find(root, ext string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ext {
files = append(files, path)
}
return nil
})
return files, err
}
func transform(args ...string) ([]byte, error) {
const suffix = ".m4s"
files, err := find(args[0], suffix)
if err != nil {
return nil, err
}
tmp := make([]string, 2)
if len(files) < len(tmp) {
return nil, fmt.Errorf("can't find enough m4s")
}
for i, _ := range tmp {
//base := filepath.Base(files[i]) base[:len(base)-len(suffix)] + ".mp4"
t, err := ioutil.TempFile("", "*.mp4")
if err != nil {
return nil, err
}
tmp[i] = t.Name()
r, err := os.Open(files[i])
if err != nil {
return nil, err
}
err = copy(t, r)
if err != nil {
return nil, err
}
err = t.Close()
if err != nil {
return nil, err
}
err = r.Close()
if err != nil {
return nil, err
}
defer os.Remove(t.Name())
}
args = append([]string{"-i", tmp[0], "-i", tmp[1], "-acodec", "copy", "-vcodec", "copy"}, args[1:]...)
cmd := exec.Command("ffmpeg", args...)
return cmd.Output()
}