2024-01-14 16:31:07 +06:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"ffmpegGui/convertor"
|
|
|
|
myError "ffmpegGui/error"
|
|
|
|
"fmt"
|
|
|
|
"fyne.io/fyne/v2/widget"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConvertorHandler struct {
|
|
|
|
convertorService convertor.ServiceContract
|
|
|
|
convertorView convertor.ViewContract
|
|
|
|
errorView myError.ViewContract
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConvertorHandler(
|
|
|
|
convertorService convertor.ServiceContract,
|
|
|
|
convertorView convertor.ViewContract,
|
|
|
|
errorView myError.ViewContract,
|
|
|
|
) *ConvertorHandler {
|
|
|
|
return &ConvertorHandler{
|
|
|
|
convertorService,
|
|
|
|
convertorView,
|
|
|
|
errorView,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h ConvertorHandler) GetConvertor() {
|
|
|
|
h.convertorView.Main(h.runConvert, h.getSockPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h ConvertorHandler) getSockPath(file convertor.File, progressbar *widget.ProgressBar) (string, error) {
|
|
|
|
totalDuration, err := h.getTotalDuration(file)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
progressbar.Value = 0
|
|
|
|
progressbar.Max = totalDuration
|
|
|
|
progressbar.Show()
|
|
|
|
progressbar.Refresh()
|
|
|
|
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
sockFileName := path.Join(os.TempDir(), fmt.Sprintf("%d_sock", rand.Int()))
|
|
|
|
l, err := net.Listen("unix", sockFileName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
re := regexp.MustCompile(`frame=(\d+)`)
|
|
|
|
fd, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("accept error:", err)
|
|
|
|
}
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
data := ""
|
|
|
|
progress := 0.0
|
|
|
|
for {
|
|
|
|
_, err := fd.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data += string(buf)
|
|
|
|
a := re.FindAllStringSubmatch(data, -1)
|
|
|
|
if len(a) > 0 && len(a[len(a)-1]) > 0 {
|
2024-01-14 17:25:35 +06:00
|
|
|
c, err := strconv.Atoi(a[len(a)-1][len(a[len(a)-1])-1])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-14 16:31:07 +06:00
|
|
|
progress = float64(c)
|
|
|
|
}
|
|
|
|
if strings.Contains(data, "progress=end") {
|
|
|
|
progress = totalDuration
|
|
|
|
}
|
2024-01-14 17:25:35 +06:00
|
|
|
if progressbar.Value != progress {
|
2024-01-14 16:31:07 +06:00
|
|
|
progressbar.Value = progress
|
|
|
|
progressbar.Refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sockFileName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h ConvertorHandler) runConvert(setting convertor.HandleConvertSetting) error {
|
|
|
|
return h.convertorService.RunConvert(
|
|
|
|
convertor.ConvertSetting{
|
|
|
|
VideoFileInput: setting.VideoFileInput,
|
|
|
|
SocketPath: setting.SocketPath,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h ConvertorHandler) getTotalDuration(file convertor.File) (float64, error) {
|
|
|
|
return h.convertorService.GetTotalDuration(file)
|
|
|
|
}
|