Turn a Bilibili Dance Clip into an ASCII‑Art Video with Python
Learn how to download a Bilibili dance video, extract GIF frames, convert them to ASCII art, rename and order the frames, transform them into images, and finally stitch them into a music‑backed video using Python tools such as you‑get, OpenCV, and moviepy.
Preface
Inspired by a popular dancing video on Bilibili, this tutorial shows how to turn the clip into a "code dance"—an ASCII‑art video generated with Python.
1. Core Function Design
The workflow consists of six main steps:
Download the video from Bilibili.
Cut the video into GIFs and convert each GIF frame to ASCII characters.
Rename the ASCII GIF frames in sequential order.
Convert the ordered GIF frames to JPG images.
Combine the images into a video.
Add background music to the final video.
2. Implementation Steps
1. Download video
Install you-get to fetch the video. pip install you-get Use the following command (replace VIDEO_URL with the actual link) to save the video locally:
you-get -o /path/to/save VIDEO_URL2. Cut GIF and convert to ASCII
Extract 20‑second GIF segments using a GIF cutter (e.g., the built‑in tool of Xunlei player). Rename each segment sequentially (1‑gif, 2‑gif, …). Then use ASCII Animator to convert each GIF frame to ASCII characters, adjusting the character density (e.g., 100 px width) and outputting an animated GIF.
3. Rename GIF frames
Import the required libraries and rename the GIF files so that they follow a numeric order suitable for later merging.
import os
import re
import shutil
import cv2
from PIL import Image
import moviepy.editor as mpy
def rename_gif():
file_list = os.listdir("./temp")
# Count frames for each original segment
num1 = num2 = 0
for f in file_list:
if f.endswith('.gif'):
nums = re.findall(r"\d+", f)
if nums[0] == '1':
num1 += 1
elif nums[0] == '2':
num2 += 1
src = os.path.abspath(f'./temp/{f}')
dst = os.path.abspath(f'./temp/{nums[0]}-{nums[1]}.gif')
os.rename(src, dst)
# Second pass to reorder based on segment counts
file_list = os.listdir("./temp")
for f in file_list:
if f.endswith('.gif'):
nums = re.findall(r"\d+", f)
src = os.path.abspath(f'./temp/{f}')
a = int(nums[0]) - 1
index = a * (num1 if a == 0 else num2)
dst = os.path.abspath(f'./temp/{index + int(nums[1])}.gif')
os.rename(src, dst)4. Convert GIF to JPG images
Read the ordered GIFs, extract each frame, convert to RGB, and save as JPG files.
def gif2img(gif_path):
gifs = os.listdir(gif_path)
gifs.sort(key=lambda x: int(x[:-4]))
for gif in gifs:
im = Image.open(os.path.join(gif_path, gif))
im = im.convert('RGB')
if not os.path.exists('./img'):
os.makedirs('./img')
for i, frame in enumerate(iter_frames(im)):
frame.save(f'./img/{gif[:-4]}.jpg', **frame.info)5. Assemble the video
Install OpenCV and use it to write the JPG sequence into a video file. The frame rate can be adjusted (e.g., 12 fps).
pip install opencv-python def charts2video(img_path, video_path):
images = os.listdir(img_path)
images.sort(key=lambda x: int(x[:-4]))
fps = 12
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
first_img = Image.open(os.path.join(img_path, images[0]))
writer = cv2.VideoWriter(video_path, fourcc, fps, first_img.size)
for img_name in images:
frame = cv2.imread(os.path.join(img_path, img_name))
writer.write(frame)
writer.release()6. Add background music
Use moviepy to extract the original video's audio and overlay it onto the ASCII video.
def add_music():
video_clip = mpy.VideoFileClip('asc.mp4')
audio_bg = mpy.AudioFileClip('dance.mp4').subclip(0, 60)
audio_bg.write_audiofile('bk.mp3')
final = video_clip.set_audio(audio_bg)
final.write_videofile('char_video.mp4')After completing these steps, the original dancing clip is transformed into an ASCII‑art video with synchronized background music.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
