-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaudiotranscode.html
138 lines (116 loc) · 2.96 KB
/
audiotranscode.html
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
<!DOCTYPE html>
<html>
<head>
<title>Transcode audio</title>
</head>
<body>
<p>Drag and drop a file in to transcode...</p>
<p>I take no credit or blame for this file or its functionality :) -- see <a href=http://crbug.com/569089>http://crbug.com/569089</a> for context.<p>
<script>
"use strict";
var audioContext = null;
function OnFileDropped(e)
{
e.preventDefault();
var files = e.dataTransfer.files;
if (files.length)
Transcode(files[0]);
};
function OnDragOver(e)
{
e.preventDefault();
};
document.addEventListener("DOMContentLoaded", function () {
window.ondrop = OnFileDropped;
window.ondragover = OnDragOver;
audioContext = new AudioContext();
});
function Transcode(file)
{
Decode(file).then(Encode).then(function (blob)
{
var url = URL.createObjectURL(blob);
InvokeDownload(url, "transcoded.ogg");
});
};
function Decode(file)
{
console.log("Starting decode of file: ", file);
return ConvertFileToArrayBuffer(file).then(function (arraybuffer)
{
return new Promise(function (resolve, reject)
{
audioContext.decodeAudioData(arraybuffer, resolve, reject);
});
});
};
function Encode(buffer)
{
return new Promise(function (resolve, reject)
{
console.log("Starting encode of buffer: ", buffer);
var bufferSource = audioContext.createBufferSource();
bufferSource.buffer = buffer;
var streamDest = audioContext.createMediaStreamDestination();
bufferSource.connect(streamDest);
var mimeType = "audio/webm";
var mediaRecorder;
// Try to handle both kinds of constructor used in recent spec versions.
try {
mediaRecorder = new MediaRecorder(streamDest.stream, { "mimeType": mimeType });
}
catch (e)
{
mediaRecorder = null;
}
if (!mediaRecorder)
{
mediaRecorder = new MediaRecorder(streamDest.stream, mimeType);
}
var chunks = [];
mediaRecorder.ondataavailable = function (e)
{
chunks.push(e.data);
};
mediaRecorder.onstop = function (e)
{
var blob = new Blob(chunks, { "type": mimeType });
console.log("Encode finished, " + chunks.length + " chunks, " + blob.size + " bytes");
resolve(blob);
};
mediaRecorder.onerror = reject;
bufferSource.onended = function () {
mediaRecorder.stop();
};
mediaRecorder.start(1);
bufferSource.start();
});
};
function ConvertFileToArrayBuffer(file)
{
return new Promise(function (resolve, reject)
{
var reader = new FileReader();
reader.onload = function ()
{
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
};
function InvokeDownload(url, filename)
{
var a = document.createElement("a");
var body = document.getElementsByTagName("body")[0];
a.textContent = filename;
a.href = url;
a["download"] = filename;
body.appendChild(a);
var clickEvent = new MouseEvent("click");
a.dispatchEvent(clickEvent);
body.removeChild(a);
};
</script>
</body>
</html>