forked from iawia002/lux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg.go
59 lines (51 loc) 路 1.5 KB
/
ffmpeg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package utils
import (
"bytes"
"fmt"
"os"
"os/exec"
"github.com/pkg/errors"
)
func runMergeCmd(cmd *exec.Cmd, paths []string, mergeFilePath string) error {
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return errors.Errorf("%s\n%s", err, stderr.String())
}
if mergeFilePath != "" {
os.Remove(mergeFilePath) // nolint
}
// remove parts
for _, path := range paths {
os.Remove(path) // nolint
}
return nil
}
// MergeFilesWithSameExtension merges files that have the same extension into one.
// Can also handle merging audio and video.
func MergeFilesWithSameExtension(paths []string, mergedFilePath string) error {
cmds := []string{
"-y",
}
for _, path := range paths {
cmds = append(cmds, "-i", path)
}
cmds = append(cmds, "-c:v", "copy", "-c:a", "copy", mergedFilePath)
return runMergeCmd(exec.Command("ffmpeg", cmds...), paths, "")
}
// MergeToMP4 merges video parts to an MP4 file.
func MergeToMP4(paths []string, mergedFilePath string, filename string) error {
mergeFilePath := filename + ".txt" // merge list file should be in the current directory
// write ffmpeg input file list
mergeFile, _ := os.Create(mergeFilePath)
for _, path := range paths {
mergeFile.Write([]byte(fmt.Sprintf("file '%s'\n", path))) // nolint
}
mergeFile.Close() // nolint
cmd := exec.Command(
"ffmpeg", "-y", "-f", "concat", "-safe", "0",
"-i", mergeFilePath, "-c", "copy", "-bsf:a", "aac_adtstoasc", mergedFilePath,
)
return runMergeCmd(cmd, paths, mergeFilePath)
}