Convert a Dancing Video into an ASCII Art Video with Python
This tutorial explains how to download a B‑site dance video, extract GIF frames, transform them into ASCII art, rename and sort the frames, convert them to images, assemble a video with OpenCV, and finally add background music using MoviePy.
This guide shows step‑by‑step how to turn a dancing video into a "code dance" using Python.
1. Download the video – Install you-get and run: pip install you-get Then download the B‑site video with: you-get -o <local_path> <video_url>
2. Extract GIF and convert to ASCII – Use a GIF cutter (e.g., the built‑in tool of the Xunlei player) to capture 20‑second clips, name them sequentially, and convert each GIF frame to ASCII using ASCII Animator, adjusting the character width to 100 pixels for desired density.
3. Rename GIF frames – Import required libraries and run the provided script to rename and sort the GIF files: import os, re, shutil, cv2<br/>from PIL import Image<br/>import moviepy.editor as mpy def rename_gif():<br/> file_list = os.listdir("./temp")<br/> # ... (renaming logic) ...
After renaming, all GIFs are ordered by frame sequence.
4. Convert GIFs to JPG images – Use the following function to read each GIF, extract frames, and save them as JPGs: def gif2img(gif_path):<br/> gifs = os.listdir(gif_path)<br/> gifs.sort(key=lambda x: int(x[:-4]))<br/> for gif in gifs:<br/> im = Image.open(gif_path+gif).convert('RGB')<br/> for i, frame in enumerate(iter_frames(im)):<br/> frame.save('./img/' + gif[:-4] + '.jpg', **frame.info)
5. Assemble the video – Install OpenCV and run the script to combine the JPG images into a video: pip install opencv-python def charts2video(img_path, video_path):<br/> images = os.listdir(img_path)<br/> images.sort(key=lambda x: int(x[:-4]))<br/> fps = 12<br/> fourcc = cv2.VideoWriter_fourcc('M','P','4','V')<br/> im = Image.open(img_path + images[0])<br/> video_writer = cv2.VideoWriter(video_path, fourcc, fps, im.size)<br/> for img_i in images:<br/> frame = cv2.imread(img_path + img_i)<br/> video_writer.write(frame)<br/> video_writer.release()
6. Add background music – Use MoviePy to extract audio from the original video and merge it with the generated ASCII video: def add_music():<br/> my_clip = mpy.VideoFileClip('asc.mp4')<br/> audio_background = mpy.AudioFileClip('dance.mp4').subclip(0, 60)<br/> audio_background.write_audiofile('bk.mp3')<br/> final_clip = my_clip.set_audio(audio_background)<br/> final_clip.write_videofile('char_video.mp4')
After completing these steps, the original dance video is transformed into an ASCII‑art video with background music, ready for sharing.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.