2024-01-14 16:31:07 +06:00
|
|
|
package convertor
|
|
|
|
|
|
|
|
import (
|
2024-01-15 20:28:02 +06:00
|
|
|
"errors"
|
2024-01-14 16:31:07 +06:00
|
|
|
"os/exec"
|
2024-01-16 00:04:50 +06:00
|
|
|
"regexp"
|
2024-01-14 16:31:07 +06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServiceContract interface {
|
|
|
|
RunConvert(setting ConvertSetting) error
|
2024-01-15 20:28:02 +06:00
|
|
|
GetTotalDuration(file *File) (float64, error)
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
pathFFmpeg string
|
|
|
|
pathFFprobe string
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
Path string
|
|
|
|
Name string
|
|
|
|
Ext string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConvertSetting struct {
|
2024-01-15 20:28:02 +06:00
|
|
|
VideoFileInput *File
|
2024-01-14 16:31:07 +06:00
|
|
|
SocketPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConvertData struct {
|
|
|
|
totalDuration float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(pathFFmpeg string, pathFFprobe string) *Service {
|
|
|
|
return &Service{
|
|
|
|
pathFFmpeg: pathFFmpeg,
|
|
|
|
pathFFprobe: pathFFprobe,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) RunConvert(setting ConvertSetting) error {
|
|
|
|
//args := strings.Split("-report -n -c:v libx264", " ")
|
|
|
|
//args := strings.Split("-n -c:v libx264", " ")
|
|
|
|
//args = append(args, "-progress", "unix://"+setting.SocketPath, "-i", setting.VideoFileInput.Path, "file-out.mp4")
|
|
|
|
//args := "-report -n -i " + setting.VideoFileInput.Path + " -c:v libx264 -progress unix://" + setting.SocketPath + " output-file.mp4"
|
|
|
|
//args := "-n -i " + setting.VideoFileInput.Path + " -c:v libx264 -progress unix://" + setting.SocketPath + " output-file.mp4"
|
2024-01-16 00:15:45 +06:00
|
|
|
//args := "-y -i " + setting.VideoFileInput.Path + " -c:v libx264 -progress unix://" + setting.SocketPath + " output-file.mp4"
|
|
|
|
args := []string{"-y", "-i", setting.VideoFileInput.Path, "-c:v", "libx264", "-progress", "unix://" + setting.SocketPath, "output-file.mp4"}
|
|
|
|
cmd := exec.Command(s.pathFFmpeg, args...)
|
2024-01-14 16:31:07 +06:00
|
|
|
|
2024-01-16 00:04:50 +06:00
|
|
|
out, err := cmd.CombinedOutput()
|
2024-01-14 16:31:07 +06:00
|
|
|
if err != nil {
|
2024-01-16 00:04:50 +06:00
|
|
|
errStringArr := regexp.MustCompile("\r?\n").Split(strings.TrimSpace(string(out)), -1)
|
|
|
|
return errors.New(errStringArr[len(errStringArr)-1])
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:28:02 +06:00
|
|
|
func (s Service) GetTotalDuration(file *File) (duration float64, err error) {
|
|
|
|
args := []string{"-v", "error", "-select_streams", "v:0", "-count_packets", "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", file.Path}
|
|
|
|
cmd := exec.Command(s.pathFFprobe, args...)
|
2024-01-14 16:31:07 +06:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2024-01-15 20:28:02 +06:00
|
|
|
return 0, errors.New(strings.TrimSpace(string(out)))
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
|
|
|
|
}
|