WEBVTT
Kind: captions
Language: en-US

00:00:00.320 --> 00:00:03.920
Have you ever wondered what would happen&nbsp;
if you converted an Excel spreadsheet to&nbsp;&nbsp;

00:00:03.920 --> 00:00:08.960
an audio file? Or an MP4 video to a&nbsp;
PDF document? Or maybe there's some&nbsp;&nbsp;

00:00:08.960 --> 00:00:14.080
other exotic combination worth trying out,&nbsp;
but I guess we'll never know, because most&nbsp;&nbsp;

00:00:14.080 --> 00:00:19.680
online file converters only let you select&nbsp;
formats in the same medium. Images to images,&nbsp;&nbsp;

00:00:19.680 --> 00:00:24.720
videos to videos, text documents to...&nbsp;
fancier text documents, and so on.

00:00:24.720 --> 00:00:30.960
What if there was a service that could natively&nbsp;
convert a Portal 2 demo file to a Flash animation,&nbsp;&nbsp;

00:00:30.960 --> 00:00:36.720
for example, and did so completely locally?&nbsp;
All of these online tools upload your files&nbsp;&nbsp;

00:00:36.720 --> 00:00:41.200
to a server which does all the processing.&nbsp;
I'm sure you can imagine why that's terrible&nbsp;&nbsp;

00:00:41.200 --> 00:00:46.000
for privacy. In fact, I cannot stress&nbsp;
this enough – uploading personal files&nbsp;&nbsp;

00:00:46.000 --> 00:00:50.720
to a random internet server is one of the&nbsp;
most careless things that we've normalized.

00:00:50.720 --> 00:00:55.120
So I'm imagining a website that&nbsp;
can convert anything to anything,&nbsp;&nbsp;

00:00:55.120 --> 00:00:58.640
and locally, in your own browser.

00:00:58.640 --> 00:01:03.120
These are the main two goals that I set out&nbsp;
with to build my own file converter. If you&nbsp;&nbsp;

00:01:03.120 --> 00:01:07.280
don't care about how I did it, here's the link&nbsp;
– you can go and try it for yourself. Though,&nbsp;&nbsp;

00:01:07.280 --> 00:01:10.640
bit of a heads-up, there is a reason&nbsp;
why no one has done this before,&nbsp;&nbsp;

00:01:10.640 --> 00:01:13.200
and that's what I'll be&nbsp;
talking about in this video.

00:01:13.200 --> 00:01:17.920
Specifically, the elephant in the room is no&nbsp;
doubt the promise of converting between two&nbsp;&nbsp;

00:01:17.920 --> 00:01:24.560
completely different mediums. Like, what would&nbsp;
it even mean to convert an MP4 video to a PDF?

00:01:24.560 --> 00:01:29.440
Well, as you might already know, videos&nbsp;
consist of multiple sequential frames.&nbsp;&nbsp;

00:01:29.440 --> 00:01:34.640
And a PDF document consists of multiple&nbsp;
sequential pages. When you frame it like that,&nbsp;&nbsp;

00:01:34.640 --> 00:01:39.200
the process becomes pretty clear – you extract&nbsp;
the video's frames into a series of images,&nbsp;&nbsp;

00:01:39.200 --> 00:01:43.600
and then build a PDF file with&nbsp;
pages containing those images.

00:01:43.600 --> 00:01:49.200
But here, I want you to notice that we're&nbsp;
actually dealing with two problems at once – sure,&nbsp;&nbsp;

00:01:49.200 --> 00:01:55.120
we have to convert between file formats like MP4,&nbsp;
PNG and PDF, but we also have to convert between&nbsp;&nbsp;

00:01:55.120 --> 00:02:01.360
different forms of media, like digital images&nbsp;
to document pages. For videos and documents,&nbsp;&nbsp;

00:02:01.360 --> 00:02:05.360
this is pretty straightforward. But things do&nbsp;
get a bit more complicated when you're converting&nbsp;&nbsp;

00:02:05.360 --> 00:02:11.360
images to audio, for example. Even though you&nbsp;
might support the JPEG format and the MP3 format,&nbsp;&nbsp;

00:02:11.360 --> 00:02:16.480
that doesn't make it any easier to&nbsp;
figure out how an image becomes sound.

00:02:16.480 --> 00:02:18.880
Let's start with the easier problem for now,&nbsp;&nbsp;

00:02:18.880 --> 00:02:24.000
which is that we need to support as many&nbsp;
file formats as possible. Now, remember,&nbsp;&nbsp;

00:02:24.000 --> 00:02:29.760
this is supposed to be a website that does all&nbsp;
of the computation on the client's computer.&nbsp;&nbsp;

00:02:29.760 --> 00:02:33.840
That makes things a bit more challenging than&nbsp;
usual, because it means we can't just spin up&nbsp;&nbsp;

00:02:33.840 --> 00:02:40.080
VLC to decode a video, for example. The tools&nbsp;
we use have to be compatible with a web browser.

