2022-11-18 21:18:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-25 13:06:26 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2022-11-18 21:18:12 +00:00
|
|
|
"os"
|
2022-11-25 13:06:26 +00:00
|
|
|
"os/exec"
|
2022-11-18 21:18:12 +00:00
|
|
|
"path/filepath"
|
2022-11-25 13:06:26 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/davecheney/xattr"
|
|
|
|
"github.com/google/uuid"
|
2022-11-18 21:18:12 +00:00
|
|
|
)
|
|
|
|
|
2023-09-06 20:46:05 +00:00
|
|
|
//var httpClient = new(http.Client)
|
2022-11-25 13:06:26 +00:00
|
|
|
|
|
|
|
type Ambiance struct {
|
|
|
|
Id string
|
|
|
|
Title string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAmbiance(id string) (amb Ambiance, err error) {
|
|
|
|
fp := filepath.Join("./ambiance", id+".opus")
|
|
|
|
_, err = os.Stat(fp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
title, err := xattr.Getxattr(fp, "title")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ambiance{
|
|
|
|
Id: id,
|
|
|
|
Title: string(title),
|
|
|
|
Path: fp,
|
|
|
|
}, nil
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
func GetAmbiances() (amb []Ambiance, err error) {
|
2022-11-18 21:18:12 +00:00
|
|
|
files, err := os.ReadDir("./ambiance")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2022-11-25 13:06:26 +00:00
|
|
|
title, err := xattr.Getxattr(filepath.Join("./ambiance", file.Name()), "title")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
amb = append(amb, Ambiance{
|
|
|
|
Id: file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))],
|
|
|
|
Title: string(title),
|
|
|
|
Path: filepath.Join("./ambiance", file.Name()),
|
|
|
|
})
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddAmbiance(uri, title string) (Ambiance, error) {
|
|
|
|
var amb Ambiance
|
|
|
|
|
2023-09-06 20:46:05 +00:00
|
|
|
msg := make(map[string]interface{})
|
|
|
|
msg["event"] = "ambiance_add_start"
|
|
|
|
data := make(map[string]string)
|
|
|
|
data["name"] = title
|
|
|
|
msg["payload"] = data
|
|
|
|
ws_msg <- msg
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
msg = make(map[string]interface{})
|
|
|
|
msg["event"] = "ambiance_add_finish"
|
|
|
|
data = make(map[string]string)
|
|
|
|
data["name"] = title
|
|
|
|
msg["payload"] = data
|
|
|
|
ws_msg <- msg
|
|
|
|
}()
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
tmpfile, err := exec.Command("mktemp", "/tmp/dnd_XXXXXXXXXXXX.opus").Output()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpfile = tmpfile[:len(tmpfile)-1]
|
|
|
|
|
2023-09-06 20:46:05 +00:00
|
|
|
log.Printf("Parsing vid from %s", uri)
|
2022-11-25 13:06:26 +00:00
|
|
|
vid, err := YTUrl(uri)
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:46:05 +00:00
|
|
|
log.Printf("Start download with YTdl for %s", uri)
|
|
|
|
ytfile, err := NewYTdl(vid)
|
2022-11-25 13:06:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:46:05 +00:00
|
|
|
log.Printf("Start ffmpeg for %s", string(ytfile))
|
2022-11-25 13:06:26 +00:00
|
|
|
ff := exec.Command(
|
|
|
|
"ffmpeg",
|
|
|
|
"-y",
|
2023-09-06 20:46:05 +00:00
|
|
|
"-i", string(ytfile),
|
2022-11-25 13:06:26 +00:00
|
|
|
"-vn",
|
|
|
|
"-acodec", "copy",
|
|
|
|
"-movflags", "+faststart",
|
|
|
|
"-t", "01:00:00",
|
|
|
|
"-v", "error",
|
|
|
|
// "-stats",
|
|
|
|
"-progress", "pipe:1",
|
|
|
|
// "-af", "loudnorm=I=-16:LRA=11:TP=-1.5",
|
|
|
|
string(tmpfile),
|
|
|
|
)
|
|
|
|
|
|
|
|
ffprogress, err := ff.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ff.Stderr = os.Stderr
|
|
|
|
|
|
|
|
err = ff.Start()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Start ffmpeg to extract audio to %s", string(tmpfile))
|
2023-09-06 20:46:05 +00:00
|
|
|
|
|
|
|
msg = make(map[string]interface{})
|
2022-11-25 13:06:26 +00:00
|
|
|
msg["event"] = "ambiance_encode_start"
|
2023-09-06 20:46:05 +00:00
|
|
|
data = make(map[string]string)
|
2022-11-25 13:06:26 +00:00
|
|
|
data["name"] = title
|
|
|
|
msg["payload"] = data
|
|
|
|
ws_msg <- msg
|
|
|
|
|
|
|
|
msg = make(map[string]interface{})
|
|
|
|
msg["event"] = "ambiance_encode_progress"
|
|
|
|
data = make(map[string]string)
|
|
|
|
data["name"] = title
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(ffprogress)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
p := strings.Split(scanner.Text(), "=")
|
|
|
|
if len(p) == 2 {
|
|
|
|
data[p[0]] = strings.TrimSpace(p[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
prate.Do(func() {
|
|
|
|
msg["payload"] = data
|
|
|
|
ws_msg <- msg
|
|
|
|
})
|
|
|
|
}
|
2023-09-06 20:46:05 +00:00
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ff.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = make(map[string]interface{})
|
|
|
|
msg["event"] = "ambiance_encode_complete"
|
|
|
|
data = make(map[string]string)
|
|
|
|
data["name"] = title
|
|
|
|
msg["payload"] = data
|
|
|
|
ws_msg <- msg
|
|
|
|
|
|
|
|
id := uuid.New()
|
|
|
|
fn := filepath.Join("./ambiance", fmt.Sprintf("%s.opus", id.String()))
|
|
|
|
|
|
|
|
log.Printf("Moving to %s", fn)
|
|
|
|
|
|
|
|
in, err := os.Open(string(tmpfile))
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
of, err := os.Create(fn)
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(of, in)
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = of.Sync()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = of.Close()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = in.Close()
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 20:46:05 +00:00
|
|
|
err = os.Remove(string(ytfile))
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
err = os.Remove(string(tmpfile))
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Setting xattr")
|
|
|
|
err = xattr.Setxattr(fn, "title", []byte(title))
|
|
|
|
if err != nil {
|
|
|
|
return amb, err
|
|
|
|
}
|
|
|
|
|
|
|
|
amb.Id = id.String()
|
|
|
|
amb.Title = title
|
|
|
|
|
|
|
|
log.Println("Return info.")
|
|
|
|
return amb, nil
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|