Fundamentals 8 min read

How to Convert a Dancing Video into an ASCII Art Video with Python

This guide walks you through downloading a B‑site video, extracting GIF frames, converting them to ASCII art, renaming and ordering the frames, turning the ASCII GIFs into images, assembling them into a video with OpenCV, and adding background music using moviepy, all with Python code.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
How to Convert a Dancing Video into an ASCII Art Video with Python

First, install you-get to download the target video from Bilibili:

<code>pip install you-get</code>

Download the video with:

<code>you-get -o <local_path> <video_url></code>

Next, extract short GIF clips (about 20 seconds each) from the video using a GIF capture tool, naming them sequentially (1, 2, 3 …). Convert each GIF to ASCII art with an ASCII Animator, adjusting the character density (e.g., 100 px width) and outputting animated GIFs.

After conversion, copy the generated ASCII GIFs from the temp folder into your Python project.

Rename and sort the GIF frames so they follow the correct order. The following script renames GIF files based on numeric patterns and sorts them:

<code>def rename_gif():
    file_list = os.listdir("./temp")  # read all files
    print("检测到文件夹下图片:")
    n = len(file_list)
    num_list = []
    num1 = num2 = 0
    for i in range(n):
        s = str(file_list[i])
        if s[-4:] == ".gif":  # check suffix
            res = re.findall(r"\d+", s)
            if res[0] == '1':
                num1 += 1
            if res[0] == '2':
                num2 += 1
            src = os.path.join(os.path.abspath('./temp/'), s)  # original name
            dst = os.path.join(os.path.abspath('./temp/'), res[0] + '-' + res[1] + '.gif')  # new name
            os.rename(src, dst)  # rename
    num_list.append(num1)
    num_list.append(num2)
    file_list = os.listdir("./temp")  # re‑read files
    for i in range(n):
        s = str(file_list[i])
        if s[-4:] == ".gif":
            res = re.findall(r"\d+", s)
            src = os.path.join(os.path.abspath('./temp/'), s)
            a = int(res[0]) - 1
            index = a * num_list[a-1]
            dst = os.path.join(os.path.abspath('./temp/'), str(index + int(res[1])) + '.gif')
            os.rename(src, dst)
</code>

Convert the ordered ASCII GIFs to JPEG images using Pillow:

<code>def gif2img(gif_path):
    gifs = os.listdir(gif_path)
    gifs.sort(key=lambda x: int(x[:-4]))  # sort by number
    for gif in gifs:
        im = Image.open(gif_path + gif)  # open GIF
        im = im.convert('RGB')
        if not os.path.exists('./img'):
            os.makedirs('./img')
        for i, frame in enumerate(iter_frames(im)):
            frame.save('./img/' + gif[0:-4] + '.jpg', **frame.info)  # save as JPG
</code>

Assemble the JPEG images into a video with OpenCV (install opencv-python if needed):

<code>pip install opencv-python</code>
<code>def charts2video(img_path, video_path):
    """Convert images in a directory to a video.
    Args:
        img_path: directory of images
        video_path: output video file
    """
    images = os.listdir(img_path)
    images.sort(key=lambda x: int(x[:-4]))
    fps = 12
    fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    im = Image.open(img_path + images[0])
    video_writer = cv2.VideoWriter(video_path, fourcc, fps, im.size)
    for img_i in images:
        frame = cv2.imread(img_path + img_i)
        print('开始将 ' + img_i + ' 加入视频\n')
        video_writer.write(frame)
    video_writer.release()
</code>

Finally, add background music to the generated ASCII video using moviepy :

<code>def add_music():
    # read the ASCII video
    my_clip = mpy.VideoFileClip('asc.mp4')
    # extract background music (first 60 s)
    audio_background = mpy.AudioFileClip('dance.mp4').subclip(0, 60)
    audio_background.write_audiofile('bk.mp3')
    # set audio to video
    final_clip = my_clip.set_audio(audio_background)
    # save final video
    final_clip.write_videofile('char_video.mp4')
</code>

After completing these steps, you obtain a “code dance” video that visualizes the original dancing clip as animated ASCII art, complete with background music.

Pythonvideo processingopencvascii artMoviePy
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.