00:02:40.080 --> 00:02:45.040
Take audiovisual media as an example. The&nbsp;
most popular utilities for such formats are:&nbsp;&nbsp;

00:02:45.040 --> 00:02:50.400
FFmpeg for video and audio, and ImageMagick&nbsp;
for images. If you've never heard of them,&nbsp;&nbsp;

00:02:50.400 --> 00:02:55.440
you might be surprised to find that they're&nbsp;
the backbone of many media-focused services.

00:02:55.440 --> 00:03:01.200
Even YouTube uses FFmpeg for transcoding&nbsp;
video, at least sometimes. Nowadays,&nbsp;&nbsp;

00:03:01.200 --> 00:03:04.800
they do of course have proprietary&nbsp;
tech to do the more common stuff.

00:03:04.800 --> 00:03:09.280
While ImageMagick and FFmpeg are usually&nbsp;
native computer programs meant to run on&nbsp;&nbsp;

00:03:09.280 --> 00:03:15.520
a desktop PC or a server, people have ported&nbsp;
them to WebAssembly – a technology that makes&nbsp;&nbsp;

00:03:15.520 --> 00:03:20.640
it possible to run them in a browser.&nbsp;
This alone is a great starting point.&nbsp;&nbsp;

00:03:20.640 --> 00:03:25.440
Just these two would already make for a&nbsp;
competent file converter for images and videos,&nbsp;&nbsp;

00:03:25.440 --> 00:03:30.800
even supporting some truly exotic formats.&nbsp;
FFmpeg can even read Bink, which is the&nbsp;&nbsp;

00:03:30.800 --> 00:03:36.800
video format used in most Valve games. But&nbsp;
I of course have no plans of stopping here.

00:03:36.800 --> 00:03:42.720
Let's also not forget that the average browser&nbsp;
already has some built-in media capabilities. I&nbsp;&nbsp;

00:03:42.720 --> 00:03:47.120
mean, how else would it decode the video you're&nbsp;
watching? Of course, in the majority of cases,&nbsp;&nbsp;

00:03:47.120 --> 00:03:53.600
our specialized tools do a much better job. But&nbsp;
still, the built-in stuff can be useful sometimes.

00:03:53.600 --> 00:03:57.360
For example, the WebAssembly port&nbsp;
of ImageMagick doesn't come with&nbsp;&nbsp;

00:03:57.360 --> 00:04:03.360
support for WebP images. However,&nbsp;
most browsers can handle WebP. So,&nbsp;&nbsp;

00:04:03.360 --> 00:04:08.400
I wrote a little script that renders the WebP&nbsp;
on an invisible canvas, and then effectively&nbsp;&nbsp;

00:04:08.400 --> 00:04:15.360
screenshots that to turn it into a PNG. That&nbsp;
PNG can then be fed directly into ImageMagick,&nbsp;&nbsp;

00:04:15.360 --> 00:04:21.280
so I've basically added support for an&nbsp;
extra format with no additional cost.

00:04:21.280 --> 00:04:25.360
Another thing that browsers are good&nbsp;
at is rendering documents. I mean,&nbsp;&nbsp;

00:04:25.360 --> 00:04:30.880
think about it – why would I include a separate&nbsp;
tool for parsing webpages or vector graphics if&nbsp;&nbsp;

00:04:30.880 --> 00:04:37.200
that's literally what your browser does all the&nbsp;
time? I can take the contents of an HTML document,&nbsp;&nbsp;

00:04:37.200 --> 00:04:41.360
embed it in an SVG image using&nbsp;
the foreignObject tag, and then,&nbsp;&nbsp;

00:04:41.360 --> 00:04:46.800
just like before, put it on an invisible&nbsp;
canvas and screenshot it into an image.

00:04:46.800 --> 00:04:52.560
But, of course, when I say "document", you&nbsp;
probably aren't thinking of a webpage. You're&nbsp;&nbsp;

00:04:52.560 --> 00:04:57.760
probably thinking of Office documents, like&nbsp;
those of Microsoft Word, PowerPoint, Excel,&nbsp;&nbsp;

00:04:57.760 --> 00:05:03.600
and so on. Unfortunately, there doesn't seem&nbsp;
to be an all-in-one solution for parsing these&nbsp;&nbsp;

00:05:03.600 --> 00:05:08.880
on a browser. Sure, there are one-off tools&nbsp;
specialized in handling one format at a time,&nbsp;&nbsp;

00:05:08.880 --> 00:05:14.720
and there are projects that glue them together,&nbsp;
but that quickly becomes a bloated mess.

00:05:14.720 --> 00:05:18.960
On its own, this could've been brushed aside&nbsp;
as an inconvenience, but that's still just the&nbsp;&nbsp;

00:05:18.960 --> 00:05:23.920
beginning. While there are options for all the&nbsp;
major Microsoft document formats, there aren't&nbsp;&nbsp;

00:05:23.920 --> 00:05:29.920
as many for the OpenDocument alternatives. And,&nbsp;
concerning the tools that do exist, the most&nbsp;&nbsp;

