├── README.md
├── build.sh
├── compile_commands.json
├── main.cpp
└── vid1.mp4
/README.md:
--------------------------------------------------------------------------------
1 | # Video to ASCII Converter (C++)
2 |
3 | This repository contains a C++ implementation of a real-time video-to-ASCII converter. It takes input from a webcam or video file and generates an ASCII art representation of the video content. The project uses OpenCV library to handle video input and processing.
4 |
5 | ## Table of Contents
6 |
7 | 1. [Features](#features)
8 | 2. [Dependencies](#dependencies)
9 | 3. [Building and Running](#building-and-running)
10 | 4. [Usage](#usage)
11 | 5. [Example](#example)
12 | 6. [Contributing](#contributing)
13 | 7. [License](#license)
14 |
15 | ## Features
16 |
17 | - Real-time video-to-ASCII conversion
18 | - Supports input from webcam or video files
19 | - Adjustable ASCII character set and resolution
20 | - Easily customizable to suit your needs
21 | - Cross-platform support (tested on Windows and Linux)
22 |
23 | ## Dependencies
24 |
25 | This project depends on the following libraries:
26 |
27 | - [OpenCV](https://opencv.org/) (>= 4.0)
28 |
29 | ## Building and Running
30 |
31 | ### MacOS
32 |
33 | 1. Install OpenCV and set up the environment.
34 | 2. If you are using vim edit the compile_commands.json. Find the path of opencv and change with the path in compile_commands.json
35 | 3. Modify the opencv path in build.sh
36 | 4. Give permission to build.sh by:
37 | $ chmod +x build.sh
38 | 5. Run in terminal:
39 | $ ./build.sh
40 |
41 | ## Usage
42 |
43 | 1. Edit the path of video in main.cpp
44 | 2. Run in terminal:
45 | $ ./build.sh
46 |
47 | ## Contributing
48 |
49 | Contributions are welcome! Please feel free to open an issue or submit a pull request if you find a bug, have a feature request, or would like to improve the project in any way.
50 |
51 | ## License
52 |
53 | This project is licensed under the MIT License.
54 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | g++ -std=c++11 -I/opt/homebrew/Cellar/opencv/4.7.0_3/include/opencv4 -L/opt/homebrew/opt/opencv/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core -o exe main.cpp
2 |
3 | ./exe
4 |
--------------------------------------------------------------------------------
/compile_commands.json:
--------------------------------------------------------------------------------
1 | [
2 | {
3 | "directory": "/Users/servetg/Projects/yt/video-to-ascii-cpp",
4 | "command": "g++ -std=c++11 -I/opt/homebrew/Cellar/opencv/4.7.0_3/include/opencv4 -L/opt/homebrew/opt/opencv/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core -o exe main.cpp",
5 | "file": "/Users/servetg/Projects/yt/video-to-ascii-cpp/main.cpp"
6 | }
7 | ]
8 |
--------------------------------------------------------------------------------
/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include
4 | #include
5 | #include
6 | using namespace std;
7 | using namespace cv;
8 |
9 | string pixelToASCII(int pixel_intensity) {
10 | // const string ASCII_CHARS = "@$%#&!*+=-_. ";
11 | // const string ASCII_CHARS = "@#&!*+=-_. ";
12 | const string ASCII_CHARS = " ._-=+*!%$@";
13 | string s =
14 | string(1, ASCII_CHARS[pixel_intensity * ASCII_CHARS.length() / 256]);
15 | return s;
16 | }
17 |
18 | int main() {
19 | string video_path = "/Users/servetg/Projects/yt/video-to-ascii-cpp/vid1.mp4";
20 | VideoCapture cap(video_path);
21 |
22 | double fps = cap.get(CAP_PROP_FPS);
23 |
24 | cout << fps << endl;
25 |
26 | int frame_duration_ms = 1000 / fps;
27 |
28 | int width = 250;
29 | int height = 50;
30 |
31 | int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
32 | int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
33 | cout << frame_width << " " << frame_height << endl;
34 | // 4096 2160
35 | // 751 × 944
36 |
37 | height = (width * frame_height / frame_width) * 0.4194;
38 |
39 | Mat frame, gray_frame, resized_frame;
40 |
41 | while (true) {
42 | cap >> frame;
43 | if (frame.empty())
44 | break;
45 |
46 | cv::cvtColor(frame, gray_frame, cv::COLOR_BGR2GRAY);
47 |
48 | resize(gray_frame, resized_frame, Size(width, height), 0, 0, INTER_LINEAR);
49 |
50 | string ascii_frame;
51 | for (int i = 0; i < height; i++) {
52 | for (int j = 0; j < width; j++) {
53 | ascii_frame += pixelToASCII(resized_frame.at(i, j));
54 | }
55 | ascii_frame += "\n";
56 | }
57 |
58 | system("clear"); // to clear the console
59 | cout << ascii_frame;
60 | std::this_thread::sleep_for(std::chrono::milliseconds(frame_duration_ms));
61 | }
62 |
63 | return 0;
64 | }
65 |
--------------------------------------------------------------------------------
/vid1.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/servetgulnaroglu/video-to-ascii-cpp/2de2a1c818f43d8c183b5fe34f4fb81aef96a669/vid1.mp4
--------------------------------------------------------------------------------