How to Write a Video Player in Less Than 1000 Lines (2015)
dranger.com
dranger.com
import json
json.loads('{"That": "was easy"}')
(To be fair, knowing ffmpeg exists doesn't mean I'd be able to easily write a video player with it without a lot of research. For that reason I find this tutorial is still quite interesting and valuable.)Don't get me wrong, this is a cool SDL <> ffmpeg integration tutorial.
[1] https://developer.apple.com/documentation/avkit/avplayerview...
It's fine if you are in the Apple ecosystem but many formats from Windows or Linux will fail with it.
The tutorial is still 1000 lines of C, involving SDL, ffmpeg and threads.
Yikes :)
Last time I did something like this (a UI that among other things, played a live stream from a cheapo IP camera), I used python and the libvlc python bindings. Creating a bare bones media player with it was trivial and it worked very well for what I needed. The only complaint I had at the time was that the documentation was absolutely terrible. A cursory look today reveals that they seem to have improved it, so yay?
And yes, the end result in my case came out MUCH smaller than 1000 lines (but lines of code is a shitty metric anyway).
If you "just" need a media player, IMHO, libvlc is not an awful option (in Python, at least - I have no experience with other bindings): https://wiki.videolan.org/LibVLC/
The equivalent to what you're saying would be if he'd just used the HTML5 <video> tag.
This is actually a great article, it taught me a little bit more about FFMPEG under the hood, and knowledge is power.
I also like that it just used C.
That seems to imply that, in 1kLoC of C, you should be able to write a complete video player for MPEG-1, and not much more beyond that. If you're even the slightest bit interested in video compression, I'd recommend giving it a try starting with '261 --- it's not as difficult as it sounds at first glance, and the results are very visible.
(When I have the time, I plan to continue this exercise with H.263, MPEG-4 ASP, and then H.264.)
The maths has actually gotten simpler; the predecessors are all traditional DCT-based (JPEG-like) codecs while H.264 uses a simple integer approximation. Regardless, from a decoder implementation perspective, the theory behind it all is not that important; the spec tells you exactly what you need to do, and the increase in complexity is mainly reflected in needing to write more code to handle the larger number of cases.
H.261 is simple because it has a choice of only two resolutions, one framerate, two frame types (I and P), and 10 macroblock types.
H.262/MPEG-2 is already a significant increase in complexity since it supports a varying set of resolutions, framerates, interlacing, and chroma subsampling, has 3 frame types (I, P, B), 20 macroblock types (dependent on frame type), and more complex motion compensation.
H.263 has no interlacing but adds optional intra prediction, more advanced motion vector encoding, and a few other features.
H.264 has about 50 macroblock types, even more complex intra prediction, and motion compensation has a larger choice of which frames to predict from.
For the naysayers out there, a better title would be "Writing a Video Player in Less Than 1000 Lines of C using FFmpeg and SDL"
Seems like a pretty good hint that you are not actually writing a video player in less than 1000 lines.
I mean it's a cool article, but the presentation is misleading and taking credit for something that it is not. And some people who aren't in programming actually see titles like that and think that this type of thing is literally 'easy to program'. Another example is when people say they quickly 'built their own browser'.
I guess I can see how it might be seen as boastful or showboating, but to me it signals "This might be a fun coding project for a single afternoon."
ffplay how_to_write_a_blog_post_with_a_better_title.movOn the other hand there are so many edge cases around codecs and assumptions around what is and is not coming so I think that if a different API was created there would likely be a lot of cases where it would break? Something like the NDI API would be great, it is really quick to integrate.
<html>
<head>
<title>Basic video player</title>
</head>
<body>
<video width = "5122" height = "51" controls>
<source src = "video.mp4" type = "video/mp4">
</video>
</body>
</html>Otherwise it can be interpreted that in those 1k lines is included the heavy workload that ffmpeg arguably does it in more then 10k lines.
Then I did the math and I guess now I’m an old guy that listens to... classic rock?
Back on topic: For anyone using ffmpeg/libav this tutorial might as well be the official documentation. Absolutely indispensable in my opinion.
(current top comment here is https://news.ycombinator.com/item?id=23435669)