00:05:29.920 --> 00:05:37.120
popular Word document-to-HTML parser "Mammoth"&nbsp;
converts a document like this... to this.

00:05:37.120 --> 00:05:42.960
It's not hard to spot the difference. And, sure,&nbsp;
I could patch it to add support for formatting,&nbsp;&nbsp;

00:05:42.960 --> 00:05:48.160
but Mammoth was never designed to do that. Its&nbsp;
purpose is just to translate the document's&nbsp;&nbsp;

00:05:48.160 --> 00:05:52.160
structure, and I'd be fighting the&nbsp;
tool if I wanted authentic output.

00:05:52.160 --> 00:05:57.360
The only other reasonable alternative seemed to&nbsp;
be including yet another commonly used open-source&nbsp;&nbsp;

00:05:57.360 --> 00:06:03.840
media parser as a WebAssembly program. However,&nbsp;
that would likely mean bundling LibreOffice,&nbsp;&nbsp;

00:06:03.840 --> 00:06:08.400
and as far as I can tell, this one's&nbsp;
not as mature as the rest. It probably&nbsp;&nbsp;

00:06:08.400 --> 00:06:13.680
would give accurate results, but I was more&nbsp;
worried about the size and loading times.

00:06:13.680 --> 00:06:20.080
So, naturally, when the tools you want don't&nbsp;
seem to exist, you go ahead and make your own.&nbsp;&nbsp;

00:06:20.640 --> 00:06:26.080
Yeah, I took a little bit of a detour&nbsp;
to work on a standalone Office-to-HTML&nbsp;&nbsp;

00:06:26.080 --> 00:06:32.640
parser in pure JavaScript. In retrospect,&nbsp;
this was probably a bad use of my time.

00:06:32.640 --> 00:06:34.640
There are three major steps to this:&nbsp;&nbsp;

00:06:34.640 --> 00:06:38.720
extracting the ZIP, parsing the XML,&nbsp;
and rendering the document itself.

00:06:38.720 --> 00:06:43.600
The first one is exactly what it sounds like –&nbsp;
a modern Microsoft Office or OpenDocument file&nbsp;&nbsp;

00:06:43.600 --> 00:06:50.080
is just a renamed ZIP file. Extracting ZIP files&nbsp;
is a fairly common task, so I didn't really feel&nbsp;&nbsp;

00:06:50.080 --> 00:06:55.120
like reinventing the wheel on this one. That&nbsp;
said, the existing libraries come with a bunch&nbsp;&nbsp;

00:06:55.120 --> 00:07:00.080
of stuff that I do not need, like support for&nbsp;
creating ZIP files, which I never plan to do,&nbsp;&nbsp;

00:07:00.080 --> 00:07:06.000
and support for decompression algorithms which&nbsp;
I will never use. So instead, I got Claude to&nbsp;&nbsp;

00:07:06.000 --> 00:07:11.360
implement the bare minimum, so that I could focus&nbsp;
more on the important aspects, like parsing XML.

00:07:11.360 --> 00:07:16.720
XML stands for "Extensible Markup Language",&nbsp;
and it's basically a somewhat human-readable&nbsp;&nbsp;

00:07:16.720 --> 00:07:22.480
data structure used for all sorts of things. When&nbsp;
you "extract" a Word document, these are the files&nbsp;&nbsp;

00:07:22.480 --> 00:07:28.080
you'll find hidden within. They are what describe&nbsp;
the document's entire layout and formatting.

00:07:28.080 --> 00:07:32.880
Now, if you've ever seen HTML, you might&nbsp;
notice that XML looks very similar.&nbsp;&nbsp;

00:07:32.880 --> 00:07:37.760
Most browsers actually do support parsing&nbsp;
XML data, but I wanted to make sure that&nbsp;&nbsp;

00:07:37.760 --> 00:07:43.520
this code can be used outside of a browser too.&nbsp;
Luckily, writing an XML parser is not at all&nbsp;&nbsp;

00:07:43.520 --> 00:07:47.760
difficult to do. My implementation&nbsp;
came in at about 70 lines of code,&nbsp;&nbsp;

00:07:47.760 --> 00:07:53.120
and while it probably won't work for everything,&nbsp;
it serves its purpose in my specific use case.

00:07:53.120 --> 00:07:58.720
Now all that's left is to parse the actual&nbsp;
document. I will say, the OpenDocument stuff&nbsp;&nbsp;

00:07:58.720 --> 00:08:04.880
is so much nicer to work with than Microsoft's&nbsp;
format. It often translates directly to HTML,&nbsp;&nbsp;

00:08:04.880 --> 00:08:11.440
with very little need for additional logic. And,&nbsp;
the components are directly reusable – you can&nbsp;&nbsp;

00:08:11.440 --> 00:08:16.160
pretty much just throw a presentation into&nbsp;
the text document parser and it'll almost&nbsp;&nbsp;

00:08:16.160 --> 00:08:21.360
kinda work. OpenDocument files also come&nbsp;
with a thumbnail image, so if my parser&nbsp;&nbsp;

