-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuggest.go
327 lines (286 loc) · 7.79 KB
/
suggest.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"errors"
"fmt"
"github.com/snoby/go-ffprobe"
)
var (
media = new(Convert)
)
// Convert Contains the control block of what we will do with the
// conversion process
type Convert struct {
inFile string
outFile string
masterVideoStream *ffprobe.Stream
masterAudioStream *ffprobe.Stream
aacAudioStream *ffprobe.Stream
outVideo string
outAudio0 string
outAudio1 string
}
func checkforAACsecondaryAudio(fileStreams []*ffprobe.Stream) (streamIndex int, err error) {
// see if there are any ac3 5.1 surround sound streams we can use.
for _, stream := range fileStreams {
if stream.CodecType == "audio" {
if stream.Channels == 2 {
if stream.CodecName == "aac" {
streamIndex = stream.Index
fmt.Printf("Found a 2 channel aac stream")
return
//FOUND IT
}
}
}
} //end of search for master audio that we can just copy over and not transcode.
err = errors.New("Could not find secondary aac audio")
return -1, err
}
//
// Take a list of audio streams and suggest the best one
// to use as a master to transcode from.
// TODO ADD a config for preferred language
//
// in: a list of streams
// out. an ffproble stream that is the audio master
func masterAudio(fileStreams []*ffprobe.Stream) (streamIndex int, err error) {
/*
*
* This may not be the most efficient way to do it, but these files are small
*
*/
// see if there are any ac3 5.1 surround sound streams we can use.
for _, stream := range fileStreams {
if stream.CodecType == "audio" {
if stream.Channels > 2 {
if stream.CodecName == "ac3" {
streamIndex = stream.Index
return
//FOUND IT
}
}
}
} //end of search for master audio that we can just copy over and not transcode.
//
// Search for another mutli channel stream we can use. It will require us to transcode it to ac3
// but at least it's multi channel. Also it doesn't matter if we multiple streams like
// 1 AAC Multi channel & 1 DTS Mutlichannel, at the end we just need one of these.
//
for _, stream := range fileStreams {
if stream.CodecType == "audio" {
if stream.Channels > 2 {
switch stream.CodecName {
case "truehd":
fallthrough
case "aac":
fallthrough
case "dca":
fallthrough
case "dts":
streamIndex = stream.Index
return
case "eac3":
streamIndex = stream.Index
return
} // end of switch
} // end of multi channel
}
}
//
//
// At this point there is only 2 channel audio available, AAC is perferred, but if we have another type we
// can use it in a pinch
//
//
for _, stream := range fileStreams {
if stream.CodecType == "audio" {
if stream.Channels == 2 {
switch stream.CodecName {
case "aac":
fallthrough
case "ac3":
streamIndex = stream.Index
return
} // end of switch
} // end of multi channel
}
}
//
// If we get to this point we are struggling
//
//
for _, stream := range fileStreams {
if stream.CodecType == "audio" {
if stream.Channels == 2 {
switch stream.CodecName {
case "truehd":
fallthrough
case "mp3":
streamIndex = stream.Index
return
case "eac3":
streamIndex = stream.Index
return
case "dts":
streamIndex = stream.Index
return
} // end of switch
} // end of multi channel
}
}
err = errors.New("Could not find an audio stream to use as master")
return
}
//
//
// Find the masterVideo stream. At this point it's usually just
// the only video stream, but may need to add code here for the
// situation where we have more than one video
//
//
func masterVideo(fileStreams []*ffprobe.Stream) (streamIndex int, err error) {
for _, stream := range fileStreams {
if stream.CodecType == "video" {
streamIndex := stream.Index
return streamIndex, nil
}
}
err = errors.New("Could not find a video stream to use as master")
return
}
func (media *Convert) print() {
fmt.Println("***")
fmt.Printf("Primary Video Stream (%s)\n", media.masterVideoStream.CodecName)
fmt.Printf("Primary Audio Stream (%s)\n", media.masterAudioStream.CodecName)
fmt.Printf("----------Planned Output-----------------\n")
fmt.Printf("Video Stream (%s) operation [%s]\n", "h264", media.outVideo)
fmt.Printf("Primary Audio Stream (%s) operation [%s]\n", "aac", media.outAudio0)
fmt.Printf("Second Audio Stream (%s) operation [%s]\n", "ac3", media.outAudio1)
}
//
// Adding a method
//
func (media *Convert) setupAudioConversion(fileStreams []*ffprobe.Stream) {
if media.masterAudioStream.Channels == 2 {
//
// Can't surround sound with this.
//
switch media.masterAudioStream.CodecName {
case "aac":
media.outAudio0 = "copy"
media.outAudio1 = "none"
case "ac3":
fmt.Println("Found ac3 Master audio...")
stream, err := checkforAACsecondaryAudio(fileStreams)
if err != nil {
media.outAudio0 = "convert"
media.outAudio1 = "none"
} else {
//Not really sure how this can happen
media.aacAudioStream = fileStreams[stream]
media.outAudio0 = "copy"
}
media.outAudio1 = "none"
case "dts":
media.outAudio0 = "convert"
media.outAudio1 = "none"
case "mp3":
media.outAudio0 = "convert"
media.outAudio1 = "none"
case "eac3":
media.outAudio0 = "convert"
media.outAudio1 = "none"
default:
fmt.Printf("Not sure what to do with this codec: %s", media.masterAudioStream.CodecName)
} // end of switch
} else {
// The Master Audio has surround sound
switch media.masterAudioStream.CodecName {
case "ac3":
stream, err := checkforAACsecondaryAudio(fileStreams)
if err != nil {
// This means we didn't find an aac alternate
media.outAudio0 = "convert"
media.outAudio1 = "copy"
} else {
// we found the aac 2 channel stream
media.aacAudioStream = fileStreams[stream]
media.outAudio0 = "copy"
media.outAudio1 = "copy"
}
case "aac":
fallthrough
case "truehd":
fallthrough
case "dca":
fallthrough
case "dts":
media.outAudio0 = "convert"
media.outAudio1 = "convert"
case "eac3":
media.outAudio0 = "convert"
media.outAudio1 = "convert"
default:
fmt.Printf("Not sure what to do with this codec: %s", media.masterAudioStream.CodecName)
} // end of switch
} //end of if channels > 2
}
//
// Adding a method
//
func (media *Convert) setupVideoConversion() {
//
// Can't surround sound with this.
//
switch media.masterVideoStream.CodecName {
case "h264":
media.outVideo = "copy"
default:
media.outVideo = "convert"
} // end of switch
}
//
//
// Find the best audio and video streams in the container
// Then make a suggestion on what to do with the streams
// to make a compatible mp4 file for the appleTV.
//
//
func suggestConvSettings(in string) {
println("Input filename:", in)
h := ffprobe.File(in)
fileFormat, err = h.Format()
if err != nil {
fmt.Println("This file format doesn't seem to be known, exiting")
fmt.Println(err)
return
}
fileStreams, err = h.Streams()
if err != nil {
fmt.Println(err)
return
}
media.inFile = in
fmt.Printf(" Information about file: %s \n", fileFormat.Filename)
fmt.Printf(" Number of Streams: %d \n", fileFormat.NBStreams)
fmt.Printf(" File has duration: %s (s)\n", fileFormat.Duration)
masterVideoInx, err := masterVideo(fileStreams)
if err != nil {
fmt.Printf("%s\n", err)
return
}
Videostream := fileStreams[masterVideoInx]
masterAudioInx, err := masterAudio(fileStreams)
if err != nil {
fmt.Printf("%s\n", err)
return
}
Audiostream := fileStreams[masterAudioInx]
media.masterVideoStream = Videostream
media.masterAudioStream = Audiostream
fmt.Printf("Master Video codec: %s \n", Videostream.CodecName)
fmt.Printf("Master Audio codec: %s numChannels:%d\n", Audiostream.CodecName, Audiostream.Channels)
media.setupAudioConversion(fileStreams)
media.setupVideoConversion()
media.print()
}