Added the ability to convert files to different extensions.

This commit is contained in:
2024-03-07 22:18:35 +05:00
parent 1eb7ea4a93
commit 1ece1e443d
45 changed files with 1735 additions and 48 deletions

View File

@@ -1,8 +1,11 @@
package kernel
import (
"bufio"
"errors"
encoder2 "git.kor-elf.net/kor-elf/gui-for-ffmpeg/encoder"
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/helper"
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/kernel/encoder"
"io"
"os/exec"
"regexp"
@@ -20,6 +23,7 @@ type ConvertSetting struct {
VideoFileInput File
VideoFileOut File
OverwriteOutputFiles bool
Encoder encoder2.EncoderContract
}
type ConvertorContract interface {
@@ -30,6 +34,7 @@ type ConvertorContract interface {
ChangeFFmpegPath(path string) (bool, error)
ChangeFFprobePath(path string) (bool, error)
GetRunningProcesses() map[int]*exec.Cmd
GetSupportFormats() (encoder.ConvertorFormatsContract, error)
}
type ProgressContract interface {
@@ -68,7 +73,9 @@ func (s Convertor) RunConvert(setting ConvertSetting, progress ProgressContract)
if setting.OverwriteOutputFiles == true {
overwriteOutputFiles = "-y"
}
args := []string{overwriteOutputFiles, "-i", setting.VideoFileInput.Path, "-c:v", "libx264", "-progress", progress.GetProtocole(), setting.VideoFileOut.Path}
args := []string{overwriteOutputFiles, "-i", setting.VideoFileInput.Path}
args = append(args, setting.Encoder.GetParams()...)
args = append(args, "-progress", progress.GetProtocole(), setting.VideoFileOut.Path)
cmd := exec.Command(s.ffPathUtilities.FFmpeg, args...)
helper.PrepareBackgroundCommand(cmd)
@@ -115,7 +122,30 @@ func (s Convertor) GetTotalDuration(file *File) (duration float64, err error) {
}
return 0, err
}
return strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
frames := strings.TrimSpace(string(out))
if len(frames) == 0 {
return s.getAlternativeTotalDuration(file)
}
return strconv.ParseFloat(frames, 64)
}
func (s Convertor) getAlternativeTotalDuration(file *File) (duration float64, err error) {
args := []string{"-v", "error", "-select_streams", "a:0", "-count_packets", "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", file.Path}
cmd := exec.Command(s.ffPathUtilities.FFprobe, args...)
helper.PrepareBackgroundCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
errString := strings.TrimSpace(string(out))
if len(errString) > 1 {
return 0, errors.New(errString)
}
return 0, err
}
frames := strings.TrimSpace(string(out))
if len(frames) == 0 {
return 0, errors.New("error getting number of frames")
}
return strconv.ParseFloat(frames, 64)
}
func (s Convertor) GetFFmpegVesrion() (string, error) {
@@ -168,6 +198,45 @@ func (s Convertor) ChangeFFprobePath(path string) (bool, error) {
return true, nil
}
func (s Convertor) GetSupportFormats() (encoder.ConvertorFormatsContract, error) {
formats := encoder.NewConvertorFormats()
cmd := exec.Command(s.ffPathUtilities.FFmpeg, "-encoders")
stdOut, err := cmd.StdoutPipe()
if err != nil {
return formats, err
}
err = cmd.Start()
if err != nil {
return formats, err
}
scannerErr := bufio.NewReader(stdOut)
for {
line, _, err := scannerErr.ReadLine()
if err != nil {
if err == io.EOF {
break
}
continue
}
text := strings.Split(strings.TrimSpace(string(line)), " ")
encoderType := string(text[0][0])
if len(text) < 2 || (encoderType != "V" && encoderType != "A") {
continue
}
formats.NewEncoder(text[1])
}
err = cmd.Wait()
if err != nil {
return formats, err
}
return formats, nil
}
func (s Convertor) GetRunningProcesses() map[int]*exec.Cmd {
return s.runningProcesses.items
}