00:08:21.360 --> 00:08:27.120
ever crashes on something, it can at least&nbsp;
still render a blurry copy of the first page.

00:08:27.120 --> 00:08:31.680
Anyway, here are the results of my&nbsp;
parser compared to the source document.&nbsp;&nbsp;

00:08:31.680 --> 00:08:35.120
And here they are compared to&nbsp;
Mammoth's output. To be clear,&nbsp;&nbsp;

00:08:35.120 --> 00:08:39.440
I'm not throwing shade at Mammoth,&nbsp;
it's just not the right tool here.

00:08:39.440 --> 00:08:42.160
Of course, my tool also&nbsp;
supports parsing PowerPoint&nbsp;&nbsp;

00:08:42.160 --> 00:08:44.960
and Excel documents, to a limited extent.

00:08:44.960 --> 00:08:49.680
In retrospect, I'm sure that forking and skimming&nbsp;
LibreOffice would've been the correct approach,&nbsp;&nbsp;

00:08:49.680 --> 00:08:54.800
and my makeshift custom tool likely doesn't have&nbsp;
the stability of all the others, but hey – at&nbsp;&nbsp;

00:08:54.800 --> 00:09:00.480
least it supports formatting! I'm definitely&nbsp;
way past recovery from the sunk cost fallacy at&nbsp;&nbsp;

00:09:00.480 --> 00:09:06.160
this point, so I kind of have to use this, even&nbsp;
if I think LibreOffice would be a better fit.

00:09:06.160 --> 00:09:11.200
As you can probably tell by now, this is&nbsp;
not a serious project. This is just me&nbsp;&nbsp;

00:09:11.200 --> 00:09:16.800
goofing around with a silly idea, hoping to make&nbsp;
something mildly practical out of it. That said,&nbsp;&nbsp;

00:09:16.800 --> 00:09:21.600
if you do genuinely want to improve either&nbsp;
of these tools, the GitHub links are in the&nbsp;&nbsp;

00:09:21.600 --> 00:09:27.840
description. And also in the description is&nbsp;
a link to this video's sponsor, Coddy.Tech.

00:09:27.840 --> 00:09:31.040
I've talked a lot about Coddy on this&nbsp;
channel, and their hands-on approach to&nbsp;&nbsp;

00:09:31.040 --> 00:09:35.840
learning programming languages. Many people have&nbsp;
pointed out that it feels kind of like Duolingo,&nbsp;&nbsp;

00:09:35.840 --> 00:09:42.000
but for programming. But one thing that I haven't&nbsp;
mentioned yet is their courses – these are sets of&nbsp;&nbsp;

00:09:42.000 --> 00:09:47.040
practical lessons and challenges which walk you&nbsp;
through the process of building real programs.&nbsp;&nbsp;

00:09:47.040 --> 00:09:51.440
Frankly, I wish I had discovered this feature&nbsp;
sooner, because I think it solves a fundamental&nbsp;&nbsp;

00:09:51.440 --> 00:09:56.240
problem that many learners have, which is&nbsp;
that they often don't know what to make.&nbsp;&nbsp;

00:09:56.240 --> 00:10:01.440
Coddy's courses focus on specific goals and&nbsp;
teach you everything you need to know so that,&nbsp;&nbsp;

00:10:01.440 --> 00:10:06.560
by the end of it, you will have built your&nbsp;
very own solution to a real programming task.&nbsp;&nbsp;

00:10:06.560 --> 00:10:10.640
The courses are maintained by real people and&nbsp;
the Coddy team, though if you ever get stuck,&nbsp;&nbsp;

00:10:10.640 --> 00:10:14.480
they do of course have an AI assistant&nbsp;
built into the editor. That said,&nbsp;&nbsp;

00:10:14.480 --> 00:10:18.640
if you'd rather take it easy and learn the&nbsp;
fundamentals of your language step-by-step,&nbsp;&nbsp;

00:10:18.640 --> 00:10:23.200
their coding journeys are still a great way&nbsp;
to learn the relevant programming languages of&nbsp;&nbsp;

00:10:23.200 --> 00:10:29.600
today. If any of that caught your attention, check&nbsp;
them out by using the link in the description.

00:10:29.600 --> 00:10:32.800
Let's take a break from the file&nbsp;
format stuff for a moment to talk&nbsp;&nbsp;

00:10:32.800 --> 00:10:37.040
about the second major problem&nbsp;
– converting between mediums.

00:10:37.040 --> 00:10:42.400
For visual stuff, like images, videos,&nbsp;
documents, animations, fonts and so on,&nbsp;&nbsp;

00:10:42.400 --> 00:10:47.760
the answer seems to be pretty obvious&nbsp;
– just convert them to an image! Sure,&nbsp;&nbsp;

00:10:47.760 --> 00:10:51.520
you might lose some quality and details&nbsp;
along the way, particularly when dealing&nbsp;&nbsp;

00:10:51.520 --> 00:10:55.520
with vector graphics, but the result&nbsp;
will likely be more or less what you&nbsp;&nbsp;

