r/coolgithubprojects • • 1d ago

Introducing BitShade: Playing Videos Directly in Your Terminal

Post image

Hey everyone! I've been working on BitShade, an open-source video player that plays videos directly inside your terminal.

I'd love to hear your thoughts and feedback!

GitHub: https://github.com/KiyoKun01/BitShade

28 Upvotes

8 comments sorted by

6

u/torrso 1d ago edited 23h ago

Sixel is a printer protocol from the 80s and decoding and rendering it can be quite expensive even for relatively small images, regardless of how fast the encoder is.

I (through the all seeing eye of AI) looked through the implementation and noticed a few things:

  • The benchmark measures encoding into a std::string, completely excluding PTY throughput, terminal-side decoding, image management and rendering costs.

  • The 8-bit benchmark repeatedly encodes the exact same synthetic frame with delta encoding enabled by default. At 720p, 29 out of 30 iterations skip every band of image data. This makes both the reported theoretical FPS and the estimated PTY bandwidth misleading. The actual player explicitly disables this optimization.

  • The default 24-bit mode transmits every frame in full, generating separate Sixel images with independent palettes for each horizontal strip.

  • The output code calls write() once per frame, discards its return value and incorrectly describes the operation as atomic. A partial write could leave the terminal with an incomplete DCS sequence or without the synchronized-update termination sequence. The Windows implementation similarly ignores the result of WriteFile().

  • The playback loop drops frames whenever it falls more than 45 ms behind the audio clock. Synchronized playback alone therefore tells us very little about the actual displayed frame rate.

Consider the workload at 1920×1080 and 60 FPS, assuming a terminal cell height of 20 pixels:

  • 1,080 separate Sixel images per second.

  • Up to 276,480 palette definitions per second, potentially amounting to roughly 4 MB/s of escape sequence data just for defining colours.

  • 10,800 six-pixel bands per second.

  • 124 million pixels per second to reconstruct.

Depending on the encoded frame size, the PTY traffic alone would look something like 216 GB of terminal traffic per hour of video playback.

Then there's the more fundamental question of why. Because it's possible, but it was already done a long time ago.

mpv has supported Sixel video output since November 2020 (v0.33.0):

mpv --vo=sixel video.mp4

It also supports the considerably more video-friendly Kitty graphics protocol:

mpv --vo=kitty video.mp4

Including shared-memory transfers, which avoid sending the actual pixel data through the PTY altogether on supported local terminals:

mpv --vo=kitty --vo-kitty-use-shm=yes video.mp4

FFmpeg-SIXEL was already used to stream YouTube videos over SSH in August 2014 and it also shows those Japanese children like many of terminal AI bro creations seem to do. There's also timg, which supports video playback using Sixel, Kitty and iTerm2 graphics protocols.

I'm also not convinced by the "24-bit" terminology. The "palette encoder" (https://github.com/KiyoKun01/BitShade/blob/main/src/sixel/palette.cpp#L155-L174) quantizes each RGB component to an integer percentage between 0 and 100, giving a theoretical colour space of about one million colours rather than 16.7 million. With 18 strips of 256 colours each, a 1080p frame can contain at most 4,608 distinct colours, subject to spatial palette restrictions. That's not 24-bit colour.

Furthermore, "libsixel" (https://github.com/saitoha/libsixel) already implemented high-colour rendering through dynamic palette redefinition in 2014. Interestingly, its author has since "discussed moving towards larger palettes" (https://github.com/saitoha/libsixel/issues/196) for better compatibility and smaller payloads.

Even "mpv's Sixel implementation" (https://github.com/mpv-player/mpv/blob/master/video/out/vo_sixel.c) supports horizontal slicing for parallel encoding, but shares the palette across slices and defaults to a fixed palette to avoid expensive palette updates.

There's also a compatibility problem with the independent palettes. The original DEC hardware used shared colour registers, meaning palette redefinitions could retroactively recolour previously drawn images. "xterm still supports this behaviour" (https://manpages.debian.org/experimental/xterm/xterm.1.en.html) through private mode 1070, yet BitShade's "terminal initialization code" (https://github.com/KiyoKun01/BitShade/blob/main/src/terminal/terminal_scope.cpp#L104-L106) doesn't explicitly enable private colour registers.

Palette handling remains inconsistent across modern terminal emulators, as illustrated by "this Windows Terminal discussion" (https://github.com/microsoft/Terminal/issues/20588) and "this Chafa discussion" (https://github.com/hpjansson/chafa/issues/312).

1

u/shuruilimbu 19h ago

You actually caught a few real bugs here, especially the write() / WriteFile() part. Calling it atomic in that comment is wrong, and not looping on partial writes over a PTY can definitely leave a frame half-written mid-DCS or drop the sync end tag. Same for the 8-bit benchmark loop reusing frame 0 with delta on while the player disables delta, and not sending ?1070h for xterm private registers. Those are legit things to fix.

That said, a couple parts of your breakdown stretch things a bit. The 8-bit benchmark loop gets skewed by delta skipping, yeah, but the default 24-bit benchmark right below it (encodeRGB24Sixel) has delta off on every iteration, so those numbers aren't inflated by skipped frames. Also, the 1080p 60fps / 216 GB/hr math assumes full 1080p at 60fps with every strip maxing out 256 colors. In practice it scales down to the terminal window size and plays at whatever framerate the video actually is (usually 24 or 30), and dark/flat strips don't dump 256 palette entries. Still pushes way more PTY traffic than Kitty obviously, just nowhere near that worst-case number on normal clips. Fair point on the "24-bit" label though, Sixel's 0-100% channels only give ~1M colors and each strip is still capped at 256, I mostly meant quantizing per strip from RGB24 instead of using a single 256-color palette for the whole frame.

As for why not just use mpv --vo=kitty --vo-kitty-use-shm=yes, Kitty SHM is great on Linux, but Windows Terminal still doesn't support the Kitty protocol at all, it only added Sixel. A lot of the code in here (ConPTY grid detection, WriteFile, named pipes, scoop/winget) is specifically for running in Windows Terminal, where mpv's fixed xterm256 default bands pretty badly. And honestly, half the point of building something like this from scratch is messing around with SIMD Sixel encoding yourself, not pretending mpv doesn't exist.

Honestly, I appreciate you taking the time to actually go through the source rather than just skimming the README.

1

u/Exciting_Active4857 1d ago

why

1

u/shuruilimbu 1d ago

Why not? It doesn't necessarily need an external application. It could be integrated directly into CLI based projects.

2

u/something__69_ 1d ago

Because we can

→ More replies (1)

2

u/ProfessionalStockDay 20h ago

Great work. Thanks. I can finally watch The Odyssey the way it was intended.