-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg.go
44 lines (36 loc) · 1.18 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path"
"strings"
)
// FFMpeg is a wrapper around ffmpeg command line tool.
type FFMpeg struct{}
// NewFFMpeg creates a new FFMpeg instance.
func NewFFMpeg() *FFMpeg {
return &FFMpeg{}
}
// TranscodeMedia transcodes the media file at filePath to a format suitable for podcast items using following command:
// ffmpeg -i $filePath -c:a copy -vn $tempFile
func (svc *FFMpeg) TranscodeMedia(ctx context.Context, filePath string) (int64, error) {
ext := path.Ext(filePath)
tempFile := strings.TrimSuffix(filePath, ext) + ".tmp" + ext
defer os.Remove(tempFile)
out, err := exec.CommandContext(ctx, "ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", filePath, "-c:a", "copy", "-vn", tempFile).CombinedOutput()
if err != nil {
log.Println("ffmpeg responded with", string(out))
return 0, fmt.Errorf("failed to transcode file: %w", err)
}
fi, err := os.Stat(tempFile)
if err != nil {
return 0, fmt.Errorf("failed to get file info for %s: %w", tempFile, err)
}
if err := os.Rename(tempFile, filePath); err != nil {
return 0, fmt.Errorf("failed to rename %s to %s: %w", tempFile, filePath, err)
}
return fi.Size(), nil
}