00:10:55.520 --> 00:11:01.200
expect. Raster images are a "good enough"&nbsp;
common factor for lots of different files.

00:11:01.200 --> 00:11:08.400
But what about audio? Sound doesn't convert to&nbsp;
an image in any obvious way. Some audio files&nbsp;&nbsp;

00:11:08.400 --> 00:11:12.400
may embed album art which you could extract,&nbsp;
but that's not really what we're talking about,&nbsp;&nbsp;

00:11:12.400 --> 00:11:17.200
is it? We need to find a way&nbsp;
to represent sound visually.

00:11:17.200 --> 00:11:21.920
Right away, you might propose a couple of&nbsp;
naive ways of going about this. For example,&nbsp;&nbsp;

00:11:21.920 --> 00:11:25.920
you could dump the audio data straight into&nbsp;
the pixel data, but that would just leave you&nbsp;&nbsp;

00:11:25.920 --> 00:11:31.520
with a bunch of incoherent noise. You might&nbsp;
also think to simply draw the audio waveform,&nbsp;&nbsp;

00:11:31.520 --> 00:11:38.240
but that wouldn't explain how images convert&nbsp;
back to audio. A waveform is one-dimensional.&nbsp;&nbsp;

00:11:38.240 --> 00:11:44.080
At any point along the X axis, there's only&nbsp;
one corresponding point on the Y axis. This&nbsp;&nbsp;

00:11:44.080 --> 00:11:50.320
is simply not true for an arbitrary image.&nbsp;
How are you going to get a wave from this?

00:11:50.320 --> 00:11:55.840
No, I want a solution that creates a semantically&nbsp;
significant image from the input audio,&nbsp;&nbsp;

00:11:55.840 --> 00:12:00.400
and can also produce a semantically&nbsp;
significant audio file from any input&nbsp;&nbsp;

00:12:00.400 --> 00:12:05.040
image. The solutions we've discussed&nbsp;
so far dabble in the spatial domain,&nbsp;&nbsp;

00:12:05.040 --> 00:12:09.120
but for this, we'll have to&nbsp;
dive into the frequency domain.

00:12:09.120 --> 00:12:14.480
You might remember from physics class that&nbsp;
all sound is just a wave. Any regular wave&nbsp;&nbsp;

00:12:14.480 --> 00:12:19.360
can be described using just a few parameters: the&nbsp;
frequency, which is how close the peaks are from&nbsp;&nbsp;

00:12:19.360 --> 00:12:24.080
each other horizontally, the amplitude, which is&nbsp;
the distance between the highest and lowest point,&nbsp;&nbsp;

00:12:24.080 --> 00:12:29.040
and the phase, which is the offset from the start&nbsp;
of the wave. Basically, you can think of these as&nbsp;&nbsp;

00:12:29.040 --> 00:12:34.480
different ways to squish, stretch, and move the&nbsp;
wave around. Audio engineers are likely boiling in&nbsp;&nbsp;

00:12:34.480 --> 00:12:39.040
rage right now due to the oversimplification,&nbsp;
but this explanation will do for now.

00:12:39.040 --> 00:12:47.280
However, a regular wave will only produce one&nbsp;
constant tone. More complicated sounds have to&nbsp;&nbsp;

00:12:47.280 --> 00:12:53.360
layer multiple waves on top of each other, and&nbsp;
change them over time to produce a waveform.

00:12:53.360 --> 00:12:58.080
That's ultimately all that an audio file&nbsp;
is – it's just the final wave gained by&nbsp;&nbsp;

00:12:58.080 --> 00:13:03.200
summing all of its component waves. So, to&nbsp;
get an additional dimension for our image,&nbsp;&nbsp;

00:13:03.200 --> 00:13:07.040
we can split this waveform back into&nbsp;
its component waves by using what's&nbsp;&nbsp;

00:13:07.040 --> 00:13:12.240
called a discrete Fourier transform.&nbsp;
This way, each point on the X axis now&nbsp;&nbsp;

00:13:12.240 --> 00:13:17.040
corresponds to multiple points on the&nbsp;
Y axis, just like pixels in an image.

00:13:17.040 --> 00:13:22.640
To actually translate this to pixels, we can order&nbsp;
the waves by frequency along the Y axis. Then,&nbsp;&nbsp;

00:13:22.640 --> 00:13:27.840
for each fragment of the wave, the red and green&nbsp;
color channels encode that wave's amplitude,&nbsp;&nbsp;

00:13:27.840 --> 00:13:30.960
while the blue color channel encodes the phase.

00:13:30.960 --> 00:13:36.640
And, just like that, we've represented a waveform&nbsp;
as a raster image. Not only that, we can then&nbsp;&nbsp;

00:13:36.640 --> 00:13:41.920
reverse this whole process to get back the input&nbsp;
audio file. Or, because this is just a grid of&nbsp;&nbsp;

00:13:41.920 --> 00:13:48.560
pixels, we can even convert arbitrary images to&nbsp;
audio, though that probably won't sound very good.

00:13:48.560 --> 00:13:54.160
Of course, I'm massively oversimplifying this&nbsp;
whole process. I'm not at all an expert on audio&nbsp;&nbsp;

