forked from iawia002/lux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils/ffmpeg: integrate merge functions into a file [ci skip]
- Loading branch information
Showing
2 changed files
with
65 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func runMergeCmd(cmd *exec.Cmd, paths []string, mergeFilePath string) error { | ||
var stderr bytes.Buffer | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("%s\n%s", err, stderr.String()) | ||
} | ||
|
||
if mergeFilePath != "" { | ||
os.Remove(mergeFilePath) | ||
} | ||
// remove parts | ||
for _, path := range paths { | ||
os.Remove(path) | ||
} | ||
return nil | ||
} | ||
|
||
// MergeAudioAndVideo merge audio and video | ||
func MergeAudioAndVideo(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", "aac", "-strict", "experimental", | ||
mergedFilePath, | ||
) | ||
return runMergeCmd(exec.Command("ffmpeg", cmds...), paths, "") | ||
} | ||
|
||
// MergeToMP4 merge video parts to MP4 | ||
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))) | ||
} | ||
mergeFile.Close() | ||
|
||
cmd := exec.Command( | ||
"ffmpeg", "-y", "-f", "concat", "-safe", "-1", | ||
"-i", mergeFilePath, "-c", "copy", "-bsf:a", "aac_adtstoasc", mergedFilePath, | ||
) | ||
return runMergeCmd(cmd, paths, mergeFilePath) | ||
} |