-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_segment_extractor.go
84 lines (73 loc) · 2.78 KB
/
media_segment_extractor.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
// media_segment_extractor
// primarily generated by ChatGPT 4o
// with edits by Mike Quentel
package main
import (
"flag"
"fmt"
"github.com/u2takey/ffmpeg-go"
"log"
"path/filepath"
)
func main() {
inputFile := flag.String("input", "", "Input media file path (mp4 or mp3)")
outputFile := flag.String("output", "", "Output media file path (mp4 or mp3)")
startMark := flag.String("start", "00:00:00", "Start mark (format: HH:MM:SS)")
duration := flag.String("duration", "00:00:30", "Duration (format: HH:MM:SS)")
stripAudioFlag := flag.Bool("strip-audio", false, "Strip audio from input mp4 file")
addAudioFlag := flag.Bool("add-audio", false, "Add mp3 audio to input mp4 file")
audioFile := flag.String("audio", "", "Input mp3 audio file to add to mp4")
flag.Parse()
if *inputFile == "" || *outputFile == "" {
flag.Usage()
log.Fatal("input and output file paths are required")
}
inputExt := filepath.Ext(*inputFile)
outputExt := filepath.Ext(*outputFile)
if inputExt != outputExt {
log.Fatal("Input and output file extensions must match and be either .mp4 or .mp3")
}
if inputExt != ".mp4" && inputExt != ".mp3" {
log.Fatal("Unsupported file extension. Only .mp4 and .mp3 are supported")
}
if *stripAudioFlag {
if inputExt != ".mp4" {
log.Fatal("strip-audio option is only supported for .mp4 files")
}
fmt.Printf("Stripping audio from %s to %s\n", *inputFile, *outputFile)
err := stripAudio(*inputFile, *outputFile)
if err != nil {
log.Fatal(err)
}
log.Println("Audio stripped successfully.")
return
}
if *addAudioFlag {
if inputExt != ".mp4" || filepath.Ext(*audioFile) != ".mp3" {
log.Fatal("add-audio option requires an input mp4 file and an mp3 audio file")
}
fmt.Printf("Adding audio from %s to %s and saving as %s\n", *audioFile, *inputFile, *outputFile)
err := addAudioToVideo(*inputFile, *audioFile, *outputFile)
if err != nil {
log.Fatal(err)
}
log.Println("Audio added successfully.")
return
}
fmt.Printf("Extracting segment from %s to %s, starting at %s, for duration %s\n", *inputFile, *outputFile, *startMark, *duration)
err := ffmpeg_go.Input(*inputFile, ffmpeg_go.KwArgs{"ss": *startMark}).Output(*outputFile, ffmpeg_go.KwArgs{"t": *duration}).Run()
if err != nil {
log.Fatal(err)
}
log.Println("Media segment extracted successfully.")
}
func stripAudio(inputFile, outputFile string) error {
err := ffmpeg_go.Input(inputFile).Output(outputFile, ffmpeg_go.KwArgs{"an": ""}).Run()
return err
}
func addAudioToVideo(inputVideo, inputAudio, outputVideo string) error {
videoStream := ffmpeg_go.Input(inputVideo)
audioStream := ffmpeg_go.Input(inputAudio)
err := ffmpeg_go.Output([]*ffmpeg_go.Stream{videoStream, audioStream}, outputVideo, ffmpeg_go.KwArgs{"c:v": "copy", "c:a": "aac", "strict": "experimental"}).Run()
return err
}