Repository navigation
Expand file tree
/
Copy pathffmpeg.go
More file actions
204 lines (168 loc) · 6.75 KB
/
Copy pathffmpeg.go
File metadata and controls
204 lines (168 loc) · 6.75 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package GOFFMPEG
import (
"fmt"
"os"
)
var (
FFMPEG_TempDir string
)
func init() {
FFMPEG_TempDir = os.TempDir() + "/_ffmpeg_tmpfiles101"
err := createDirectoryIfNotExists(FFMPEG_TempDir)
if err != nil {
FFMPEG_TempDir = os.TempDir()
}
}
// FFmpeg represents the FFmpeg wrapper.
type FFmpeg struct {
ffmpegPath string // Path to the FFmpeg executable
}
// NewFFmpeg creates a new FFmpeg instance with the specified path.
func NewFFmpeg(ffmpegPath string) *FFmpeg {
return &FFmpeg{ffmpegPath: ffmpegPath}
}
// TranscodeVideo transcodes a video file to a different format.
// example: outputVideoPath,err := TranscodeVideo("/home/linuxsploit/demo.mp4", ".mkv)
func (f FFmpeg) TranscodeVideo(inputPath, OutPutFormatExtension string) (string, error) {
// Define the output file path
outputPath, err := createTempOutputFile(OutPutFormatExtension)
if err != nil {
return "", err
}
args := []string{"-i", inputPath, outputPath, "-y"}
return outputPath, f.Run(args)
}
// ExtractVideo extracts video stream from a video file with the specified output format extension and video codec options.
// It takes the input video file path, output file path with the desired extension (e.g., ".mp4", ".avi"), and a boolean flag to control video codec copying.
// It returns the path to the extracted video file and any encountered errors.
// Example: outputVideoPath, err := ExtractVideo("/home/linuxsploit/demo.mp4", ".mp4", false)
func (f FFmpeg) ExtractVideo(inputPath, OutPutFormatExtension string, copyVideoCodec bool) (string, error) {
// Define the output file path for the extracted video
outputVideo, err := createTempOutputFile(OutPutFormatExtension)
if err != nil {
return "", err
}
// Generate FFmpeg command to extract the video
args := []string{"-i", inputPath, "-an"} // "-an" to disable audio
// Add video codec options based on the boolean flag
if copyVideoCodec {
args = append(args, "-vcodec", "copy") // Copy video codec
}
args = append(args, outputVideo, "-y") // Output file path
// Run the FFmpeg command
return outputVideo, f.Run(args)
}
// ExtractAudio extracts audio stream from a video file with the specified output format extension and audio codec options.
// It takes the input video file path, output file path with the desired extension (e.g., ".mp3", ".ogg"), and a boolean flag to control audio codec copying.
// It returns the path to the extracted audio file and any encountered errors.
// example: outputVideoPath,err := ExtractAudio("/home/linuxsploit/demo.mp4",".mp3",false)
func (f FFmpeg) ExtractAudio(inputPath, OutPutFormatExtension string, copyAudioCodec bool) (string, error) {
// Define the output file path for the extracted audio
outputAudio, err := createTempOutputFile(OutPutFormatExtension)
if err != nil {
return "", err
}
// Generate FFmpeg command to extract the audio
args := []string{"-i", inputPath, "-vn"}
// Add audio codec options based on the boolean flag
if copyAudioCodec {
args = append(args, "-acodec", "copy") // Copy audio codec
}
args = append(args, outputAudio, "-y") // Output file path
// Run the FFmpeg command
return outputAudio, f.Run(args)
}
// ExtractVideoClip extracts a video clip from a specified time range in a video file.
// It takes the input video file path, start time in seconds, end time in seconds,
// and boolean flags to control video and audio options.
// It returns the path to the extracted video clip file and any encountered errors.
// example: ExtractVideoClip("/home/linuxsploit/demo.mp4", ".mp4", 25, 50, false, false)
func (f FFmpeg) ExtractVideoClip(inputPath, OutPutFormatExtension string, startSec, endSec float64, copyVideoCodec, disableAudio bool) (string, error) {
// Ensure startSec and endSec are valid time ranges
if startSec < 0 || endSec <= startSec {
return "", fmt.Errorf("invalid time range for video extraction")
}
// Define the output file path for the extracted video clip
outputVideo, err := createTempOutputFile(OutPutFormatExtension)
if err != nil {
return "", err
}
// Generate FFmpeg command to extract the video clip
args := []string{
"-i", inputPath,
"-ss", fmt.Sprintf("%f", startSec), // Start time
"-to", fmt.Sprintf("%f", endSec), // End time
}
// Add video and audio options based on the boolean flags
if copyVideoCodec {
args = append(args, "-c:v", "copy") // Copy video codec
}
if disableAudio {
args = append(args, "-an") // Disable audio
}
args = append(args, outputVideo, "-y") // Output file path
// Run the FFmpeg command
return outputVideo, f.Run(args)
}
// ExtractAudioClip extracts an audio clip from a specified time range in a video file.
// It takes the input video file path, start time in seconds, end time in seconds,
// and boolean flags to control video and audio options.
// It returns the path to the extracted audio clip file and any encountered errors.
// example: ExtractAudioClip("/home/linuxsploit/demo.mp4", ".mp3", 25, 50, false)
func (f FFmpeg) ExtractAudioClip(inputPath, OutPutFormatExtension string, startSec, endSec float64, copyAudioCodec bool) (string, error) {
// Ensure startSec and endSec are valid time ranges
if startSec < 0 || endSec <= startSec {
return "", fmt.Errorf("invalid time range for audio extraction")
}
// Define the output file path for the extracted audio clip
outputAudio, err := createTempOutputFile(OutPutFormatExtension)
if err != nil {
return "", err
}
// Generate FFmpeg command to extract the audio clip
args := []string{
"-i", inputPath,
"-ss", fmt.Sprintf("%f", startSec), // Start time
"-to", fmt.Sprintf("%f", endSec), // End time
"-vn", // Disable video
}
if copyAudioCodec {
args = append(args, "-c:a", "copy") // Copy audio codec
}
args = append(args, outputAudio, "-y") // Output file path
// Run the FFmpeg command
return outputAudio, f.Run(args)
}
// ConcatVideos concatenates multiple video files into a single video.
// []string videos should have same resolution.
// Example:
// ConcatVideos(
//
// []string{
// "/home/linuxsploit/demo1.mp4",
// "/home/linuxsploit/demo2.mp4",
// "/home/linuxsploit/demo3.mp4",
// },
// ".mkv",
// )
func (f FFmpeg) ConcatVideos(inputPaths []string, OutPutFormatExtension string) (string, error) {
// Define the output file path for the concatenated video
outputPath, err := createTempOutputFile(OutPutFormatExtension)
if err != nil {
return "", err
}
// Create a list of arguments for FFmpeg
args := []string{}
// Add the input files
for _, path := range inputPaths {
args = append(args, "-i", path)
}
// Add the filter_complex and output file options
args = append(args, "-filter_complex", "concat=n="+fmt.Sprint(len(inputPaths))+":v=1:a=1[outv][outa]", "-map", "[outv]", "-map", "[outa]", outputPath, "-y")
// Run the FFmpeg command
err = f.Run(args)
if err != nil {
return "", err
}
return outputPath, nil
}