Of course! To convert the video to ASCII art using libcaca, you need to change the ffmpeg output format. Instead of encoding to a new video file (output.mkv), you'll pipe the output directly to libcaca for real-time display in your terminal.
Here is the rewritten script:
#!/bin/bash
# Get terminal size for optimal scaling.
# The aspect ratio of characters is not 1:1, so we divide height by 2 for a better look.
COLS=$(tput cols)
LINES=$(( $(tput lines) / 2 ))
# Check if we got dimensions, otherwise use a safe default.
if ! [[ "$COLS" =~ ^[0-9]+$ ]] || ! [[ "$LINES" =~ ^[0-9]+$ ]]; then
COLS=120
LINES=40
echo "Could not detect terminal size, defaulting to ${COLS}x${LINES} characters."
fi
echo "Rendering ASCII art at ${COLS}x${LINES} characters..."
podman run --rm -it \
--device=/dev/dri:/dev/dri \
-v $(pwd):/config \
linuxserver/ffmpeg \
-vaapi_device /dev/dri/renderD128 \
-i /config/input.mp4 \
-vf "scale=${COLS}:${LINES}" \
-an \
-f caca \
-
How It Works: Key Changes
- Terminal Size Detection: The script now automatically detects your terminal's width (
cols) and height (lines) to scale the ASCII art appropriately. It halves the lines because terminal characters are typically twice as tall as they are wide, preventing the image from looking "stretched".
- Interactive TTY (
-it): This flag was in your original script, but now it's critical. It connects your terminal to the container's output, allowing libcaca to draw the ASCII art directly to your screen.
- Removed Output File Options: All options related to creating a video file have been removed:
-c:v h264_vaapi (video codec)
-b:v 4M (bitrate)
-c:a copy (audio stream)
/config/output.mkv (output file path)
- New Video Filter (
-vf):
- The complex VA-API scaling chain was replaced with a simple software scaler:
-vf "scale=${COLS}:${LINES}".
- We scale the video down to the number of characters in your terminal. This is essential for a clear ASCII image. The hardware acceleration is now primarily for decoding the input file, which is still a benefit.
- No Audio (
-an): We explicitly disable the audio stream since a terminal can't play it.
- CACA Output Format (
-f caca): This is the core of the change. It tells ffmpeg to use the libcaca library to format its output.
- Output to stdout (
-): The final - tells ffmpeg to send its output to "standard output" instead of a file. The podman run -it command then displays this output in your terminal.
How to Use
- Save the code above as a script (e.g.,
ascii-player.sh).
- Make it executable:
chmod +x ascii-player.sh.
- Make sure you have a video file named
input.mp4 in the same directory.
- Run it:
./ascii-player.sh.
The video will start "playing" as ASCII art in your terminal. Press q or Ctrl+C to stop it.
A Simpler Version (Without Hardware Acceleration)
If you don't have or don't need VA-API hardware acceleration, you can use a much simpler version of the command.
#!/bin/bash
# Use a fixed size or uncomment the lines below to use terminal size
# COLS=$(tput cols)
# LINES=$(( $(tput lines) / 2 ))
COLS=120
LINES=40
podman run --rm -it \
-v $(pwd):/config \
linuxserver/ffmpeg \
-i /config/input.mp4 \
-vf "scale=${COLS}:${LINES}" \
-an \
-f caca \
-