forked from bgrins/videoconverter.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
307 lines (266 loc) · 17.7 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>videoconverter.js - Convert Videos In Your Web Browser</title>
<meta name="description" content="Video Converter is a port of the FFmpeg project that allows audio/video conversion and processing in JavaScript.">
<meta name="author" content="Brian Grinstead, Aaron Marasco, and contributors">
<link rel="shortcut icon" href="site/favicon.ico">
<link href="site/site.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
<div class='content'>
<div class='inner-content'>
<div class="header clearfix">
<div class="pull-left">
<H1><a href='./'><img src='site/logo.png' width="30" /><span>videoconverter.js</span></a>
</H1>
</div>
<div class="pull-right links">
<a href='demo/'>demo</a>
<a href='#docs'>docs</a>
<a href='http://github.com/bgrins/videoconverter.js'>source code on github</a>
</div>
</div>
<div class="clearfix">
<p class="tagline">
videoconverter.js is a program that lets you <strong>process videos in your browser</strong>.
</p>
<p>
<hr />
<center>
<a class="big" href="demo/">View a Demonstration</a>
<a class="big" href="https://video-funhouse.herokuapp.com">View a Sample Application</a><br />
or <a href='http://github.com/bgrins/videoconverter.js'>view code on github</a>
</center>
<hr />
</p>
<h2 id="about">About <a href="#about">[#]</a></h2>
<p>
Videoconverter.js was originally conceived and implemented for a project in Node Knockout 2013 called <a href="http://2013.nodeknockout.com/teams/devcomo">Video Funhouse</a>.
</p>
<p>
The idea for the application was to try and convert any video file into another video format, while allowing filters to be applied to the video – <strong>all inside of the browser, without uploading anything</strong>. And to build it in a single weekend.
</p>
<p>
This is a huge task, and we knew that existing libraries like <a href="http://ffmpeg.org/">FFmpeg</a> would do a great job. But, FFmpeg is not written in JavaScript. Luckily, there is a project called <a href="https://github.com/kripken/emscripten">Emscripten</a>, which is an LLVM to JavaScript compiler, so we were able to compile FFmpeg into JavaScript.
</p>
<p>
Here is a video demonstrating the sample application we made with this library over the weekend:
</p>
<center>
<iframe src="//www.youtube.com/embed/TR_RGeRBroI" allowfullscreen="" frameborder="0" height="315" width="560"></iframe>
</center>
<h2 id="faq">FAQ <a href="#faq">[#]</a></h2>
<h3 id="why">Why?</h3>
<p>
Why would you compile FFmpeg into JavaScript? We were curious if it would work, and it seemed like a fun project.
<p>
<h3>Who?</h3>
<p>
Most of the grunt work has been done by <a href="https://twitter.com/bgrins">@bgrins</a> and <a href="https://twitter.com/aaronm67">@aaronm67</a>.
</p>
<h3>How Big is the JavaScript File?</h3>
<p>
The <a href="https://github.com/bgrins/videoconverter.js/blob/master/build/ffmpeg.js">ffmpeg.js</a> file is around <code>24.1 MB</code> or so. It ends up being around <code>6.1 MB gzipped</code>.
</p>
<p>
The <a href="https://github.com/bgrins/videoconverter.js/blob/master/build/ffmpeg-all-codecs.js">ffmpeg-all-codecs.js</a> file is around <code>27.5 MB</code> or so. It ends up being around <code>6.9 MB gzipped</code>.
</p>
<h3>Should I Use This?</h2>
<p>
Feel free to use this program, but keep in mind the following things:
</p>
<ul>
<li>
FFmpeg has <a href="http://www.ffmpeg.org/legal.html">license terms</a> that you must abide by. The default build in this project is LGPL 2.1, but including different codecs or build parameters when <a href="#build">building it yourself</a> can change this.
</li>
<li>
It is quite a large JavaScript file, and it will take a long time to load and evaluate.
</li>
<li>
We haven't yet thoroughly tested the performance - use it at your own risk.
</li>
</ul>
<h2 id="uses">Pontential Uses <a href="#uses">[#]</a></h2>
<h3>Audio / Video Editing and Conversion</h3>
<p>
This is what we are doing in the browser with <a href="https://video-funhouse.herokuapp.com">https://video-funhouse.herokuapp.com</a>. Obviously, this could be expanded and optimized. It is quite likely to bump up against performance bottlenecks - I <a href="http://www.briangrinstead.com/blog/video-funhouse">wrote about some of the issues we bumped into</a> if you are interested in more information.
</p>
<p>
Also, it may be possible to make this run on node, and distribute this as an npm module, to make setting up video conversions on a server or desktop much easier.
</p>
<h3>Benchmarking</h3>
<p>
It would be interesting to build a benchmark to compare performance of such a large application across different browsers, node.js, and native performance.
</p>
<h3>Processing Additional Codecs</h3>
<p>
This isn't yet compiled with support for additional codecs like zlib, x264, libvpx, etc. It should be possible to do.
</p>
<h2 id="contributing">Contributing <a href="#contributing">[#]</a></h2>
<p>
Want to help with this project? If you are interested, ping <a href="https://twitter.com/bgrins">@bgrins</a> or <a href="https://twitter.com/aaronm67">@aaronm67</a> on twitter or open an issue / pull request at <a href="https://github.com/bgrins/videoconverter.js">https://github.com/bgrins/videoconverter.js</a>.
</p>
<p>
Here are some ideas of things we'd like to do, and could use some help with:
</p>
<ul>
<li>
Have some experience with benchmarks or performance testing? We would like to benchmark FFmpeg performance in browser / node / native.
</li>
<li>
Know a bunch about <a href="https://github.com/kripken/emscripten">Emscripten</a> / <a href="http://asmjs.org/">asm.js</a>, or want to learn more? There should be ways to tweak the <a href="https://github.com/bgrins/videoconverter.js/blob/master/build/build_lgpl.sh">build script</a> and squeeze out some extra performance or trim down the size.
</li>
<li>
Good at building programs and linking libraries together? It would be cool to build in support for libraries like zlib, x264, and libvpx.
</li>
</ul>
<h2 id="docs">Usage Documentation <a href="#docs">[#]</a></h2>
<p class="note">
To see the code used in the terminal demo on this site, see <a href="https://github.com/bgrins/videoconverter.js/blob/master/demo/terminal.js">terminal.js</a> and <a href="https://github.com/bgrins/videoconverter.js/blob/master/demo/worker.js">worker.js</a> in the repository.
</p>
<p>
The only function exposed from the library is <code>ffmpeg_run</code>. It can be described by the following interface:
</p>
<!-- example-1 -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">var</span> results <span style="color: #666666">=</span> ffmpeg_run({
arguments<span style="color: #666666">:</span> [string],
files<span style="color: #666666">:</span> [
{
data<span style="color: #666666">:</span> UInt8Array,
name<span style="color: #666666">:</span> string
}
]
});
<span style="color: #408080; font-style: italic">// results is an Array of { data: UInt8Array, name: string }</span>
results.forEach(<span style="color: #008000; font-weight: bold">function</span>(file) {
console.log(<span style="color: #BA2121">"File recieved"</span>, file.name, file.data);
});
</pre></div>
<!-- /example-1 -->
<h3>Calling from a Worker</h3>
<p class="warning">
Note: while ffmpeg.js could be loaded directly from a <script> tag, it should be loaded from a <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers">Web Worker</a> to prevent blocking the main thread.
</p>
<p>
To load ffmpeg.js inside of a web worker, we just need to call <code>importScripts('ffmpeg.js');</code> from inside the worker. Following is a sample worker. Or, click to view a slightly bigger sample <a href="https://github.com/bgrins/videoconverter.js/blob/master/demo/worker.js">worker implementation</a> used in the <a href="demo/">demo</a>
</p>
<!-- example-2 -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">importScripts(<span style="color: #BA2121">'ffmpeg.js'</span>);
<span style="color: #008000; font-weight: bold">function</span> print(text) {
postMessage({
<span style="color: #BA2121">'type'</span> <span style="color: #666666">:</span> <span style="color: #BA2121">'stdout'</span>,
<span style="color: #BA2121">'data'</span> <span style="color: #666666">:</span> text
});
}
onmessage <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">function</span>(event) {
<span style="color: #008000; font-weight: bold">var</span> module <span style="color: #666666">=</span> {
files<span style="color: #666666">:</span> event.data.files <span style="color: #666666">||</span> [],
arguments<span style="color: #666666">:</span> event.data.arguments <span style="color: #666666">||</span> [],
print<span style="color: #666666">:</span> print,
printErr<span style="color: #666666">:</span> print
};
postMessage({
<span style="color: #BA2121">'type'</span> <span style="color: #666666">:</span> <span style="color: #BA2121">'start'</span>,
<span style="color: #BA2121">'data'</span> <span style="color: #666666">:</span> module.arguments
});
<span style="color: #008000; font-weight: bold">var</span> result <span style="color: #666666">=</span> ffmpeg_run(module);
postMessage({
<span style="color: #BA2121">'type'</span> <span style="color: #666666">:</span> <span style="color: #BA2121">'done'</span>,
<span style="color: #BA2121">'data'</span> <span style="color: #666666">:</span> result
});
};
postMessage({
<span style="color: #BA2121">'type'</span> <span style="color: #666666">:</span> <span style="color: #BA2121">'ready'</span>
});
</pre></div>
<!-- /example-2 -->
<p>
Then, from your page start up the worker:
</p>
<!-- example-3 -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">var</span> worker <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Worker(<span style="color: #BA2121">"worker.js"</span>);
worker.onmessage <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">function</span> (event) {
<span style="color: #008000; font-weight: bold">var</span> message <span style="color: #666666">=</span> event.data;
<span style="color: #008000; font-weight: bold">if</span> (message.type <span style="color: #666666">==</span> <span style="color: #BA2121">"ready"</span>) {
outputElement.textContent <span style="color: #666666">=</span> <span style="color: #BA2121">"Loaded"</span>;
worker.postMessage({
type<span style="color: #666666">:</span> <span style="color: #BA2121">'command'</span>,
arguments<span style="color: #666666">:</span> [<span style="color: #BA2121">'-help'</span>]
})
} <span style="color: #008000; font-weight: bold">else</span> <span style="color: #008000; font-weight: bold">if</span> (message.type <span style="color: #666666">==</span> <span style="color: #BA2121">"stdout"</span>) {
outputElement.textContent <span style="color: #666666">+=</span> message.data <span style="color: #666666">+</span> <span style="color: #BA2121">"\n"</span>;
} <span style="color: #008000; font-weight: bold">else</span> <span style="color: #008000; font-weight: bold">if</span> (message.type <span style="color: #666666">==</span> <span style="color: #BA2121">"start"</span>) {
outputElement.textContent <span style="color: #666666">=</span> <span style="color: #BA2121">"Worker has received command\n"</span>;
}
};
</pre></div>
<!-- /example-3 -->
<h3>Converting to and from UInt8Array</h3>
<p>
OK, but how do you get a UInt8Array from a file? There are a few different ways to do this.
If you want to read files from the user's computer, you could use a FileReader or FileReaderSync, and call <code>readAsArrayBuffer</code>. Here is a little wrapper for this functionality that allows drag/drop, file selection, and clipboard access: <a href="https://github.com/bgrins/filereader.js/">https://github.com/bgrins/filereader.js/</a>.
You could also fetch the data from the server, like so:
</p>
<!-- example-4 -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">var</span> sampleVideoData;
<span style="color: #008000; font-weight: bold">function</span> retrieveSampleVideo() {
<span style="color: #008000; font-weight: bold">var</span> oReq <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> XMLHttpRequest();
oReq.open(<span style="color: #BA2121">"GET"</span>, <span style="color: #BA2121">"bigbuckbunny.webm"</span>, <span style="color: #008000; font-weight: bold">true</span>);
oReq.responseType <span style="color: #666666">=</span> <span style="color: #BA2121">"arraybuffer"</span>;
oReq.onload <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">function</span> (oEvent) {
<span style="color: #008000; font-weight: bold">var</span> arrayBuffer <span style="color: #666666">=</span> oReq.response;
<span style="color: #008000; font-weight: bold">if</span> (arrayBuffer) {
sampleVideoData <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Uint8Array(arrayBuffer);
}
};
oReq.send(<span style="color: #008000; font-weight: bold">null</span>);
}
</pre></div>
<!-- /example-4 -->
<p>
And what can I do with the results? If you want to provide a download link or a preview of the output, you could convert it into a Blob using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob">Blob constructor</a>, and create an objectURL. Something like this:
</p>
<!-- example-5 -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">function</span> getDownloadLink(fileData, fileName) {
<span style="color: #008000; font-weight: bold">var</span> a <span style="color: #666666">=</span> <span style="color: #008000">document</span>.createElement(<span style="color: #BA2121">'a'</span>);
a.download <span style="color: #666666">=</span> fileName;
<span style="color: #008000; font-weight: bold">var</span> blob <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">new</span> Blob([fileData]);
<span style="color: #008000; font-weight: bold">var</span> src <span style="color: #666666">=</span> <span style="color: #008000">window</span>.URL.createObjectURL(blob);
a.href <span style="color: #666666">=</span> src;
a.textContent <span style="color: #666666">=</span> <span style="color: #BA2121">'Click here to download '</span> <span style="color: #666666">+</span> fileName <span style="color: #666666">+</span> <span style="color: #BA2121">"!"</span>;
<span style="color: #008000; font-weight: bold">return</span> a;
}
<span style="color: #008000; font-weight: bold">var</span> result <span style="color: #666666">=</span> ffmpeg_run(module);
result.forEach(<span style="color: #008000; font-weight: bold">function</span>(file) {
getDownloadLink(file.data, file.name);
});
</pre></div>
<!-- /example-5 -->
<h2 id="build">Build Documentation <a href="#build">[#]</a></h2>
<p>
Want to build the ffmpeg.js file for yourself? First, make sure you have Emscripten set up:
</p>
<pre>
git clone git@github.com:kripken/emscripten.git
</pre>
<p>
Depending on your system may need to also get the SDK to make sure Emscripten will work. The have <a href="http://kripken.github.io/emscripten-site/docs/getting_started/Tutorial.html">documentation on their site</a> about getting this to work.
</p>
<p>
Once this is all set up and <code>emcc</code> is on your path, you should be able to run:
</p>
<pre>
git clone git@github.com:bgrins/videoconverter.js.git
cd videoconverter.js/build
./build_lgpl.sh
</pre>
</div>
</div>
</div>
</div>
<script type='text/javascript' src='site/site.js'></script>
</body>
</html>