00:13:54.160 --> 00:13:58.880
processing, so my implementation introduces&nbsp;
quite a bit of noise in certain areas. If&nbsp;&nbsp;

00:13:58.880 --> 00:14:04.320
you think can help improve this, please do.&nbsp;
Again, the GitHub link is in the description.

00:14:04.320 --> 00:14:10.400
With audial and visual media covered, you might be&nbsp;
tempted to say that that's it. Computers display&nbsp;&nbsp;

00:14:10.400 --> 00:14:15.760
pictures and play sound, so surely that's&nbsp;
all you need to represent all digital media.

00:14:15.760 --> 00:14:20.640
But let's look at this from a different&nbsp;
perspective. Audio, as we've already discussed,&nbsp;&nbsp;

00:14:20.640 --> 00:14:23.840
is one-dimensional. Images are two-dimensional,&nbsp;&nbsp;

00:14:23.840 --> 00:14:28.880
so that of course raises the question – do&nbsp;
we have any three-dimensional media? The&nbsp;&nbsp;

00:14:28.880 --> 00:14:33.600
most obvious answer comes in the form of&nbsp;
3D models. I mean, think about it – this&nbsp;&nbsp;

00:14:33.600 --> 00:14:40.320
raises the exact same problems that audio did.&nbsp;
Representing a 3D model as a 2D render loses a&nbsp;&nbsp;

00:14:40.320 --> 00:14:46.080
whole dimension of information. For that same&nbsp;
reason, it's also not a reversible process.

00:14:46.080 --> 00:14:50.800
My immediate reaction would be to slice the&nbsp;
model on every vertex along some axis. You&nbsp;&nbsp;

00:14:50.800 --> 00:14:54.800
could then use a library like three.js to&nbsp;
render the slices with an isometric camera&nbsp;&nbsp;

00:14:54.800 --> 00:14:59.440
and a depth-based shader. This would allow you&nbsp;
to later reconstruct the model from the slices&nbsp;&nbsp;

00:14:59.440 --> 00:15:05.120
based on their shape and pixel brightness.&nbsp;
At least, that's the idea. This is definitely&nbsp;&nbsp;

00:15:05.120 --> 00:15:09.680
outside of my area of expertise, and this&nbsp;
project is already dragging on longer than it&nbsp;&nbsp;

00:15:09.680 --> 00:15:14.480
should. If you're up for the task, community&nbsp;
contributions are once again welcome here.

00:15:14.480 --> 00:15:18.960
Thankfully, I don't think I can come up&nbsp;
with any examples of four-dimensional media,&nbsp;&nbsp;

00:15:18.960 --> 00:15:25.840
so... well, actually, no. 3D animations do&nbsp;
count as four-dimensional. I suppose you&nbsp;&nbsp;

00:15:25.840 --> 00:15:29.440
could divide them into multiple output&nbsp;
files like we already do for videos,&nbsp;&nbsp;

00:15:29.440 --> 00:15:34.160
but... Ugh, again, I'll just&nbsp;
stick with two dimensions for now.

00:15:34.160 --> 00:15:39.280
All that's left is to figure out how&nbsp;
exactly all of these tools link together.

00:15:39.280 --> 00:15:43.760
Let's say you have a tool that handles&nbsp;
videos, and a tool that handles images.&nbsp;&nbsp;

00:15:43.760 --> 00:15:49.120
You've been tasked to convert an MP4 video&nbsp;
into a series of PNGs. Neither one of these&nbsp;&nbsp;

00:15:49.120 --> 00:15:54.720
tools can do that on its own, so you'll have to&nbsp;
find a way to "link" them together. To do that,&nbsp;&nbsp;

00:15:54.720 --> 00:16:00.640
you can look at the list of formats supported by&nbsp;
both tools and find one that they have in common.

00:16:00.640 --> 00:16:06.880
In this case, both FFmpeg and ImageMagick natively&nbsp;
support the GIF format – it's not quite an image,&nbsp;&nbsp;

00:16:06.880 --> 00:16:14.000
but it's also not quite a video. So, your MP4 can&nbsp;
become a GIF, and that GIF can then become PNG.

00:16:14.000 --> 00:16:19.920
By this logic, we can algorithmically determine&nbsp;
whether any one tool is compatible with another.&nbsp;&nbsp;

00:16:19.920 --> 00:16:24.480
You just need a list of formats outputted by&nbsp;
the first tool, and a list of formats accepted&nbsp;&nbsp;

00:16:24.480 --> 00:16:29.680
by the second tool. If there's a match&nbsp;
in these lists, you've got a connection.

00:16:29.680 --> 00:16:35.040
So then, the big idea is to throw all of our&nbsp;
tools into one big pile, and have the program&nbsp;&nbsp;

00:16:35.040 --> 00:16:40.960
route through all of them to get from point A to&nbsp;
point B, chaining different formats along the way.

00:16:40.960 --> 00:16:46.240
It's also important to note that we don't just&nbsp;
want any path. We want the shortest path with&nbsp;&nbsp;

