Skip to content

Commit bd4e10e

Browse files
authored
feat(data): add WebM audio support and improve constant naming (#1142)
**Because** - WebM is a widely supported audio format that can contain audio-only content - The existing codebase had inconsistent naming for WebM constants between audio and video types - Users need the ability to process and convert WebM audio files **This commit** - Adds `WEBM_AUDIO` constant and WebM audio support to the audio processing pipeline - Renames video WebM constant from `WEBM` to `WEB_VIDEO` for clarity - Updates all references throughout the codebase to use the new constants - Creates WebM audio test file and comprehensive unit tests covering: - WebM audio file loading and property extraction - Bidirectional audio conversion (to/from WebM format) - Integration with existing audio processing workflows - Fixes video test file references after constant renaming
1 parent 683de3a commit bd4e10e

File tree

9 files changed

+48
-35
lines changed

9 files changed

+48
-35
lines changed

pkg/component/ai/gemini/v0/common.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ var (
5151
}
5252

5353
geminiVideoFormats = []string{
54-
data.MP4, // MP4
55-
data.MPEG, // MPEG
56-
data.MOV, // MOV (video/quicktime)
57-
data.AVI, // AVI (video/x-msvideo)
58-
data.FLV, // FLV (video/x-flv)
59-
data.WEBM, // WEBM
60-
data.WMV, // WMV (video/x-ms-wmv)
54+
data.MP4, // MP4
55+
data.MPEG, // MPEG
56+
data.MOV, // MOV (video/quicktime)
57+
data.AVI, // AVI (video/x-msvideo)
58+
data.FLV, // FLV (video/x-flv)
59+
data.WEBMVIDEO, // WEBM
60+
data.WMV, // WMV (video/x-ms-wmv)
6161
}
6262

6363
convertibleVideoFormats = []string{

pkg/data/audio.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ type audioData struct {
2525
func (audioData) IsValue() {}
2626

2727
const (
28-
MP3 = "audio/mpeg"
29-
WAV = "audio/wav"
30-
AAC = "audio/aac"
31-
OGG = "audio/ogg"
32-
FLAC = "audio/flac"
33-
M4A = "audio/mp4"
34-
WMA = "audio/x-ms-wma"
35-
AIFF = "audio/aiff"
28+
MP3 = "audio/mpeg"
29+
WAV = "audio/wav"
30+
AAC = "audio/aac"
31+
OGG = "audio/ogg"
32+
FLAC = "audio/flac"
33+
M4A = "audio/mp4"
34+
WMA = "audio/x-ms-wma"
35+
AIFF = "audio/aiff"
36+
WEBMAUDIO = "audio/webm"
3637
)
3738

3839
var audioGetter = map[string]func(*audioData) (format.Value, error){
@@ -46,6 +47,7 @@ var audioGetter = map[string]func(*audioData) (format.Value, error){
4647
"m4a": func(a *audioData) (format.Value, error) { return a.Convert(M4A) },
4748
"wma": func(a *audioData) (format.Value, error) { return a.Convert(WMA) },
4849
"aiff": func(a *audioData) (format.Value, error) { return a.Convert(AIFF) },
50+
"webm": func(a *audioData) (format.Value, error) { return a.Convert(WEBMAUDIO) },
4951
}
5052

5153
func NewAudioFromBytes(b []byte, contentType, filename string, isUnified bool) (*audioData, error) {
@@ -63,6 +65,12 @@ func NewAudioFromURL(ctx context.Context, binaryFetcher external.BinaryFetcher,
6365
func createAudioData(b []byte, contentType, filename string, isUnified bool) (*audioData, error) {
6466
// Normalize MIME type first
6567
normalizedContentType := normalizeMIMEType(contentType)
68+
69+
// Special handling: if video/webm is passed for audio, convert to audio/webm
70+
if normalizedContentType == "video/webm" {
71+
normalizedContentType = WEBMAUDIO
72+
}
73+
6674
finalContentType := normalizedContentType
6775

6876
// If the audio should be unified, convert it to OGG (the internal unified audio format)

pkg/data/audio_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestNewAudioFromBytes(t *testing.T) {
3131
{"Valid M4A audio (non-standard MIME)", "small_sample.m4a", "audio/x-m4a", 1.0},
3232
{"Valid WMA audio", "small_sample.wma", "audio/x-ms-wma", 1.0},
3333
{"Valid AIFF audio", "small_sample.aiff", "audio/aiff", 1.0},
34+
{"Valid WebM audio", "small_sample_audio.webm", "audio/webm", 1.0},
3435
{"Invalid file type", "sample_640_426.png", "", 0.0},
3536
{"Invalid audio format", "", "", 0.0},
3637
{"Empty audio bytes", "", "", 0.0},
@@ -142,6 +143,9 @@ func TestAudioConvert(t *testing.T) {
142143
{"FLAC to OGG", "small_sample.flac", "audio/flac", "audio/ogg"},
143144
{"M4A to WAV", "small_sample.m4a", "audio/mp4", "audio/wav"},
144145
{"AIFF to MP3", "small_sample.aiff", "audio/aiff", "audio/mpeg"},
146+
{"WAV to WebM", "small_sample.wav", "audio/wav", "audio/webm"},
147+
{"WebM to MP3", "small_sample_audio.webm", "audio/webm", "audio/mpeg"},
148+
{"MP3 to WebM", "small_sample.mp3", "audio/mpeg", "audio/webm"},
145149
}
146150

147151
for _, tc := range testCases {

pkg/data/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func convertToAVIF(raw []byte, sourceContentType string) ([]byte, error) {
282282
}
283283

284284
func convertAudio(raw []byte, sourceContentType, targetContentType string) ([]byte, error) {
285-
supportedFormats := []string{MP3, WAV, AAC, OGG, FLAC, M4A, WMA, AIFF, OCTETSTREAM, "video/x-ms-asf", "video/mp4"}
285+
supportedFormats := []string{MP3, WAV, AAC, OGG, FLAC, M4A, WMA, AIFF, WEBMAUDIO, OCTETSTREAM, "video/x-ms-asf", "video/mp4"}
286286
if !slices.Contains(supportedFormats, sourceContentType) || !slices.Contains(supportedFormats, targetContentType) {
287287
return nil, fmt.Errorf("convert audio: unsupported format: source=%s, target=%s", sourceContentType, targetContentType)
288288
}
@@ -317,7 +317,7 @@ func convertAudio(raw []byte, sourceContentType, targetContentType string) ([]by
317317
}
318318

319319
func convertVideo(raw []byte, sourceContentType, targetContentType string) ([]byte, error) {
320-
supportedFormats := []string{MP4, AVI, MOV, WEBM, MKV, FLV, WMV, MPEG, OCTETSTREAM, "video/x-ms-asf"}
320+
supportedFormats := []string{MP4, AVI, MOV, WEBMVIDEO, MKV, FLV, WMV, MPEG, OCTETSTREAM, "video/x-ms-asf"}
321321
if !slices.Contains(supportedFormats, sourceContentType) || !slices.Contains(supportedFormats, targetContentType) {
322322
return nil, fmt.Errorf("convert video: unsupported format: source=%s, target=%s", sourceContentType, targetContentType)
323323
}
9.35 KB
Binary file not shown.
File renamed without changes.

pkg/data/utils.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,19 @@ func isAudioContentType(contentType string) bool {
143143
contentType == OGG ||
144144
contentType == FLAC ||
145145
contentType == M4A ||
146-
contentType == WMA
146+
contentType == WMA ||
147+
contentType == WEBMAUDIO
147148
}
148149

149150
func isVideoContentType(contentType string) bool {
150151
return contentType == MPEG ||
151152
contentType == AVI ||
152153
contentType == MOV ||
153-
contentType == WEBM ||
154154
contentType == MKV ||
155155
contentType == FLV ||
156156
contentType == WMV ||
157-
contentType == MP4
157+
contentType == MP4 ||
158+
contentType == WEBMVIDEO
158159
}
159160

160161
func isDocumentContentType(contentType string) bool {

pkg/data/video.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ type videoData struct {
2727
func (videoData) IsValue() {}
2828

2929
const (
30-
MP4 = "video/mp4"
31-
AVI = "video/x-msvideo"
32-
MOV = "video/quicktime"
33-
WEBM = "video/webm"
34-
MKV = "video/x-matroska"
35-
FLV = "video/x-flv"
36-
WMV = "video/x-ms-wmv"
37-
ASF = "video/x-ms-asf"
38-
MPEG = "video/mpeg"
30+
MP4 = "video/mp4"
31+
AVI = "video/x-msvideo"
32+
MOV = "video/quicktime"
33+
MKV = "video/x-matroska"
34+
FLV = "video/x-flv"
35+
WMV = "video/x-ms-wmv"
36+
ASF = "video/x-ms-asf"
37+
MPEG = "video/mpeg"
38+
WEBMVIDEO = "video/webm"
3939
)
4040

4141
var videoGetters = map[string]func(*videoData) (format.Value, error){
@@ -49,7 +49,7 @@ var videoGetters = map[string]func(*videoData) (format.Value, error){
4949
"wmv": func(v *videoData) (format.Value, error) { return v.Convert(WMV) },
5050
"asf": func(v *videoData) (format.Value, error) { return v.Convert(ASF) },
5151
"flv": func(v *videoData) (format.Value, error) { return v.Convert(FLV) },
52-
"webm": func(v *videoData) (format.Value, error) { return v.Convert(WEBM) },
52+
"webm": func(v *videoData) (format.Value, error) { return v.Convert(WEBMVIDEO) },
5353
}
5454

5555
func NewVideoFromBytes(b []byte, contentType, filename string, isUnified bool) (video *videoData, err error) {

pkg/data/video_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestNewVideoFromBytes(t *testing.T) {
2626
{"Valid MP4 video", "small_sample.mp4", "video/mp4", false},
2727
{"Valid MOV video", "small_sample.mov", "video/mp4", false},
2828
{"Valid AVI video", "small_sample.avi", "video/mp4", false},
29-
{"Valid WebM video", "small_sample.webm", "video/mp4", false},
29+
{"Valid WebM video", "small_sample_video.webm", "video/mp4", false},
3030
{"Valid MKV video", "small_sample.mkv", "video/mp4", false},
3131
{"Valid FLV video", "small_sample.flv", "video/mp4", false},
3232
{"Valid MPEG video", "small_sample.mpeg", "video/mp4", false},
@@ -114,7 +114,7 @@ func TestVideoProperties(t *testing.T) {
114114
{"MP4 video", "small_sample.mp4", "video/mp4", 320, 240, 1.0, 15.0},
115115
{"MOV video", "small_sample.mov", "video/quicktime", 320, 240, 1.0, 15.0},
116116
{"AVI video", "small_sample.avi", "video/x-msvideo", 320, 240, 1.0, 15.0},
117-
{"WebM video", "small_sample.webm", "video/webm", 320, 240, 1.0, 15.0},
117+
{"WebM video", "small_sample_video.webm", "video/webm", 320, 240, 1.0, 15.0},
118118
{"MKV video", "small_sample.mkv", "video/x-matroska", 320, 240, 1.0, 15.0},
119119
{"FLV video", "small_sample.flv", "video/x-flv", 320, 240, 1.0, 15.0},
120120
{"MPEG video", "small_sample.mpeg", "video/mpeg", 320, 240, 1.0, 25.0},
@@ -154,7 +154,7 @@ func TestVideoConvert(t *testing.T) {
154154
{"MP4 to WebM", "small_sample.mp4", "video/mp4", "video/webm"},
155155
{"MOV to MP4", "small_sample.mov", "video/quicktime", "video/mp4"},
156156
{"AVI to MP4", "small_sample.avi", "video/x-msvideo", "video/mp4"},
157-
{"WebM to MP4", "small_sample.webm", "video/webm", "video/mp4"},
157+
{"WebM to MP4", "small_sample_video.webm", "video/webm", "video/mp4"},
158158
{"MKV to WebM", "small_sample.mkv", "video/x-matroska", "video/webm"},
159159
{"FLV to MP4", "small_sample.flv", "video/x-flv", "video/mp4"},
160160
{"WMV to MP4", "small_sample.wmv", "video/x-ms-wmv", "video/mp4"},
@@ -213,7 +213,7 @@ func TestNewVideoFromBytesUnified(t *testing.T) {
213213
{"MP4 as unified", "small_sample.mp4", "video/mp4", 320, 240, 1.0, 15.0},
214214
{"MOV as unified", "small_sample.mov", "video/quicktime", 320, 240, 1.0, 15.0},
215215
{"AVI as unified", "small_sample.avi", "video/x-msvideo", 320, 240, 1.0, 15.0},
216-
{"WebM as unified", "small_sample.webm", "video/webm", 320, 240, 1.0, 15.0},
216+
{"WebM as unified", "small_sample_video.webm", "video/webm", 320, 240, 1.0, 15.0},
217217
{"MKV as unified", "small_sample.mkv", "video/x-matroska", 320, 240, 1.0, 15.0},
218218
{"FLV as unified", "small_sample.flv", "video/x-flv", 320, 240, 1.0, 15.0},
219219
{"MPEG as unified", "small_sample.mpeg", "video/mpeg", 320, 240, 1.0, 25.0},
@@ -291,7 +291,7 @@ func TestAllSupportedVideoFormats(t *testing.T) {
291291
{"MP4", "small_sample.mp4", "video/mp4", 320, 240, 1.0, 15.0},
292292
{"MOV", "small_sample.mov", "video/quicktime", 320, 240, 1.0, 15.0},
293293
{"AVI", "small_sample.avi", "video/x-msvideo", 320, 240, 1.0, 15.0},
294-
{"WebM", "small_sample.webm", "video/webm", 320, 240, 1.0, 15.0},
294+
{"WebM", "small_sample_video.webm", "video/webm", 320, 240, 1.0, 15.0},
295295
{"MKV", "small_sample.mkv", "video/x-matroska", 320, 240, 1.0, 15.0},
296296
{"FLV", "small_sample.flv", "video/x-flv", 320, 240, 1.0, 15.0},
297297
{"MPEG", "small_sample.mpeg", "video/mpeg", 320, 240, 1.0, 25.0},

0 commit comments

Comments
 (0)