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-18 01:39:20 +06:00
|
|
|
GetFFmpegVesrion() (string, error)
|
|
|
|
GetFFprobeVersion() (string, error)
|
|
|
|
ChangeFFmpegPath(path string) (bool, error)
|
|
|
|
ChangeFFprobePath(path string) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type FFPathUtilities struct {
|
|
|
|
FFmpeg string
|
|
|
|
FFprobe string
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2024-01-18 01:39:20 +06:00
|
|
|
ffPathUtilities *FFPathUtilities
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
Path string
|
|
|
|
Name string
|
|
|
|
Ext string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConvertSetting struct {
|
2024-01-18 20:23:23 +06:00
|
|
|
VideoFileInput *File
|
|
|
|
VideoFileOut *File
|
|
|
|
SocketPath string
|
|
|
|
OverwriteOutputFiles bool
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConvertData struct {
|
|
|
|
totalDuration float64
|
|
|
|
}
|
|
|
|
|
2024-01-18 01:39:20 +06:00
|
|
|
func NewService(ffPathUtilities FFPathUtilities) *Service {
|
2024-01-14 16:31:07 +06:00
|
|
|
return &Service{
|
2024-01-18 01:39:20 +06:00
|
|
|
ffPathUtilities: &ffPathUtilities,
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) RunConvert(setting ConvertSetting) error {
|
2024-01-18 20:23:23 +06:00
|
|
|
overwriteOutputFiles := "-n"
|
|
|
|
if setting.OverwriteOutputFiles == true {
|
|
|
|
overwriteOutputFiles = "-y"
|
|
|
|
}
|
|
|
|
args := []string{overwriteOutputFiles, "-i", setting.VideoFileInput.Path, "-c:v", "libx264", "-progress", "unix://" + setting.SocketPath, setting.VideoFileOut.Path}
|
2024-01-18 01:39:20 +06:00
|
|
|
cmd := exec.Command(s.ffPathUtilities.FFmpeg, 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)
|
2024-01-16 18:08:50 +06:00
|
|
|
if len(errStringArr) > 1 {
|
|
|
|
return errors.New(errStringArr[len(errStringArr)-1])
|
|
|
|
}
|
|
|
|
return err
|
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}
|
2024-01-18 01:39:20 +06:00
|
|
|
cmd := exec.Command(s.ffPathUtilities.FFprobe, args...)
|
2024-01-14 16:31:07 +06:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2024-01-16 18:08:50 +06:00
|
|
|
errString := strings.TrimSpace(string(out))
|
|
|
|
if len(errString) > 1 {
|
|
|
|
return 0, errors.New(errString)
|
|
|
|
}
|
|
|
|
return 0, err
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
|
|
|
|
}
|
2024-01-18 01:39:20 +06:00
|
|
|
|
|
|
|
func (s Service) GetFFmpegVesrion() (string, error) {
|
|
|
|
cmd := exec.Command(s.ffPathUtilities.FFmpeg, "-version")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
text := regexp.MustCompile("\r?\n").Split(strings.TrimSpace(string(out)), -1)
|
|
|
|
return text[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) GetFFprobeVersion() (string, error) {
|
|
|
|
cmd := exec.Command(s.ffPathUtilities.FFprobe, "-version")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
text := regexp.MustCompile("\r?\n").Split(strings.TrimSpace(string(out)), -1)
|
|
|
|
return text[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) ChangeFFmpegPath(path string) (bool, error) {
|
|
|
|
cmd := exec.Command(path, "-version")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if strings.Contains(strings.TrimSpace(string(out)), "ffmpeg") == false {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
s.ffPathUtilities.FFmpeg = path
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) ChangeFFprobePath(path string) (bool, error) {
|
|
|
|
cmd := exec.Command(path, "-version")
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if strings.Contains(strings.TrimSpace(string(out)), "ffprobe") == false {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
s.ffPathUtilities.FFprobe = path
|
|
|
|
return true, nil
|
|
|
|
}
|