-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathffmpeg.go
89 lines (85 loc) · 1.76 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
)
type convertedMedia struct {
os.FileInfo
Id string
Path string
}
// mux uses ffmpeg to merge the video from one file with the audio from another.
// The command used:
//
// ffmpeg -y -loglevel repeat+info \
// -i file:video.mp4 \
// -i file:audio.mp4 \
// -c copy -map 0:v:0 -map 1:a:0 file:output.mp4
func mux(video, audio, output, cwd string) error {
cmd := exec.Command(
"ffmpeg",
"-y",
"-loglevel",
"repeat+info",
"-i",
"file:"+video,
"-i",
"file:"+audio,
"-c",
"copy",
"-map",
"0:v:0",
"-map",
"1:a:0",
"file:"+output,
)
cmd.Dir = cwd
// TODO: log cmd.Stdout to ffmpeg.log file
cmd.Stderr = os.Stderr
log.Printf("\t[RUN] %v", cmd.Args)
log.Printf("%v", cmd.Dir)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed ffmpeg mux: %v", cmd.Stderr)
}
return nil
}
// compress handles file conversion and formatting with ffmpeg.
// Discord allows a maximum 8MB file upload, so this is only
// required if the size limit is exceeded.
func compress(f media) (*convertedMedia, error) {
var crf = 40
//cmd := exec.Command(
// "ffmpeg",
// "-i "+f.Name(),
// " -c:v libvpx ",
// fmt.Sprintf("-crf %v ", crf),
// "-b:v 1M ",
// "-c:a libvorbis ",
// f.Id+convertedExt,
//)
cmd := exec.Command(
"ffmpeg",
fmt.Sprintf("-i %v -c:v libvpx -crf %v -b:v 1M -c:a libvorbis %v",
f.Name(), crf, f.Id+convertedExt),
)
cmd.Dir = path.Join(dir, f.Id)
err := cmd.Run()
if err != nil {
log.Fatalf("Failed: %v\n", cmd.Args)
}
cm := convertedMedia{
FileInfo: nil,
Id: f.Id,
Path: "",
}
cm.FileInfo, err = os.Stat(cm.Path)
if err != nil {
log.Print("Error finding formatted file")
}
// TODO want to return output error from ffmpeg
return &cm, nil
}