前三个点位所有功能已调通

This commit is contained in:
2024-11-08 15:37:36 +08:00
parent 239d542edc
commit 660ae1326f
13 changed files with 331 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
package audio
package utils
import (
"bytes"
@@ -47,15 +47,19 @@ func get(u string) io.ReadCloser {
return resp.Body
}
// LinkAudio 链接音频,解析链接,直接提取为数据流
func LinkAudio(link string) (bgm io.ReadCloser) {
if link == "" {
return nil
}
u, err := url.Parse(link)
if err != nil {
log.Println("音频 URL 解析错误: ", err)
} else {
if u.Scheme == "file" {
bgm = open(u.String())
bgm = open(link)
} else if u.Scheme == "http" || u.Scheme == "https" {
bgm = get(u.String())
bgm = get(link)
} else {
log.Printf("不支持的音频文件协议: %v\n", u.String())
return

55
pkg/utils/link_video.go Normal file
View File

@@ -0,0 +1,55 @@
package utils
import (
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
)
// LinkVideo 链接视频,解析链接,网络文件会下载到临时目录并返回本地路径
func LinkVideo(link string) (local string) {
if link == "" {
return
}
u, err := url.Parse(link)
if err != nil {
log.Println("音频 URL 解析错误: ", err)
} else {
if u.Scheme == "file" {
local, _ = strings.CutPrefix(link, "file://")
} else if u.Scheme == "http" || u.Scheme == "https" {
p, _ := url.PathUnescape(u.EscapedPath())
tmpLocal := path.Join(os.TempDir(), path.Base(p))
err = Download(link, tmpLocal)
if err != nil {
log.Println("音频文件下载失败: ", err)
return
}
local = tmpLocal
} else {
log.Printf("不支持的视频链接协议: %v\n", u.String())
return
}
}
return
}
// Download 下载文件
func Download(link string, local string) (err error) {
resp, err := http.Get(link)
if err != nil {
return
}
defer resp.Body.Close()
f, err := os.OpenFile(local, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
return
}

View File

@@ -1 +1,47 @@
package video
import (
"bufio"
"context"
"io"
"log"
"os/exec"
"sync"
)
func Play(ctx context.Context, file string) error {
if file == "" {
log.Println("video file is empty")
return nil
}
cmd := exec.CommandContext(ctx, "ffplay", "-autoexit", "-fs", file)
pipe, err := cmd.StderrPipe()
if err != nil {
return err
}
var wait sync.WaitGroup
defer wait.Wait()
a := make(chan struct{})
defer close(a)
go func() {
wait.Add(1)
defer wait.Done()
for {
select {
case <-a:
default:
line, _, err := bufio.NewReader(pipe).ReadLine()
if err != nil {
if err == io.EOF {
return
}
break
}
log.Println(string(line))
}
}
}()
return cmd.Run()
}