-
Notifications
You must be signed in to change notification settings - Fork 0
/
mms_transcode.go
527 lines (443 loc) · 14.7 KB
/
mms_transcode.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gabriel-vasile/mimetype"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
ffmpeg "github.com/u2takey/ffmpeg-go"
"go.mongodb.org/mongo-driver/bson/primitive"
"image"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
_ "image/gif"
_ "image/jpeg"
)
// Size limits
const (
maxImageSize = 0.5 * 1024 * 1024 // 1 MB
maxFileSize = 0.5 * 1024 * 1024 // 5 MB
)
func (s *MM4Server) transcodeMedia() {
for {
mm4Message := <-s.MediaTranscodeChan
transId := primitive.NewObjectID().Hex()
ff, err := mm4Message.processAndConvertFiles()
if err != nil {
mm4Message.Files = nil
mm4Message.Content = nil // remove content to be safe
mm4Message.Client.Password = "***"
// todo add log privacy
var lm = s.gateway.LogManager
lm.SendLog(lm.BuildLog(
"Server.MM4.TranscodeMedia",
"Failed to transcode media. %s",
logrus.ErrorLevel,
map[string]interface{}{
"mm4Message": mm4Message,
"logID": transId,
}, err,
))
continue
}
mm4Message.Files = ff
msgItem := MsgQueueItem{
To: mm4Message.To,
From: mm4Message.From,
ReceivedTimestamp: time.Now(),
Type: MsgQueueItemType.MMS,
Files: mm4Message.Files,
LogID: transId,
}
s.gateway.Router.ClientMsgChan <- msgItem
}
}
// List of compatible MIME types
var compatibleTypes = map[string]bool{
"image/jpeg": true, "image/jpg": true, "image/gif": true, "image/png": true,
"audio/basic": true, "audio/L24": true, "audio/mp4": true, "audio/mpeg": true,
"audio/ogg": true, "audio/vnd.rn-realaudio": true, "audio/vnd.wave": true,
"audio/3gpp": true, "audio/3gpp2": true, "audio/ac3": true, "audio/webm": true,
"audio/amr-nb": true, "audio/amr": true, "audio/aac": true, "audio/ogg; codecs=opus": true,
"video/mpeg": true, "video/mp4": true, "video/quicktime": true, "video/webm": true,
"video/3gpp": true, "video/3gpp2": true, "video/H264": true,
"application/pdf": true, "application/msword": true,
"application/vnd.ms-excel": true, "application/vnd.ms-powerpoint": true,
"application/vnd.openxmlformats-officedocument.presentationml.presentation": true,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": true,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": true,
}
// processAndConvertFiles processes and converts files as needed.
func (m *MM4Message) processAndConvertFiles() ([]MsgFile, error) {
var processedFiles []MsgFile
for _, file := range m.Files {
if strings.Contains(file.ContentType, "application/smil") {
processedFiles = append(processedFiles, file)
continue
}
// Step 1: Decode the Base64 content to get raw data
decodedContent, err := decodeBase64(file.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode Base64 content: %v", err)
}
// Step 2: Detect content type if it's octet-stream or unknown
if strings.Contains(file.ContentType, "application/octet-stream") || file.ContentType == "" {
detectedType := detectMIMEType(decodedContent)
file.ContentType = detectedType
}
var convertedContent []byte
var newType string
// Step 3: Process files based on their content type
switch {
case strings.HasPrefix(file.ContentType, "image/"):
if strings.Contains(file.ContentType, "jpeg") || strings.Contains(file.ContentType, "jpg") {
// Compress JPEG images to fit under 1MB
convertedContent, err = compressJPEG(decodedContent, int(maxImageSize))
if err != nil {
return nil, fmt.Errorf("failed to compress JPEG: %v", err)
}
newType = "image/jpeg"
} else if strings.Contains(file.ContentType, "png") {
// Compress PNG images to fit under 1MB
convertedContent, err = compressPNG(decodedContent, int(maxImageSize))
if err != nil {
return nil, fmt.Errorf("failed to compress PNG: %v", err)
}
newType = "image/png"
} else {
// Convert other image formats to PNG and compress
convertedContent, newType, err = convertImageToPNG(decodedContent)
if err != nil {
return nil, fmt.Errorf("failed to convert image to PNG: %v", err)
}
convertedContent, err = compressPNG(convertedContent, int(maxImageSize))
if err != nil {
return nil, fmt.Errorf("failed to compress converted PNG: %v", err)
}
}
case strings.HasPrefix(file.ContentType, "video/"):
// Compress video content and convert to 3GPP format
convertedContent, newType, err = processVideoContent(decodedContent)
if err != nil {
return nil, fmt.Errorf("failed to process video: %v", err)
}
case strings.HasPrefix(file.ContentType, "audio/"):
// Compress audio content to fit under 5MB
convertedContent, newType, err = convertToMP3(decodedContent)
if err != nil {
return nil, fmt.Errorf("failed to convert audio: %v", err)
}
default:
// Compress other file types to fit under 5MB
convertedContent, err = compressFile(decodedContent, int(maxFileSize))
if err != nil {
return nil, fmt.Errorf("failed to compress file: %v", err)
}
newType = file.ContentType
}
// Step 4: Update the processed file
file.Content = []byte(encodeToBase64(convertedContent))
file.ContentType = newType
file.Base64Data = encodeToBase64(convertedContent)
processedFiles = append(processedFiles, file)
}
return processedFiles, nil
}
// convertTo3GPP compresses and converts video content to 3GPP format suitable for MMS transmission.
func convertTo3GPP(content []byte, transcodeVideo, transcodeAudio bool) ([]byte, error) {
// Determine temporary file path
tempPath := os.Getenv("TRANSCODE_TEMP_PATH")
if tempPath == "" {
tempPath = os.TempDir() // Use OS temp directory as fallback
}
// Generate unique file names for input and output
inputFile := filepath.Join(tempPath, uuid.New().String()+".mp4")
outputFile := filepath.Join(tempPath, uuid.New().String()+".3gp") // Use .3gp extension for 3GPP format
// Save the input content to a temporary file
err := ioutil.WriteFile(inputFile, content, 0644)
if err != nil {
return nil, fmt.Errorf("failed to write temporary input file: %v", err)
}
defer os.Remove(inputFile) // Ensure cleanup
// Build FFmpeg command
ffmpegCmd := ffmpeg.Input(inputFile)
// Apply video filters if transcoding is required
if transcodeVideo {
// Apply scale and pad filters to maintain aspect ratio and fit into 176x144
ffmpegCmd = ffmpegCmd.Filter("scale", ffmpeg.Args{"w=176", "h=144", "force_original_aspect_ratio=decrease"}).Filter(
"pad",
ffmpeg.Args{"w=176", "h=144", "x=(ow-iw)/2", "y=(oh-ih)/2"},
)
}
// Prepare output arguments
outputArgs := ffmpeg.KwArgs{
"f": "3gp", // Output format
}
// Set video codec options
if transcodeVideo {
outputArgs["c:v"] = "h263" // Use H.263 codec for compatibility
outputArgs["b:v"] = "128k" // Lower video bitrate for smaller size
outputArgs["maxrate"] = "128k" // Limit max bitrate
outputArgs["bufsize"] = "256k" // Buffer size for rate control
outputArgs["r"] = "12" // Reduce frame rate to 12 FPS
} else {
outputArgs["c:v"] = "copy"
}
// Set audio codec options
if transcodeAudio {
outputArgs["c:a"] = "amr_nb" // Use AMR-NB codec for MMS compatibility
outputArgs["b:a"] = "12.2k" // Lower audio bitrate
outputArgs["ar"] = "8000" // Set audio sample rate to 8000 Hz
} else {
outputArgs["c:a"] = "copy"
}
// Add output to command
ffmpegCmd = ffmpegCmd.Output(outputFile, outputArgs)
// Capture FFmpeg's stderr output for debugging
var stderr bytes.Buffer
err = ffmpegCmd.OverWriteOutput().ErrorToStdOut().WithErrorOutput(&stderr).Run()
if err != nil {
return nil, fmt.Errorf("FFmpeg processing failed: %v\nFFmpeg stderr:\n%s", err, stderr.String())
}
defer os.Remove(outputFile) // Ensure cleanup
// Read the processed output file
processedContent, err := ioutil.ReadFile(outputFile)
if err != nil {
return nil, fmt.Errorf("failed to read temporary output file: %v", err)
}
// Validate file size (600 KB limit for MMS)
if len(processedContent) > int(maxFileSize) {
return nil, fmt.Errorf("compressed video file exceeds size limit of %.2f KB", float64(maxFileSize)/1024)
}
return processedContent, nil
}
// detectMIMEType detects the actual MIME type of the content.
func detectMIMEType(content []byte) string {
mimeType := mimetype.Detect(content)
if mimeType != nil {
return mimeType.String()
}
return "application/octet-stream"
}
// convertImageToPNG converts an image to PNG format.
func convertImageToPNG(content []byte) ([]byte, string, error) {
img, _, err := image.Decode(bytes.NewReader(content))
if err != nil {
return nil, "", fmt.Errorf("failed to decode image: %v", err)
}
var buf bytes.Buffer
err = png.Encode(&buf, img)
if err != nil {
return nil, "", fmt.Errorf("failed to encode image as PNG: %v", err)
}
return buf.Bytes(), "image/png", nil
}
// processVideoContent converts video content if needed.
func processVideoContent(content []byte) ([]byte, string, error) {
_, _, err := detectCodecs(content)
if err != nil {
return nil, "", err
}
/*transcodeVideo := videoCodec != "h264"
transcodeAudio := audioCodec != "aac"
if !transcodeVideo && !transcodeAudio {
return content, "video/3gpp", nil
}*/
data, err := convertTo3GPP(content, true, false)
return data, "video/3gpp", err
}
// compressJPEG compresses JPEG images to be under 1MB.
func compressJPEG(content []byte, maxSize int) ([]byte, error) {
img, err := jpeg.Decode(bytes.NewReader(content))
if err != nil {
return nil, fmt.Errorf("failed to decode JPEG: %v", err)
}
var buf bytes.Buffer
quality := 80
for {
buf.Reset()
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
if err != nil {
return nil, fmt.Errorf("failed to encode JPEG: %v", err)
}
if buf.Len() <= maxSize || quality < 10 {
break
}
quality -= 10 // Gradually reduce quality if size is too large
}
return buf.Bytes(), nil
}
// compressPNG compresses PNG images using lower compression levels to be under 1MB.
func compressPNG(content []byte, maxSize int) ([]byte, error) {
img, err := png.Decode(bytes.NewReader(content))
if err != nil {
return nil, fmt.Errorf("failed to decode PNG: %v", err)
}
var buf bytes.Buffer
err = png.Encode(&buf, img)
if err != nil {
return nil, fmt.Errorf("failed to encode PNG: %v", err)
}
// Check if the output is larger than the allowed limit (1MB)
if buf.Len() > maxSize {
return nil, fmt.Errorf("PNG image exceeds size limit")
}
return buf.Bytes(), nil
}
// compressFile compresses any other file type to be under the specified max size.
func compressFile(content []byte, maxSize int) ([]byte, error) {
if len(content) <= maxSize {
return content, nil
}
pr, pw := io.Pipe()
prOut, pwOut := io.Pipe()
defer pr.Close()
defer pwOut.Close()
go func() {
_, _ = pw.Write(content)
_ = pw.Close()
}()
var outputBuffer bytes.Buffer
ffmpegCmd := ffmpeg.Input("pipe:0").
Output("pipe:1", ffmpeg.KwArgs{"c:v": "libx264", "crf": 28, "preset": "slow"}).
WithInput(pr).
WithOutput(pwOut).
OverWriteOutput().
Run()
go func() {
_ = ffmpegCmd
_ = pwOut.Close()
}()
_, _ = io.Copy(&outputBuffer, prOut)
if outputBuffer.Len() > maxSize {
return nil, fmt.Errorf("file exceeds size limit after compression")
}
return outputBuffer.Bytes(), nil
}
// detectCodecs probes the input content to determine its codecs.
func detectCodecs(content []byte) (string, string, error) {
tmpFile, err := ioutil.TempFile("", "probe-*")
if err != nil {
return "", "", err
}
defer os.Remove(tmpFile.Name())
_, err = tmpFile.Write(content)
if err != nil {
return "", "", err
}
tmpFile.Close()
data, err := ffmpeg.Probe(tmpFile.Name())
if err != nil {
return "", "", err
}
type StreamInfo struct {
Streams []struct {
CodecType string `json:"codec_type"`
CodecName string `json:"codec_name"`
} `json:"streams"`
}
var info StreamInfo
err = json.Unmarshal([]byte(data), &info)
if err != nil {
return "", "", err
}
var videoCodec, audioCodec string
for _, stream := range info.Streams {
if stream.CodecType == "video" {
videoCodec = stream.CodecName
} else if stream.CodecType == "audio" {
audioCodec = stream.CodecName
}
}
return videoCodec, audioCodec, nil
}
// convertToMP4 compresses and converts video content to MP4 format using ffmpeg.
func convertToMP4(content []byte, transcodeVideo, transcodeAudio bool) ([]byte, error) {
pr, pw := io.Pipe()
prOut, pwOut := io.Pipe()
go func() {
_, _ = pw.Write(content)
_ = pw.Close()
}()
var outputBuffer bytes.Buffer
ffmpegCmd := ffmpeg.Input("pipe:0")
// Set video transcoding options with compression
if transcodeVideo {
ffmpegCmd = ffmpegCmd.Output("pipe:1", ffmpeg.KwArgs{
"c:v": "libx264",
"crf": 30, // Higher CRF value for better compression
"preset": "veryfast",
"maxrate": "1M",
"bufsize": "2M",
})
} else {
ffmpegCmd = ffmpegCmd.Output("pipe:1", ffmpeg.KwArgs{"c:v": "copy"})
}
// Set audio transcoding options with compression
if transcodeAudio {
ffmpegCmd = ffmpegCmd.Output("pipe:1", ffmpeg.KwArgs{
"c:a": "aac",
"b:a": "96k", // Lower bitrate for audio compression
})
} else {
ffmpegCmd = ffmpegCmd.Output("pipe:1", ffmpeg.KwArgs{"c:a": "copy"})
}
go func() {
_ = ffmpegCmd.WithInput(pr).WithOutput(pwOut).OverWriteOutput().Run()
_ = pwOut.Close()
}()
_, _ = io.Copy(&outputBuffer, prOut)
// Check if the output is larger than the allowed limit (5MB)
if outputBuffer.Len() > maxFileSize {
return nil, fmt.Errorf("compressed video file exceeds size limit of 5MB")
}
return outputBuffer.Bytes(), nil
}
// convertToMP3 compresses and converts audio content to MP3 format using ffmpeg.
func convertToMP3(content []byte) ([]byte, string, error) {
pr, pw := io.Pipe()
prOut, pwOut := io.Pipe()
go func() {
_, _ = pw.Write(content)
_ = pw.Close()
}()
var outputBuffer bytes.Buffer
go func() {
err := ffmpeg.Input("pipe:0").
Output("pipe:1", ffmpeg.KwArgs{
"c:a": "libmp3lame",
"b:a": "128k", // Set bitrate for compression
"ar": "44100",
}).
WithInput(pr).
WithOutput(pwOut).
OverWriteOutput().
Run()
if err != nil {
fmt.Printf("FFmpeg error: %v\n", err)
}
_ = pwOut.Close()
}()
_, _ = io.Copy(&outputBuffer, prOut)
// Check if the output is larger than the allowed limit (5MB)
if outputBuffer.Len() > maxFileSize {
return nil, "", fmt.Errorf("compressed audio file exceeds size limit of 5MB")
}
return outputBuffer.Bytes(), "audio/mp3", nil
}
// encodeToBase64 converts raw bytes to Base64.
func encodeToBase64(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}
// decodeBase64 decodes a Base64 string.
func decodeBase64(encodedContent []byte) ([]byte, error) {
return base64.StdEncoding.DecodeString(string(encodedContent))
}