00:16:46.240 --> 00:16:50.640
the fewest links, to improve speed and&nbsp;
reduce quality loss. We also want to&nbsp;&nbsp;

00:16:50.640 --> 00:16:56.320
weigh our choices towards more mainstream and&nbsp;
ideally lossless formats for that same reason.

00:16:56.320 --> 00:17:01.280
I think it helps to visualize the different&nbsp;
tools as nodes in a tree-like data structure.&nbsp;&nbsp;

00:17:01.280 --> 00:17:06.560
We start at a root node, and make a list of&nbsp;
all the tools compatible with our input. Then,&nbsp;&nbsp;

00:17:06.560 --> 00:17:11.520
we search each of these tools to see if they're&nbsp;
capable of outputting our target format. Or,&nbsp;&nbsp;

00:17:11.520 --> 00:17:16.800
in other words, we're checking if they're&nbsp;
connected to some target node. If they're not,&nbsp;&nbsp;

00:17:16.800 --> 00:17:22.400
we descend downwards and repeat the first&nbsp;
step to generate a new list of child nodes.&nbsp;&nbsp;

00:17:22.400 --> 00:17:26.480
We again check if any of these tools are&nbsp;
capable of outputting our target format,&nbsp;&nbsp;

00:17:26.480 --> 00:17:31.040
and if not, we move to an adjacent&nbsp;
node, repeating the same process.

00:17:31.040 --> 00:17:36.240
This is a common tree search algorithm known&nbsp;
as breadth-first search. It guarantees that&nbsp;&nbsp;

00:17:36.240 --> 00:17:40.320
whatever solution we find is the shortest&nbsp;
one in terms of the number of nodes,&nbsp;&nbsp;

00:17:40.320 --> 00:17:45.760
as we only descend downward when we've exhausted&nbsp;
all options available on the current level.

00:17:45.760 --> 00:17:48.960
In order to bias the search&nbsp;
towards more favorable formats,&nbsp;&nbsp;

00:17:48.960 --> 00:17:54.480
we can simply reorder the nodes horizontally&nbsp;
so that the better formats are looked at first.

00:17:54.480 --> 00:17:59.280
And, really, that's all there is to it. There's&nbsp;
a bit more logic to make sure that we don't&nbsp;&nbsp;

00:17:59.280 --> 00:18:03.520
route through the same format more than once.&nbsp;
And each candidate path is tested for errors,&nbsp;&nbsp;

00:18:03.520 --> 00:18:09.120
as some routes tend to only be compatible on&nbsp;
paper, and not in practice. This also means that,&nbsp;&nbsp;

00:18:09.120 --> 00:18:15.520
to add a new format, you just have to be able to&nbsp;
convert it to and from just one of the already&nbsp;&nbsp;

00:18:15.520 --> 00:18:21.680
supported formats. The algorithm will then&nbsp;
automatically find its way to everything else.

00:18:21.680 --> 00:18:27.200
Still, the algorithm itself is really not&nbsp;
the hard part. The hard part is getting the&nbsp;&nbsp;

00:18:27.200 --> 00:18:33.600
required information out of all these tools, and&nbsp;
standardizing it so that it can be cross-compared.

00:18:33.600 --> 00:18:37.040
Most file converter programs don't&nbsp;
really keep a list of supported&nbsp;&nbsp;

00:18:37.040 --> 00:18:41.840
formats. It's surprisingly difficult&nbsp;
to accurately label a file format.

00:18:41.840 --> 00:18:47.840
Take MP4 for example. It might seem like the&nbsp;
".mp4" file extension would be a good enough&nbsp;&nbsp;

00:18:47.840 --> 00:18:55.280
label. But no! Far from it! MP4 is really&nbsp;
just a container for a range of video and&nbsp;&nbsp;

00:18:55.280 --> 00:19:00.000
audio formats. It might not matter much for&nbsp;
the end user, but it does matter for the&nbsp;&nbsp;

00:19:00.000 --> 00:19:07.440
program doing the conversion – is this an H.264&nbsp;
codec? H.265? AV1? Is the audio AAC? Or maybe&nbsp;&nbsp;

00:19:07.440 --> 00:19:14.000
Opus? This is actually the reason why some MP4&nbsp;
files embed in Discord while others don't. So,&nbsp;&nbsp;

00:19:14.000 --> 00:19:20.480
of course, FFmpeg lists MP4 in a group&nbsp;
of a whole bunch of other containers.

00:19:20.480 --> 00:19:26.640
PNG has a similar issue. Yes, most of the&nbsp;
time, the ".png" extension is sufficient,&nbsp;&nbsp;

00:19:26.640 --> 00:19:31.680
but not always. Animated PNGs&nbsp;
are rare, but they do exist,&nbsp;&nbsp;

00:19:31.680 --> 00:19:36.400
and they often use the same ".png"&nbsp;
extension. Getting this wrong could&nbsp;&nbsp;

00:19:36.400 --> 00:19:41.600
mean treating a video file as an image&nbsp;
file, which is obviously not what we want.

