165 lines
2.8 KiB
Go
165 lines
2.8 KiB
Go
|
package mods
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
|
||
|
"go.cdmnky.io/v2/fs"
|
||
|
"go.cdmnky.io/v2/str"
|
||
|
"go.cdmnky.io/v2/utils"
|
||
|
)
|
||
|
|
||
|
func Format(format string, i ...any) string {
|
||
|
return fmt.Sprintf(format, i...)
|
||
|
}
|
||
|
|
||
|
func ProcessVideo(videoURL, videofile string) (err error) {
|
||
|
|
||
|
fmt.Printf("Fetching video data into file '%s'... ", videofile)
|
||
|
data, err := Dload(videoURL)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err = os.WriteFile(videofile, data, 0600); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
fmt.Println("Complete!")
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Dload(src string) (data []byte, err error) {
|
||
|
|
||
|
client := &http.Client{}
|
||
|
req, _ := http.NewRequest("GET", src, nil)
|
||
|
|
||
|
res, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
if res.StatusCode == 200 {
|
||
|
data, err = io.ReadAll(res.Body)
|
||
|
res.Body.Close()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
} else {
|
||
|
err = fmt.Errorf("errcode: %d", res.StatusCode)
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return data, nil
|
||
|
}
|
||
|
|
||
|
func Encode(ffmpeg string, files []string) (err error) {
|
||
|
|
||
|
if !fs.FileExists(ffmpeg) {
|
||
|
return fmt.Errorf("ffmpeg binary not found: %s", ffmpeg)
|
||
|
}
|
||
|
ratio := "1280x720"
|
||
|
|
||
|
for _, file := range files {
|
||
|
encfile := fmt.Sprintf("/tmp/%s.mp4", str.Random(12))
|
||
|
fmt.Printf("Encoding (%s) '%s'\n", ratio, encfile)
|
||
|
args := []string{
|
||
|
"-i",
|
||
|
file,
|
||
|
"-s", ratio,
|
||
|
"-vcodec", "h264",
|
||
|
"-acodec", "aac",
|
||
|
encfile,
|
||
|
}
|
||
|
cmd := exec.Command(ffmpeg, args...)
|
||
|
|
||
|
err = cmd.Start()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = cmd.Wait()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
os.Rename(encfile, file)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func GetFileSize(label, filename string) int64 {
|
||
|
|
||
|
fh, err := os.OpenFile(filename, os.O_RDONLY, os.ModeTemporary)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
}
|
||
|
defer fh.Close()
|
||
|
|
||
|
fi, err := fh.Stat()
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
fmt.Printf("%s: %s (%s)\n", label, filename, utils.GetHumanSize(fi.Size()))
|
||
|
|
||
|
return fi.Size()
|
||
|
}
|
||
|
|
||
|
func ProcessAudio(URL, audiofile string) (err error) {
|
||
|
|
||
|
fmt.Printf("Fetching audio data into file '%s'... ", audiofile)
|
||
|
data, err := Dload(URL)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err = os.WriteFile(audiofile, data, 0600); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
fmt.Println("Complete!")
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func Merge(ffmpeg, videofile, audiofile, tmpfile string) (err error) {
|
||
|
|
||
|
if !fs.FileExists(ffmpeg) {
|
||
|
return fmt.Errorf("ffmpeg binary not found: %s", ffmpeg)
|
||
|
}
|
||
|
if !fs.FileExists(videofile) {
|
||
|
return fmt.Errorf("work file not found: %s", videofile)
|
||
|
}
|
||
|
if !fs.FileExists(audiofile) {
|
||
|
return fmt.Errorf("work file not found: %s", audiofile)
|
||
|
}
|
||
|
|
||
|
args := []string{
|
||
|
"-i",
|
||
|
videofile,
|
||
|
"-i",
|
||
|
audiofile,
|
||
|
"-c:v",
|
||
|
"copy",
|
||
|
"-c:a",
|
||
|
"aac",
|
||
|
tmpfile,
|
||
|
}
|
||
|
cmd := exec.Command(ffmpeg, args...)
|
||
|
err = cmd.Start()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = cmd.Wait()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|