Transforming Videos into ASCII Art in C++

September 26, 2023 (1y ago)

Have you ever wondered what your favorite video would look like if it were made entirely of text characters? One afternoon, fueled by curiosity and coffee, I decided to find out. The result? A C++ program that transforms any video into a flowing stream of ASCII characters in real-time. Let me take you through this fascinating journey of creative coding.

The Magic Behind ASCII Art

ASCII art has been around since the early days of computing, when printers and terminals could only display text characters. By using different characters to represent varying levels of brightness, we can create surprising detailed images using nothing but text. Now imagine doing this 30 times per second to create fluid video – that's exactly what we're going to build.

Building the ASCII Video Converter

The Core Components

Our ASCII video converter relies on two main pieces:

  1. OpenCV - A powerful computer vision library that handles video processing
  2. C++ - Our language of choice for its performance and low-level control

Key Features

  • Real-time Processing: Watch your video transform into ASCII art as it plays
  • Dynamic Resizing: Automatically adjusts to your terminal size while maintaining aspect ratio
  • Customizable Character Set: Fine-tune the visual output by modifying the ASCII characters used
  • Cross-platform Support: Runs on Windows, macOS, and Linux

Let's Build It!

Step 1: Setting Up Your Environment

First, you'll need to grab the code and set up your environment:

# Clone the repository
git clone git@github.com:jmartinn/vid-to-ascii.git
 
# For macOS users, install OpenCV using Homebrew
brew install opencv

Step 2: Configuration

The setup process requires a few configuration tweaks to ensure everything works smoothly:

  1. Update OpenCV Paths:
# In compile_commands.json, update the OpenCV path:
g++ -std=c++11 -I/opt/homebrew/Cellar/opencv/<your_version>/include/opencv4 ...
  1. Set Your Video Path:
// In main.cpp
string video_path = "./video.mp4";  // Replace with your video path
  1. Prepare the Build Script:
# Make the build script executable
chmod +x build.sh

Step 3: Running the Converter

With everything set up, it's showtime:

./build.sh

Under the Hood

The conversion process follows these steps:

  1. Read each frame from the video
  2. Convert the frame to grayscale
  3. Resize to fit the terminal
  4. Map each pixel's brightness to an ASCII character
  5. Display the resulting ASCII frame

The magic happens in the pixel-to-ASCII conversion, where we map different brightness levels to specific characters. Darker pixels become denser characters like '@' or '#', while lighter pixels become lighter characters like '.' or ' '.

Troubleshooting Tips

  • If you're getting OpenCV-related errors, double-check your paths using brew info opencv
  • Make sure your terminal window is large enough for the ASCII output
  • For performance issues, try reducing the input video resolution

Future Enhancements

There's always room for improvement! Some ideas I'm considering:

  • Color support using ANSI escape codes
  • Custom character mappings via configuration files
  • GUI interface for real-time parameter adjustment
  • Video export functionality

Ready to Try It?

The complete source code is available on GitHub. Feel free to experiment, modify, and create your own ASCII masterpieces. Whether you're creating art or just having fun with code, this project shows how programming can transform the ordinary into something extraordinary.


"Art is not what you see, but what you make others see." – Edgar Degas