00:19:41.600 --> 00:19:46.720
Other files have the opposite problem. Take&nbsp;
XML, for example. As previously discussed,&nbsp;&nbsp;

00:19:46.720 --> 00:19:51.840
this is used internally in Office documents,&nbsp;
but it's also the backbone of SVG images and&nbsp;&nbsp;

00:19:51.840 --> 00:19:59.360
RSS feeds. These are obviously different mediums,&nbsp;
but they all share the same underlying format.

00:19:59.360 --> 00:20:03.120
In order to better categorize each&nbsp;
file in my system, I label them all&nbsp;&nbsp;

00:20:03.120 --> 00:20:08.240
by their MIME type. "MIME" stands for&nbsp;
"Multipurpose Internet Mail Extensions",&nbsp;&nbsp;

00:20:08.240 --> 00:20:12.400
and as the name suggests, this was&nbsp;
initially a standard for email. But,&nbsp;&nbsp;

00:20:12.400 --> 00:20:16.320
because of its usefulness in categorizing&nbsp;
file formats by their semantic meaning,&nbsp;&nbsp;

00:20:16.320 --> 00:20:20.800
it's become a sort of de facto standard&nbsp;
for all kinds of file-related things.

00:20:20.800 --> 00:20:25.760
Now, admittedly, this is still not a perfect&nbsp;
system, but it is far better than just using&nbsp;&nbsp;

00:20:25.760 --> 00:20:30.880
file extensions. Still, there are many formats&nbsp;
that simply don't have a MIME type, and plenty&nbsp;&nbsp;

00:20:30.880 --> 00:20:37.440
of other formats have too many MIME types. And,&nbsp;
in rare cases, one MIME type can correspond to&nbsp;&nbsp;

00:20:37.440 --> 00:20:44.000
multiple formats, as is the case with MP2 and&nbsp;
MP3. Regardless of which way you look at it,&nbsp;&nbsp;

00:20:44.000 --> 00:20:49.680
there just is no universal way to identify&nbsp;
a file. We're working with what we've got.

00:20:50.240 --> 00:20:53.840
Finally, let's talk performance. A website like&nbsp;&nbsp;

00:20:53.840 --> 00:20:57.280
this is obviously going to&nbsp;
be very heavy. After all,&nbsp;&nbsp;

00:20:57.280 --> 00:21:02.720
we're making the user download tens of megabytes&nbsp;
of binaries before they can even do anything.

00:21:02.720 --> 00:21:07.760
But if you're just here to convert a PNG to a&nbsp;
JPEG and don't care about all that exotic stuff,&nbsp;&nbsp;

00:21:07.760 --> 00:21:11.360
you probably don't need to load the&nbsp;
video or document tools. You don't&nbsp;&nbsp;

00:21:11.360 --> 00:21:15.760
even need to load ImageMagick, really.&nbsp;
Instead, we can simply cache the lists&nbsp;&nbsp;

00:21:15.760 --> 00:21:21.520
of supported formats and only load the&nbsp;
actual programs when they're being used.

00:21:21.520 --> 00:21:26.480
Now, as you can imagine, the silliness of this&nbsp;
project is defined solely by the formats that&nbsp;&nbsp;

00:21:26.480 --> 00:21:31.600
it supports. Sure, you can transcode your&nbsp;
videos to MP4 or convert your images to PNG,&nbsp;&nbsp;

00:21:31.600 --> 00:21:36.560
but that's not as fun as a 20-page&nbsp;
PDF document of the Valve intro. Also,&nbsp;&nbsp;

00:21:36.560 --> 00:21:40.720
that example from the start of the video&nbsp;
wasn't a joke – this thing really does support&nbsp;&nbsp;

00:21:40.720 --> 00:21:45.600
rendering Portal 2 replay files. I know that&nbsp;
absolutely nobody will ever use that feature,&nbsp;&nbsp;

00:21:45.600 --> 00:21:50.080
but I had to mention it here so that the&nbsp;
time spent adding it feels less like a waste.

00:21:50.080 --> 00:21:55.520
In an ideal world, this would support every&nbsp;
file format to some degree. But, of course,&nbsp;&nbsp;

00:21:55.520 --> 00:22:01.040
that's an unrealistically time-consuming&nbsp;
task. That's why, once again, I invite you&nbsp;&nbsp;

00:22:01.040 --> 00:22:05.840
to contribute, if you can. All it takes to&nbsp;
add support for a new format is to write a&nbsp;&nbsp;

00:22:05.840 --> 00:22:10.720
bit of code that converts it to something that&nbsp;
we already support. From there, the algorithm&nbsp;&nbsp;

00:22:10.720 --> 00:22:16.240
can take it away and route you to literally&nbsp;
any other format on the list automatically.

00:22:16.240 --> 00:22:19.120
So, as I said before, the links to this project,&nbsp;&nbsp;

00:22:19.120 --> 00:22:23.920
as well as the GitHub repositories, are&nbsp;
in the description. Thanks for watching.

